Chapter Five Relational Algebra Relational Calculus Objectives Fundamental operations in RA Union Set difference Select Project Cartesian Product Relational Calculus Query Languages Procedural 1. Relational Algebra (RA) Non-Procedural 2. Tuple Relational Calculus Domain Relational Calculus 2 Fundamental Operations in RA Binary Operations UNION MINUS CARTESIAN PRODUCT Unary Operations SELECT PROJECT 3 Union of R S 1. Union of R S a. b. All tuples in R or S Union Compatible Some degree Attributes of R&S must be the same 4 Union of R S Faculty Name ID Salary Smith 1 Name ID Salary 70,000 Smith 1 70,000 Nelson 3 85,000 Anderson 3 45,000 Larson 6 62,000 Faculty U Staff Staff Name ID Salary Smith 1 70,000 Nelson 3 85,000 Larson 6 62,000 Anderson 3 45,000 5 Union of R S name (faculty) name (staff) 6 Set Difference (MINUS) R - S Set of tuples in R but not in S Union Compatible Faculty - Staff Name ID Salary Nelson 3 85,000 Larson 6 62,000 7 Intersection: R S Set of of tuples belong to both R & S Union Compatible Find the list of faculty members who are also staff Faculty Staff Name ID Salary Smith 1 70,000 R S = R – (R – S) 8 Cartesian Product R x S Set of (K1 + K2) tuples: The first K1 tuples are from R. The last K2 tuples are from S Semester S_Num Year Season 1 99 F 2 99 S 3 00 F Semester_Course C_Num S_Num 200 1 250 1 300 2 9 Cartesian Product R x S RxS S_Num Year Season C_Num S_Num 1 99 F 200 1 1 99 F 250 1 1 99 F 300 2 2 99 S 200 1 2 99 S 250 1 2 99 S 300 2 3 00 F 200 1 3 00 F 250 1 3 00 F 300 2 List of courses offered in year 99? 10 Selection (Unary Relation) Select tuples that satisfy a given predicate major = ‘COSC’ (Student) Result is another relation Conditions are relational operator (,, , , ) Logical operators AND (), OR(), NOT() 11 Selection (Unary Relation) Find all Faculty members which make less than $45,000 salary < 45,000(Faculty) Find all staff who make less than $40,000 and ID > 100 salary < 40,000 AND ID > 100 (Staff) List of number of courses offered in year 99 S_Num = S_Num AND year = 99 (Semester x Semester_Course) 12 Projection (Unary) Select attributes (Pi) Find the number of faculty that teach COSC courses name( course=‘COSC’(Faculty)) 13 Theta Join R R ij S Theta join allows us to combine the selection & the cartesian product into an operation A B a b B c b c R R BD S A B C D E a b a b c a b a b d a c a c d b c a c d C D E a b c a b d b e c a c d If is = It is called Equijoin If the attributes have the same name, the join is called Natural Join 14 Part Two Relational Calculus (RC) Non-procedural Most commercial query language Types: Tuple RC: Variables represent tuples Domain RC: Variables represent values of domain 15 Tuple Relational Calculus: { t | P(t)} Examples List of students with GPA > 3 { t | t Є student ^ t[GPA] > 3 } List of students name with GPA > 3 { t | s Є student (t[NAME] = s[NAME] ^ t[GPA] > 3)} 16