Download presentation

Presentation is loading. Please wait.

Published byHelena Philpott Modified over 2 years ago

1
Kumarapathirana K. P. S. D. Section 3.0

2
Introduction The basic set of operations for the relational model is the relational algebra User specifies basic retrieval requests The result of a retrieval is a new relation The algebra operations thus produce new relations

3
Introduction A sequence of relational algebra operations forms a relational algebra expression Why …….. ? –I–It provides a formal foundation for relational model operations –I–It is used as a basis for implementing and optimizing queries –S–Some of its concepts are incorporated into the SQL

4
Two types –O–Operations developed specifically for relational databases Eg:SELECT, PROJECT and JOIN –S–Set operations from mathematical set theory Eg: UNION, INTERSECTION, SET DIFFERENCE, and CARTESIAN PRODUCT

5
Two types –U–Unary operations Operate on single relation Eg:SELECT and PROJECT –B–Binary operations Operate on two relations Eg: JOIN, UNION, INTERSECTION, MINUS ….

6
Unary Relational Operations SELECT –T–This operation is used to select a subset of the tuples from a relation that satisfy a selection condition –A–A filter –T–The SELECT operation is a horizontal partition of the relation into two sets of tuples Those tuples that satisfy the condition and are selected Those tuples that do not satisfy the condition and are discarded.

7
–R–Representation σ

8
The relation resulting from the SELECT operation has the same attributes as R

9
Example : σ FNAME = Nimal (R) SELECT * FROM EMPLOYEE WHERE FNAME = ‘Nimal’ EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 34SunimalKeerthiPerera33Production 22LalithVithanaNimalasena45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing

10
EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 34SunimalKeerthiPerera33Production 22LalithVithanaNimalasena45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 22LalithVithanaNimalasena45Finance To select the EMPLOYEE tuples whose department is ‘Finance’,

11
EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 34SunimalKeerthiPerera33Production 22LalithVithanaNimalasena45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 34SunimalKeerthiPerera33Production 22LalithVithanaNimalasena45Finance To select the EMPLOYEE tuples whose age is greater than 30

12
The way the result is determined ……. –<–

13
The number of tuples in the resulting relation is always less than or equal to the number of tuples in R |σ c (R)| <= |R| The SELECT operation is commutative σ

