Download presentation
Presentation is loading. Please wait.
Published byDamian Sparks Modified over 9 years ago
1
CSE 4701 Chapter 6-1 Chapter 6 6e & 7 5e : Relational Algebra – Part 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT 06269-3155 steve@engr.uconn.edu http://www.engr.uconn.edu/~steve (860) 486 - 4818 n A large portion of these slides are being used with the permission of Dr. Ling Lui, Associate Professor, College of Computing, Georgia Tech. n The remainder of these slides have been adapted from the AWL web site for the textbook.
2
CSE 4701 Chapter 6-2 n Basic Relational Operations: l Unary Operations SELECT PROJECT or . l Binary Operations à Set operations: UNION INTERSECTION F DIFFERENCE – CARTESIAN PRODUCT à JOIN operations What is Relational Algebra? n Relational Algebra is a Procedural Paradigm You Need to Tell What/How to Construct the Result n Consists of a Set of Operators Which, When Applied to Relations, Yield Relations (Closed Algebra)
3
CSE 4701 Chapter 6-3 Relational Algebra R Sunion R Sintersection R \ Sset difference R SCartesian product A1, A2,..., An (R)projection F (R)selection R Snatural join R Stheta-join R Sdivision [ A 1 B 1,.., An Bn ]rename
4
CSE 4701 Chapter 6-4 Selection n Selects the Tuples (Rows) From a Relation, Which Satisfy a Selection Condition n Selection Produces a Horizontal Subset of the Operand Relation General Form F (R) l R is a Relation l F is a Boolean Expression on the Attributes of R l Resulting Relation Has the Same Schema as R n Select Finds and Retrieves All Relevant Rows (Tuples) of Table/Relation R which Includes ALL of its Columns (Attributes)
5
CSE 4701 Chapter 6-5 ENOENAMETITLE E1J. DoeElect. Eng E6L. ChuElect. Eng. TITLE='Elect. Eng.' (EMP) ENOENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLE='Elect. Eng.’ OR TITLE=‘Mech.Eng’ (EMP) Selection Example
6
CSE 4701 Chapter 6-6 Another Selection Example A S C null W B null
7
CSE 4701 Chapter 6-7 n A SELECT Condition is a Boolean Expression Form F 1 F 2 F q (Q>=1), Where l F i (I=1,…,q) are Atomic Boolean Expressions of the Form a c or a b, l a, b are Attributes of R and c is a Constant. The Operator is one of the Arithmetic Comparison Operators :, =, <> The Operator is one of the Logical Operators: , , ¬ l Nesting: ( ) Selection Condition
8
CSE 4701 Chapter 6-8 n Extract Only Certain Columns (Attributes) Specified in an Attribute List X From a Relation R n Produces a New Relation, which is a Vertical Subset of the Operand Relation R n The Schema (Columns) of the Resulting Relation is X General Form X (R) l R is a Relation l X is a Subset of the Attributes of R Over Which the Projection is Performed n Project Retrieves Specified Columns of Table/Relation R which Includes ALL of its Rows (Tuples) Projection
9
CSE 4701 Chapter 6-9 Projection Example PNO,BUDGET (PROJ) PNOBUDGET P1150000 P2135000 P3250000 P4310000 P5500000 PROJ PNOBUDGET P2135000 P3250000 P4310000 P5500000 PNAME P1150000Instrumentation Database Develop. CAD/CAM Maintenance CAD/CAM
10
CSE 4701 Chapter 6-10 Other Projection Examples
11
CSE 4701 Chapter 6-11 Relational Algebra Expression n Several Operations can be Combined to form a Relational Algebra Expression (query) n Example: Retrieve all Customers over age 60? Method 1: CNAME, ADDRESS, AGE ( AGE>60 (CUSTOMER) ) l Method 2: Senior-CUST(C#, Addr, Age) = CNAME, ADDRESS, AGE ( AGE>60 (CUSTOMER) ) Method 3: CNAME, ADDRESS, AGE (C) where C AGE>60 (CUSTOMER)
12
CSE 4701 Chapter 6-12 ENOENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLE (PROJ) Elect.Eng Syst.Anal Mec.Eng Programmer TITLE Characteristics of Projection n The PROJECT Operation Eliminates Duplicate Tuples in the Resulting Relation l Why? l Projection Must Maintain a Mathematical Set (No Duplicate Elements)
13
CSE 4701 Chapter 6-13 Selection with Projection Example
14
CSE 4701 Chapter 6-14 n General Form R S where R, S are Relations l Result contains Tuples from both R and S l Duplications are Removed l The two Operands R, S should be union-compatible (type-compatible w.r.t Columns/Attributes) n Example: “find students registered for course C1 or C3” s# ( CNO=‘C1’ (S-C)) s# ( CNO=‘C3’ (S-C)) Union
15
CSE 4701 Chapter 6-15 Union Compatibility n Two Relations R 1 (A 1, A 2,..., A n ) and R 2 (B 1, B 2,..., B n ) are said Union-compatible If and Only If They Have l The Same Number of Attributes l The Domains of Corresponding Attributes are Compatible, i.e., Dom(A i )=dom(B i ) for I=1, 2,..., N l Names Do Not Have to be Same! n For Relational Union and Difference Operations, the Operand Relations Must Be Union Compatible n The Resulting Relation for Relational Set Operations l Has the Same Attribute Names as the First Operand Relation R 1 (by Convention)
16
CSE 4701 Chapter 6-16 n General Form R – S where R and S are Relations l Result Contains all tuples that are in R, but not in S. l R – S <> S – R l Again, there Must be Compatibility n Example “Find the students who registered course C1 but not C3” s# ( CNO=‘C1’ (S-C)) – s# ( CNO=‘C3’ (S-C)) Set Difference
17
CSE 4701 Chapter 6-17 n General Form R S where R and S are Relations l Result Contains all Tuples that are in R and S. R S = R – (R – S) l Again, there Must be Compatibility n Example “find the students who registered for both C1 and C3” s# ( CNO=‘C1’ (S-C)) s# ( CNO=‘C3’ (S-C)) Set Intersection
18
CSE 4701 Chapter 6-18 Union, Difference, Intersection Examples What are these Other Three Result Tables?
19
CSE 4701 Chapter 6-19 n Given Relations l R of Degree k 1 and Cardinality card 1 l S of Degree k 2 and Cardinality card 2 n Cartesian Product R S is a Relation of Degree (k 1 + k 2 ) and Consists of Tuples of Degree (k 1 + k 2 ) where each Tuple is a Concatenation of one Tuple of R with one Tuple of S Cardinality of the Result of the Cartesian Product R S is card 1 * card 2 n What is One Problem with Cartesian Product w.r.t. the Result Set? Cartesian Product
20
CSE 4701 Chapter 6-20 Cartesian Product: Example ABC a1 a2 a3 b1 b1 b4 c3 c5 c7 F E f1 f5 e1 e2 ABCE a1 a2 a3 b1 b4 c3 c5 c7 e1 e2 e1 e2 e1 e2 RS R S F f1 f5 f1 f5 f1 f5
21
CSE 4701 Chapter 6-21 Given R(A1, …,An) and S(B1,…,Bm), the result of a Cartesian product R S is a relation of schema R’(A1, …, An, B1, …, Bm). n Example “Get a list containing (S#, C#) for all students who live in Storrs but are not registered for the database course” Cartesian Product ( S# ( city=‘Storrs’ ( STUDENT )) C# ( CNAME=‘Database’ ( COURSE ))) – S#, C# ( S-C )
22
CSE 4701 Chapter 6-22 Cartesian Product Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng.40000 Syst. Anal.34000 Mech. Eng.27000 Programmer24000
23
CSE 4701 Chapter 6-23 n General Form R S where l R, S are Relations, l F is a Boolean Expression, called a Join Condition. n A Derivative of Cartesian Product R S = (R S) R(A 1, A 2,..., A m, B 1, B 2,..., B n ) is the Resulting Schema of a -Join over R 1 and R 2 : R 1 (A 1, A 2,..., A m ) R 2 (B 1, B 2,..., B n ) Theta Join ( -Join)
24
CSE 4701 Chapter 6-24 A -Join Condition is a Boolean Expression of the form F 1 1 F 2 2 n-1 F q (q>=1), where l F i (i=1,…,q) are Atomic Boolean Expressions of the form A i B j, l A i, B j are Attributes of R 1 and R 2 Respectively is one of the Algorithmic Comparison Operators =, <>, >, =, <= The Operator i ( i=1,…,n-1 ) is Either a Logical AND operator or a logical OR operator -Join Condition
25
CSE 4701 Chapter 6-25 -Join Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng. 40000 Syst. Anal.34000 Mech. Eng.27000 Programmer24000 ENOENAME E1J. Doe M. SmithE2 E3A. Lee E4J. Miller E5B. Casey E6L. Chu E7R. Davis E8J. Jones TITLE Elect. Eng. Analyst Mech. Eng. Programmer Syst. Anal. Elect. Eng. Mech. Eng. Syst. Anal. SAL 40000 34000 27000 24000 34000 40000 27000 34000 EMP E.TITLE=SAL.TITLE SAL SAL.TITLE Elect. Eng. Analyst Mech. Eng. Programmer Syst. Anal. Elect. Eng. Mech. Eng. Syst. Anal.
26
CSE 4701 Chapter 6-26 Other Types of Join n Equi-join (EQUIJOIN) The Expression only Contains one or more Equality Comparisons Involving Attributes from R 1 and R 2 n Natural Join Denoted as R S l Special Equi-join of Two Relations R and S Over a Set of Attributes Common to both R and S l By Common, it means that each Join Attribute in A has not only Compatible Domains but also the Same Name in both Relations R and S
27
CSE 4701 Chapter 6-27 Examples ABC a1 a2 a3 b1 b1 b4 c3 c5 c7 BE b1 b5 e1 e2 RS R R.B=S.B S AR.B a1 a2 b1 CE c3 c5 e1 S.B b1 EQUIJOIN AR.BCE a1 a2 b1 c3 c5 e1 R S Natural Join
28
CSE 4701 Chapter 6-28 Natural Join n Natural Join Combines Relations on Attributes with the Same Names l STUDENT(S#, SN, CITY, Email) l S-C(S#, C#, G) n Example Query 1: “list of students with complete course grade info” STUDENT S-C n All Natural Joins can be Expressed by a Combination of Primitive Operators n Example Query 2: “print all students info (courses taken and grades)” S#, SN, CITY, Email, C#, G ( STUDENT.S# = S-C.S# (STUDENT S-C))
29
CSE 4701 Chapter 6-29 Natural Join Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng.70000 Syst. Anal.80000 Mech. Eng.56000 Programmer60000 ENOENAMEE.TITLE SAL E1J. DoeElect. Eng. 70000 E2M. SmithSyst. Anal. 80000 56000 80000 E3A. LeeMech. Eng. E8J. JonesSyst. Anal. EMP SAL 60000 E4J. MillerProgrammer 80000 E5B.CaseySyst.Anal 70000 E6L. ChuElect.Eng 56000 E7R.DavisMech.Eng
30
CSE 4701 Chapter 6-30 Another Natural Join Example
31
CSE 4701 Chapter 6-31 Yet Another Natural Join Example 14551455
32
CSE 4701 Chapter 6-32 n Given Relations l R(T,U) of degree r l S(U) of degree s n The Division of R by S, R ÷ S Results is a Relation of Degree (r s) Consists of all (r s)-tuples t such that for all s-tuples u in S, the tuple tu is in R. Quotient (Division)
33
CSE 4701 Chapter 6-33 Division Example ENO E3 R ÷ S ENOPNOPNAME E1P1Instrumentation 150000 BUDGET E2P1Instrumentation 150000 E2P2Database Develop.135000 E3P1Instrumentation E3P4Maintenance E4P2Instrumentation E5P2Instrumentation E6P4 E7P3CAD/CAM E8P3CAD/CAM 310000 150000 310000 250000 R Maintenance 150000 S PNO PNAME BUDGET P1 Instrumentation150000 P4Maintenance310000 Find the employees who work for both project P1 and project P4?
34
CSE 4701 Chapter 6-34 Division: Another Example n “list the S# of students that have taken all of the courses listed in S-C” S# (S-C) C# (S-C) n “list the S# of students who have taken all of the courses taught by instructor Smith” S# (S-C C# ( Instructor=‘Smith’ (C)))
35
CSE 4701 Chapter 6-35 Relational Algebra Fundamental OperatorsDerivable from the fundamental operators l Selection l Projection l Union l Difference l Cartesian Product n Intersection n Join, Equi-join, Natural Join
36
CSE 4701 Chapter 6-36 n A Set of Relational Algebra Operations Is Called a Complete Set, If and Only If l Any Relational Algebra Operator in the Set Cannot be Derived in Terms of a Sequence of Others in Set l Any Relational Algebra Operator Not in the Set Can Be Derived in Terms of a Sequence of Only the Operators in the Set n Important Concepts: The Set of Algebra Operations { , , , –, } is a Complete Set of Relational Algebra Operations · Any Query Language Equivalent to These Five Operations is Called Relationally Complete All Relational Algebra Operations
37
CSE 4701 Chapter 6-37 n Fundamental Operators l Selection l Projection l Union l Set Difference l Cartesian Product Relational Algebra: Summary Form: Result> Relation (s) Relation n Additional Operators l Join l Intersection l Quotient (Division) n Union Compatibility l Same Degree l Corresponding Attributes Defined Over the Same Domain
38
CSE 4701 Chapter 6-38 Semi-Join General form: R F S R is Target Relation, S is the Source Relation n Semi-join l A Form of Join where the Result Contains only those Tuples of the Target Relation, which Participate in the Join with the Source Relation n Benefit: Decreases the Number of Tuples that need to be Handled in the Join R F S = (R F S) (R) F (S) = R F (S) l where A is a set of Attributes of R, B is a set of attributes of S
39
CSE 4701 Chapter 6-39 Semi-Join Example BCD b2 b3 b4 b5 b6 b7 b8 b9 b10 S A B R b1 b3 b4 a1 a2 a3 a4. R SAL R B S) = c1 c2 c3 c4 d9 d8 d7 d9 d8 d7 d5 d4 d9 AB b3 b4 a3 a4.
40
CSE 4701 Chapter 6-40 Additional DB Operations n Additional Operations, Needed for Database Applications, but were Not Part of the Original Relational Algebra, Include: l OUTER JOIN l OUTER UNION l Aggregate Functions and Grouping
41
CSE 4701 Chapter 6-41 n Definition l LEFT OUTER JOIN: R 1 F R 2 lets every Tuple in R 1 Appear in the result. l RIGHT OUTER JOIN: R 1 F R 2 lets every Tuple in R 2 Appear in the result. l FULL OUTER JOIN: R 1 F R 2 lets every Tuple in R 1 or R 2 Appear in the result n Examples - See Textbook Chapter 7 Outer Joins
42
CSE 4701 Chapter 6-42 n Commonly Used Aggregate Functions: l SUM, COUNT, AVERAGE, MIN, MAX l They Often Applied to sets of Values or sets of Tuples in Database Applications [ ] f (R) l The Grouping Attributes are Optional Aggregate Functions
43
CSE 4701 Chapter 6-43 n Example 1: l Retrieve the average salary of all employees (no grouping): R(AVG_SAL) = AVG(SALARY) (EMPLOYEE) n Example 2: l For each department, retrieve the department number, the number of employees, and the average salary (in the department): R(D#,Num_E,AVG_SAL) = GROUP DNO ( DNO, COUNT(ENO), AVG(SALARY) (EMPLOYEE)) l DNO is called the grouping attribute in this example Examples of Aggregate Functions
44
CSE 4701 Chapter 6-44 Concluding Remarks n What have we Seen in Chapter 7? l Basic Concepts of Relational Model Including Relation/Table, Tuple/Row, Attribute/Column, Domain/Attribute Value l Concept of SK, CK, PK, and FK for Identification and Referential Integrity l Integrity Constraints as they Relate to Referential Dependencies Check for Modification Operations l Relational Algebra Operations that Allow Querying via Selection, Project, Join, and Other Set Operations n Overall, Relational Theory is Basis for SQL, Normal Forms, ER-Relational Translation, etc.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.