COMP 244 DATABASE CONCEPTS AND APPLICATIONS Relational Algebra And Calculus 1
Relational Algebra A formal query language associated with the relational model. Queries in ALGEBRA are composed using a collection of operators. 2
Selection in Relational Algebra A selection is an operation that selects a subset of the rows of a relation based on certain criteria. The σ (Sigma) operator is used as a selection operator. The σ operator uses a selection condition to specify the tuples of a relation to retain. 3
Selection in Relational Algebra Selection conditions are Boolean expressions that use logical connectives ^ (AND) and v (OR) Each ^ and v joins terms of the following forms: (attribute op constant) (attribute1 op attribute2) op is one of the comparison operators (<, <=, =,, >=, >) 4
Selection in Relational Algebra Example: Display all students whose gpa below 3.0 σ gpa < 3.0 (Students) Students sid 12345 32717 name Khalsa Ahmed age 22 21 gpa 2.9 3.2 sid 12345 77883 name Khalsa Muna age 22 22 gpa 2.9 2.5 77883 Muna 22 2.5 54900 Fatma 20 3.7 21827 Rashid 19 3.3 5
Selection Using SQL Example: Display all students whose gpa below 3.0 SELECT * FROM Students WHERE gpa < 3.0 Students sid name age gpa sid name age gpa 12345 Khalsa 22 2.9 12345 Khalsa 22 2.9 32717 Ahmed 21 3.2 77883 Muna 22 2.5 77883 Muna 22 2.5 54900 Fatma 20 3.7 21827 Rashid 19 3.3 6
Projection in Relational Algebra A projection is an operation that selects a subset of the attributes of a relation. The π (Phi) operator is used as a projection operator. 7
Projection in Relational Algebra Example: Display the name and age of all students π name, age (Students) Students sid name age gpa name age 12345 Khalsa 22 2.9 Khalsa 22 32717 Ahmed 21 3.2 Ahmed 21 77883 Muna 22 2.5 Muna 22 54900 Fatma 20 3.7 Fatma 20 21827 Rashid 19 3.3 Rashid 19 8
Projection in Relational Algebra Projection eliminates duplicate tuples Example: Display the age of all students π age (Students) Students sid name age gpa age 12345 Khalsa 22 2.9 22 32717 Ahmed 21 3.2 21 77883 Muna 22 2.5 20 54900 Fatma 20 3.7 19 21827 Rashid 19 3.3 9
Projection Using SQL Example: Display the name and age of all students SELECT FROM Students name, gpa Students sid name age gpa name gpa 12345 Khalsa 22 2.9 Khalsa 2.9 32717 Ahmed 21 3.2 Ahmed 3.2 77883 Muna 22 2.5 Muna 2.5 54900 Fatma 20 3.7 Fatma 3.7 21827 Rashid 19 3.3 Rashid 3.3 10
Combining Selection and Projection Since selection and projection in relational algebra results in a relation, we can form expressions containing any combination of them by substituting any relation name with another selection or projection 11
Combining Selection and Projection Example: Display the name and age of all students whose gpa >= 3.0 π name, age (σ gpa >= 3.0 (Students)) Using SQL: SELECT name, gpa FROM Students WHERE gpa >= 3.0 12
Sequence of Operations GPA_R σ gpa >= 3.0 (Students) Result π name, age (GPA_R) 13
Students sid name age gpa 12345 Khalsa 22 2.9 32717 Ahmed 21 3.2 name age 77883 Muna 22 2.5 Ahmed 21 54900 Fatma 20 3.7 Fatma 20 21827 Rashid 19 3.3 Rashid 19 σ gpa >= 3.0 (Students) sid 32717 54900 name Ahmed Fatma age 21 20 gpa 3.2 3.7 π name, age (σ gpa >= 3.0 (Students)) 21827 Rashid 19 3.3 14
Set Operations Set operations, such as union ( ), intersection ( ), difference ( ), and product (x) are also available in relational algebra 15
Unions Union: R S returns a new relation containing all tuples that are in relation R or relation S or both R and S must be union-compatible They have the same number of fields Corresponding fields, taken from left to right, have the same domains Field names are not important for unioncompatibility The schema of the result will be identical to the schema of R Note: Duplicate tuples will be removed 16
Union Compatibility STUDENT StudentID Name Phone 150 Yousif 369 270 Khalid 741 300 Arwa 210 Union Compatible ADVISOR AdvisorID Name Phone 150 Yousif 369 387 Khalid 777 COURSE Union Incompatible Course- Number Title Credit- Hours COMP2101 Introduction to Computer Science 4 COMP2103 Introduction to Object Orientation 3 COMP4414 Database Management 3 17
Unions in Relational Algebra Union: R S Students StudentID Name Phone 150 Yousif 369 270 Khalid 741 300 Arwa 210 Union of Students and Advisors StudentID OR Name Phone AdvisorID 150 Yousif 369 270 Khalid 741 Advisors AdvisorID Name Phone 300 Arwa 210 387 Khalid 777 150 Yousif 369 387 Khalid 777 18
Union Operation in SQL SQL supports the Union operation using either the OR operator or the UNION operator Example: to represent Union: R shown in the previous slide: S SELECT StudentID, name, phone FROM Students UNION SELECT StudentID, name, phone FROM Students ; 19
Union Operation in SQL Example: Find the student name and course number of all students who are enrolled in either COMP151 or COMP244: SELECT FROM WHERE AND S.name, E.cid AS course Students S, Enrollments E S.sid = E.sid (E.cid = COMP151 OR E.cid = COMP244 ) ; 20
Union Operation in SQL Another solution: SELECT FROM WHERE UNION SELECT FROM WHERE S.name, E.cid AS course Students S, Enrollments E S.sid = E.sid AND E.cid = COMP151 S.name, E.cid AS course Students S, Enrollments E S.sid = E.sid AND E.cid = COMP244 ; 21
Intersection: R S returns a relation containing all tuples that are in both R and S Intersection R and S must be union-compatible The schema of the result will be identical to the schema of R 22
Intersection Intersection: R S Students StudentID Name Phone 150 Yousif 369 270 Khalid 741 300 Arwa 210 Advisors AdvisorID Name Phone Intersection of Students and Advisors StudentID OR Name Phone AdvisorID 150 Yousif 369 150 Yousif 369 387 Khalid 777 23
Intersection Operation in SQL SQL supports the Intersection operation using the AND or the INTERSECTION operators 24
Intersection Operation in SQL Example: Find the name of all students who are enrolled in both COMP151 and COMP244: SELECT FROM WHERE S.name, E.cid AS course Students S, Enrollments E S.sid = E.sid AND (E.cid = COMP151 AND E.cid = COMP244 ); The above SELECT will ALWAYS return an empty answer 25
Intersection Operation in SQL The correct solution: SELECT S.name FROM Students S, Enrollments E1, Enrollments E2 WHERE (S.sid = E1.sid AND E1.cid = COMP151 ) AND (S.sid = E2.sid AND E2.cid = COMP244 ) 26
Intersection Operation in SQL Another solution: SELECT FROM WHERE INTERSECT SELECT FROM WHERE S1.name Students S1, Enrollments E1 S1.sid = E1.sid AND E1.cid = COMP150 S2.name Students S2, Enrollments E2 S2.sid = E2.sid AND E2.cid = COMP224 ; 27
Set Operations Set-Difference: R S returns a relation containing all tuples that are in R but NOT in S R and S must be union-compatible The schema of the result will be identical to the schema of R 28
Set Operations Set-Difference: R S Students StudentID Name Phone 150 Yousif 369 270 Khalid 741 300 Arwa 210 Difference of Students and Advisors StudentID OR Name Phone AdvisorID 270 Khalid 741 300 Arwa 210 Advisors AdvisorID Name Phone 150 Yousif 369 387 Khalid 777 29
Cross-Product: R x S returns a relation containing All fields of R (in the same order as they appear in R), followed by All fields of S (in the same order as they appear in S), followed by A combination of the tuples of the tuples (r, s) for each pair of tuples r R, s S Set Operations 30
Set Operations Cross-Product: R x S The cross-product operation is sometimes called Cartesian product Since R and S could contain fields with the same name, the corresponding fields in R x S are unnamed. They are referred to by position 31
Set Operations Cross-Product: R x S Relations A and B do not need to be union compatible The resulting relation might contain meaningless information A X B is the same as B X A 32
Cross-Product Students StudentID Name Phone 150 Yousif 369 270 Khalid 741 300 Arwa 210 Grades Student- Number Course- Number Sem Grade 150 COMP2101 F02 B 387 COMP4414 S03 A Product of Students and Grades StudentID Name Phone Student- Number Course- Number Sem Grade 150 Yousif 369 150 COMP2101 F02 B 150 Yousif 369 387 COMP4414 S03 A 270 Khalid 741 150 COMP2101 F02 B 270 Khalid 741 387 COMP4414 S03 A 300 Arwa 210 150 COMP2101 F02 B 300 Arwa 210 387 COMP4414 S03 A 33
Relational Calculus Relational Calculus allows us to describe how data are retrieved from a database. Relational Calculus is used in many commercial query languages, such as SQL, and Query-by-Example (QBE). We will use a variant of the calculus called the Tuple Relational Calculus (TRC). TRC has influence on SQL There is another calculus variant called Domain Relational Calculus (DRC). DRC has influence on QBE 34
Syntax of TRC Queries We need the following definitions: Rel is a relation name R and S are tuple variables op is an operator in the set { <, >, =, <=, >=, } 35
Syntax of TRC Queries We define an atomic formula to take any of the following forms: R Rel R.a op S.b R.a op constant, or constant op R.a 36
Syntax of TRC Queries A TRC formula can be any of the following forms: Any atomic formula p, R ( R ( p q, p( R)), p( R)), p q, or p q where Ris atuple where Ris atuple var var iable iable 37
Syntax of TRC Queries A TRC Query is an expression in the following form: { T p(t) } T is a tuple variable p(t) is a formula 38
Syntax of TRC Queries Example: Find all students who are enrolled in COMP244: { P S Students, E Enrollments ( S. sid E. sid ^ cid ' COMP244')} 39
Syntax of DRC Queries DRC queries have similar syntax to that of TRC. Example: Find the name and gpa of all students who are enrolled in COMP244: {( N, G) S, A(( S, N, A, G) Students ^ ( Se, Ce, Ge) Enrollments ( S Se ^Ce ' COMP244'))} 40
Syntax of DRC Queries Example: Find all students who are enrolled in COMP244: { P S Students, E Enrollments ( S. sid E. sid ^ cid ' COMP244')} 41