Relational Algebra
-
Upload
alfreda-reynolds -
Category
Documents
-
view
22 -
download
0
description
Transcript of Relational Algebra
![Page 1: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/1.jpg)
Relational Algebra
![Page 2: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/2.jpg)
Relational Algebra
• Set operations: Union, intersection, difference, Cartesian product
• Relational operations: Selection, projection, join, division
![Page 3: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/3.jpg)
Union
• Set1={A, B, C}
• Set2={C, D, E}
• Union: Members in Set 1 or in Set 2– Set1 U Set 2 = {A, B, C, D, E}
• Or
![Page 4: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/4.jpg)
Intersect
• Members in Set 1 and in Set 2– Set1 ∩ Set2={C}
• And
![Page 5: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/5.jpg)
Difference
• Set1 – Set2: Members in Set1 but not in set2 = {A,B}
• Set2 – Set1:Members in Set2 but not in set1 = {D, E}
• Set1-Set2 ≠ Set2 – Set1
![Page 6: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/6.jpg)
Union Compatibility
• Two relations that have the same number of attributes and same type of attributes.
• Union, Intersect and difference operators require the two relations to be union compatible.
![Page 7: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/7.jpg)
• File 1: – SID – 9 characters– Sname – 25 characters
• File 2:– SSN – 9 characters– Ename – 25 characters
• File 3:– Ename – 25 characters– EID – 9 characters
![Page 8: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/8.jpg)
Use Union and Difference to Simulate Intersect
• Set1 ∩ Set2 = Set1 – (Set1 – Set2)
![Page 9: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/9.jpg)
Venn DiagramSet 1: Major = ‘Business’
Set 2: Sex = ‘F’
Set 3: GPA > 3.0
![Page 10: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/10.jpg)
Files as Sets
• Business students’ file: BusSt• Science student’s file: SciSt
– BusSt U SciSt:– BusSt ∩ SciSt– BusSt – SciSt
• Spring 04 Student file: S04St• Fall 04 Student file: F04St
– S04St – F04St– F04St – S04St
![Page 11: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/11.jpg)
Product
• Set1 = {a, b, c}
• Set2 = {X, Y, Z}
• Set1 X Set2 = {aX, aY aZ, bX, bY, bZ, cX, cY, cZ}
![Page 12: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/12.jpg)
• Faculty File: • FID Fname• F1 Chao• F2 Smith
• Student File:• SID Sname FID• S1 Peter F1• S2 Paul F2• S3 Smith F1
• Faculty X Student:
![Page 13: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/13.jpg)
Selection
• Selection operation works on a single relation and defines a relation that contains records that satisfy the criteria.
– σ criteria ( Relation)
– σ Major = ‘Bus’ AND GPA > 3.0 (Student)
![Page 14: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/14.jpg)
Projection
• Projection operation works on a single relation and defines a vertical subset of the relation, extracting the values of specified attributes and eliminating duplicates.
• π a1, a2, … (Relation)
• π sid, sname (Student)
![Page 15: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/15.jpg)
• Student file: • SID, Sname Sex Major• S1 Peter M Bus• S2 Paul M Art• S3 Mary F Bus• S4 Nancy F Sci• S5 Peter M Art
• π sid, sname (Student)• π sname, sex (Student)
• π sname, sex (σ Major = ‘Bus’ (Student))
• σ Major = ‘Bus’ (π sname, sex (Student))
![Page 16: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/16.jpg)
Duplications due to Projection• WorkLog file:
• EID PjID Hours• E1 P2 5• E1 P1 4• E2 P2 6• E2 P1 8• E3 P1 4
• π eid (WorkLog)• Relation contrains: no duplication• Eliminating duplicates may cause problems:
– π Hours (σ PjID = ‘P1 (WorkLog))• In practice, users determine whether to eliminate
duplicates:– SELECT DISTINCT EID FROM WorkLog;– SELECT HOURS FROM WorkLog WHERE PjID = ‘P1’;
![Page 17: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/17.jpg)
Natural Join
• The two relations must have common attributes.
• Combines two relations to form a new relation where records of the two relations are combined if the common attributes have the same value. One occurrence of each common attribute is eliminated.
![Page 18: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/18.jpg)
Faculty File: FID FnameF1 ChaoF2 Smith
Student File:SID Sname FIDS1 Peter F1S2 Paul F2S3 Smith F1
Faculty Join Student =
π All except the duplicated attributes (σ Faculty.FID = Student.FID (Faculty X Student))
Note: Use RelationName.FieldName to make a field name unique.
![Page 19: Relational Algebra](https://reader035.fdocuments.us/reader035/viewer/2022072015/56813066550346895d963f82/html5/thumbnails/19.jpg)
Examples
• University database:– Student: SID, Sname, Sex, Major, GPA, FID– Account: SID, Balance– Faculty: FID, Fname– Course: CID, Cname, Credits– StudentCourse: SID, CID