14
A cascade of SELECT operations can be combined into a single SELECT operation with a conjunctive (AND) condition σ (σ (….(σ (R)) = σ AND AND …. AND (R)

15
Exercise…. Find the students of IIT & CST degree program Find the 4 th year students whose degree program is SCT SIDFNAMEMNAMELNAMEDEGPROYEAR 12SahanNilankaPereraCST2 23NuwanLasanthaSilvaIIT3 45ChamaliNadeeKumariSCT4 50VipuliSamadhiPereraCST2 03NimaliPubuduPereraSCT4 11VishvaLakmalFernandoCST3 34LasithPiumalSilvaIIT2

16
PROJECT –S–Selects certain columns from the table and discards the other columns –v–vertical partition of the relation into two relations: One has the needed columns (attributes) and contains the result of the operation The other contains the discarded columns

17
–R–Representation ∏

18
To list each employee's employee id, first and last name EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 34SunimalKeerthiPerera33Production 22LalithVithanaNimalasena45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing EIDFNAMEMNAME 12NimalKumara 34SunimalKeerthi 22LalithVithana 35ChathuraShanaka 11WicramaSapumal

19
If the attribute list includes only non-key attributes of R –D–Duplicate tuples are likely to occur –P–PROJECT operation removes any duplicate tuples Duplicate elimination –E–Eg: ∏ LNAME,AGE,DEPARTMENT (Employee) EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage45Finance 34SunimalKeerthiPerera33Production 22LalithVithanaLiyanage45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing LNAMEAGEDEPARTMENT Liyanage45Finance Perera33Production Liyanage45Finance Silva20Sales Amarathunga23Marketing LNAMEAGEDEPARTMENT Liyanage45Finance Perera33Production Silva20Sales Amarathunga23Marketing

20
The number of tuples in a relation resulting from a PROJECT operation is always less than or equal to the number of tuples in R If the projection list is a superkey of R, the resulting relation has the same number of tuples as R ∏ list1 (∏ list2 R) = ∏ list1 (R) If list 2 contains list 1

21
Exercise…. Find the names of students of all the students Find id and the degree program of all the students SIDFNAMEMNAMELNAMEDEGPROYEAR 12SahanNilankaPereraCST2 23NuwanLasanthaSilvaIIT3 45ChamaliNadeeKumariSCT4 50VipuliSamadhiPereraCST2 03NimaliPubuduPereraSCT4 11VishvaLakmalFernandoCST3 34LasithPiumalSilvaIIT2

22
Sequences of Operations Several relational algebra operations can be applied one after the other –W–We can write the operations as a single relational algebra expression –W–We can apply one operation at a time and create intermediate result relations We must give names to the relations that hold the intermediate results.

23
Eg: –R–Retrieve the first name, last name, and age of all employees who work in finance department ∏ FNAME,LNAME,AGE ( σ DEPARTMENT = FINANCE (Employee)) EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage45Finance 34SunimalKeerthiPerera33Production 22LalithVithanaLiyanage45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing FNAMELNAMEAGE NimalLiyanage45 LalithLiyanage45

24
Eg: –R–Retrieve the first name, last name, and age of all employees who work in finance department FinDept_Emp σ

25
Exercise…. Find the names of 2 nd year students of IIT & CST degree program Find the first name and the degree program of students with sid 12, 50 and 34 SIDFNAMEMNAMELNAMEDEGPROYEAR 12SahanNilankaPereraCST2 23NuwanLasanthaSilvaIIT3 45ChamaliNadeeKumariSCT4 50VipuliSamadhiPereraCST2 03NimaliPubuduPereraSCT4 11VishvaLakmalFernandoCST3 34LasithPiumalSilvaIIT2

26
It is often simpler to break down a complex sequence of operations We can also use this technique to rename the attributes in the intermediate and result relations FinDept_Emp σ

27
RENAME –R–Rename either the relation name or the attribute names, or both –R–Repesentation ρ S(B1, B2,.... Bn) (R) ρ S (R) ρ (B1,B2,...,Bn) (R)

28
Eg: FinDept_Emp σ

29
Exercise…. Find the names of 2 nd year students of IIT & CST degree program name them as FIRSTNAME, MIDDLENAME and LASTNAME Find the ids and the degree program of students with sid 12, 50 and 34 name the new relation as ENROLLMENTS with the column names ID and ENROLLED_DEGREE_PROGRAM SIDFNAMEMNAMELNAMEDEGPROYEAR 12SahanNilankaPereraCST2 23NuwanLasanthaSilvaIIT3 45ChamaliNadeeKumariSCT4 50VipuliSamadhiPereraCST2 03NimaliPubuduPereraSCT4 11VishvaLakmalFernandoCST3 34LasithPiumalSilvaIIT2

30
Relational Algebra Operations From Set Theory UNION Result1 = ∏

31
RESULT = RESULT1 υ RESULT2 EID 12 34 22 35 11 EID 03 04 12 34 01 RESULT1RESULT2 EID 12 34 22 35 11 03 04 01 RESULT Duplicate tuples are eliminated

32
–T–The UNION operation produces the tuples that are in either RESULT1 or RESULT2 or both –T–Two relations R(A1, A2,..., An) and S(B1,B2,…, Bn) are said to be union compatible If they have the same degree n If dom(Ai) = dom(Bi) for 1< i < n –c–commutative operations R υ S = S υ R

33
Exercise…. Find the employees who works on project ‘345’ Find the employees who works on project ‘678’ Find the employees who works on project ‘345’ or ‘678’ with the results of the above two answers. EIDFNAMEMNAMELNAME 12SahanNilankaPerera 23NuwanLasanthaSilva 45ChamaliNadeeKumari EIDPROJECTID 12345 12678 12463 23678 23345 45463 45345

34
INTERSECTION Result1 = ∏

35
RESULT= RESULT1 RESULT2 EID 12 34 22 35 11 EID 03 04 12 34 01 RESULT1RESULT2 EID 12 34 RESULT υ

36
Result is a relation that includes all tuples that are in both relations commutative operations R S = S R associative operations (R S) T = R (S T) υ υ υυυυ

37
Exercise…. Find the employees who works on project ‘345’ Find the employees who works on project ‘678’ Find the employees who works on project ‘345’ & ‘678’ with the results of the above two answers. EIDFNAMEMNAMELNAME 12SahanNilankaPerera 23NuwanLasanthaSilva 45ChamaliNadeeKumari EIDPROJECTID 12345 12678 12463 23678 23345 45463 45345

38
MINUS / SET DIFFERENCE Result1 = ∏

39
RESULTX = RESULT1 – RESULT2 RESULTY = RESULT2 – RESULT1 EID 12 34 22 35 11 EID 03 04 12 34 01 RESULT1RESULT2 EID 03 04 01 RESULTY EID 22 35 11 RESULTX

40
The result of this operation, denoted by R - S, is a relation that includes all tuples that are in R but not in S not commutative R - S ≠ S - R

41
Exercise…. Find the employees who works on project ‘345’ Find the employees who works on project ‘678’ Find the employees who is working on project ‘345’ and not working on project ‘678’ with the results of the above two answers. EIDFNAMEMNAMELNAME 12SahanNilankaPerera 23NuwanLasanthaSilva 45ChamaliNadeeKumari EIDPROJECTID 12345 12678 12463 23678 23345 45463 45345

42
The CARTESIAN PRODUCT Operation –K–Known as CROSS PRODUCT or CROSS JOIN –B–Binary set operation –T–The relations do not have to be union compatible –C–Combines tuples from two relations in a combinatorial fashion –d–denoted by X –T–The result of R(A 1, A 2,..., A n ) X S(B 1, B 2,..., B m ) will be Q(A 1, A 2,..., A n, B 1, B 2,..., B m ) –I–If R has n R tuples, and S has n S tuples, then R x S will have n R * n S tuples

43
SIDNameAgeDegreeProgram 112Sahan21CST 212Achini21IIT 234Nalaka22SCT DegreeProgram Code DegreeProgram Name CSTComputer Science & Technology IITIndustrial Information Technology SCTScience & Technology STUDENT DEGREEPROGRAM Eg :

44
SIDNameAgeDegreeProgramDegreeProgramCodeDegreeProgramName 112Sahan21CST Computer Science & Technology 212Achini21IIT CSTComputer Science & Technology 234Nalaka22SCT CSTComputer Science & Technology 112Sahan21CST IITIndustrial Information Technology 212Achini21IIT Industrial Information Technology 234Nalaka22SCT IITIndustrial Information Technology 112Sahan21CST SCTScience & Technology 212Achini21IIT SCTScience & Technology 234Nalaka22SCT Science & Technology STUDENT X DEGREEPROGRAMINTERMEDIATE_RESULT

45
∏ SID,NAME,AGE,DEGREEPROGRAMNAME ( σ DegreeProgramCode = DegreeProgram (INTERMEDIATE_RESULT)) SIDNameAgeDegreeProgramName 112Sahan21 Computer Science & Technology 212Achini21 Industrial Information Technology 234Nalaka22 Science & Technology

46
BINARY RELATIONAL OPERATIONS JOIN –D–Denoted by –C–Combine related tuples from two relations into single tuples. –A–Allows us to process relationships among relations –R–Representation S

47
SIDNameAgeDegreeProgram 112Sahan21CST 212Achini21IIT 234Nalaka22SCT DegreeProgram Code DegreeProgram Name CSTComputer Science & Technology IITIndustrial Information Technology SCTScience & Technology STUDENT DEGREEPROGRAM Eg : INTERMEDIATERESULT SIDNameAgeDegreeProgramDegreeProgramCodeDegreeProgramName 112Sahan21CST Computer Science & Technology 212Achini21IIT Industrial Information Technology 234Nalaka22SCT Science & Technology INTERMEDIATERESULT STUDENT DEGREEPROGRAM RESULT ∏ SID,NAME,AGE,DEGREEPROGRAMNAME (INTERMEDIATERESULT) SIDNameAgeDegreeProgramName 112Sahan21 Computer Science & Technology 212Achini21 Industrial Information Technology 234Nalaka22 Science & Technology RESULT

48
SIDNameAgeDegreeProgramName 112Sahan21 Computer Science & Technology 212Achini21 Industrial Information Technology 234Nalaka22 Science & Technology SIDNameAgeDegreeProgram 112Sahan21CST 212Achini21IIT 234Nalaka22SCT DegreeProgram Code DegreeProgram Name CSTComputer Science & Technology IITIndustrial Information Technology SCTScience & Technology STUDENT DEGREEPROGRAM Eg : RESULT ∏ SID,Name,Age,DegreeProgramName ( STUDENT DegreeProgram = DegreeProgramCode DEGREEPROGRAM) RESULT

49
A general join condition is of the form AND AND... AND

50
Equi Join –W–Where only equality comparisons is used

51
Natural Join –T–The two join attributes have the same name in both relations –I–If not, a renaming operation is applied first –D–Denoted by * –R–Representation R * S R * ρ

52
SIDNameAgeDegreeProgramName 112Sahan21 Computer Science & Technology 212Achini21 Industrial Information Technology 234Nalaka22 Science & Technology SIDNameAgeDegreeProgram 112Sahan21CST 212Achini21IIT 234Nalaka22SCT DegreeProgram Code DegreeProgram Name CSTComputer Science & Technology IITIndustrial Information Technology SCTScience & Technology STUDENT DEGREEPROGRAM Eg : RESULT STUDENT * ρ (DegreeProgram, Name) DEGREEPRGRAM RESULT

53
SIDNameAgeDegreeProgramName 112Sahan21 Computer Science & Technology 212Achini21 Industrial Information Technology 234Nalaka22 Science & Technology SIDNameAgeDegreeProgram 112Sahan21CST 212Achini21IIT 234Nalaka22SCT DegreeProgram Code DegreeProgram Name CSTComputer Science & Technology IITIndustrial Information Technology SCTScience & Technology STUDENT DEGREEPROGRAM Eg : DEGREE_PRO ρ (DegreeProgram, Name) DEGREEPRGRAM RESULT STUDENT * DEGREE_PRO RESULT

54
OUTER JOIN –W–When we want to keep all the tuples in R, or all those in S, or all those in both relations in the result of the JOIN –3–3 types LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN

55
–A–An extension of the join operation that avoids loss of information. –U–Uses null values: Null signifies that the value is unknown or does not exist

56
Relation loan Relation borrower customer-nameloan-number Jones Smith Hayes L-170 L-230 L-155 3000 4000 1700 loan-numberamount L-170 L-230 L-260 branch-name Downtown Redwood Perryridge

57
Inner Join loan Borrower loan-numberamount L-170 L-230 3000 4000 customer-name Jones Smith branch-name Downtown Redwood Jones Smith null loan-numberamount L-170 L-230 L-260 3000 4000 1700 customer-namebranch-name Downtown Redwood Perryridge Left Outer Join loan Borrower

58
Right Outer Join loan borrower Full Outer Join loan-numberamount L-170 L-230 L-155 3000 4000 null customer-name Jones Smith Hayes branch-name Downtown Redwood null loan-numberamount L-170 L-230 L-260 L-155 3000 4000 1700 null customer-name Jones Smith null Hayes branch-name Downtown Redwood Perryridge null

59
Aggregation function – Takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Aggregate operation in relational algebra – Representation G1, G2, …, Gn g F1( A1 ), F2( A2 ),…, Fn( An ) (E)

60
LOANNOBRANCHAMOUNT 233123122121Colombo23,322 324234234233Badulla34,211 434234232323Kandy12,213 231231312312Colombo23,221 342324234223Kandy12,332 g sum(AMOUNT) (ACCOUNT) AMOUNT 105,299 BRANCH g sum(AMOUNT) (ACCOUNT) BRANCHAMOUNT Colombo 46543 Badulla34,211 Kandy24545

61
Aggregate Functions (Cont.) Result of aggregation does not have a name – Can use rename operation to give it a name – For convenience, we permit renaming as part of aggregate operation branch-name g sum(balance) as sum-balance (account)

Similar presentations

OK

Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on acute myeloid leukemia Ppt on indian classical music Ppt on phase sequence checker for three phase supply Ppt on regional trade agreements definition Ppt on memory management in operating system Ppt on computer virus symptoms Ppt on power generation by speed breaker symbols Ppt on mars one application Hindi ppt on stress management Ppt on automobile related topics about economics