CPSC471 Database Management Systems NOTES on Relational Algebra N.B. : Duplicate tuples are eliminated by the project ( P ) operation. CARTESIAN PRODUCT ( X ) vs JOIN ( X ) Given two relations: R1(A1, A2, ..., Am) and R2 (B1, B2, ..., Bn) R1 X R2 returns a new relation say R(A1, A2, ..., Am, B1, B2, ..., Bn) Example: Student Department Sid 20 35 48 51 59 60 Name Jim Susan Diana John Lara David dept CS EE CS CS EE EE Student X Department Sid Name 20 Jim 35 Susan 48 Diana 51 John 59 Lara 60 David dept CS EE CS CS EE EE Dname CS CS CS CS CS CS Head Eddy Eddy Eddy Eddy Eddy Eddy location CSB CSB CSB CSB CSB CSB CS EE CS CS EE EE EE EE EE EE EE EE Adam Adam Adam Adam Adam Adam EEB EEB EEB EEB EEB EEB 20 35 48 51 59 60 s--- (dept=dname) Jim Susan Diana John Lara David Dname Head location CS Eddy CSB EE Adam EEB (Student X Department) Student Sid 20 48 51 35 59 60 Name Jim Diana John Susan Lara David dept CS CS CS EE EE EE "! (dept=dname) Department Dname Head location (dept=department) CS Eddy CSB CS Eddy CSB CS Eddy CSB EE Adam EEB EE Adam EEB EE Adam EEB So, JOIN is equivalent to CROSS-PRODUCT followed by SELECT. CPSC471 Database Management Systems Division: ( ÷) (done using a combination of -, X, P, s-- ) Example: Project Pno 20 20 30 35 35 35 Part P# 2 4 1 1 2 4 P# 1 2 4 weight 150 130 95 Color red green blue Query: Find projects using all parts. The answer in simply: Project ÷ (PP# Part ) Pno 35 How does it work: 1. Find all Projects and all Parts PPno Project PP# Part Pno P# 20 1 30 2 35 4 2. Find what should have been the relationship between projects and parts if each project is using every part. This is the cross-product of the results in 1. That is, assume every project is using all the parts. (PPno Project ) X (PP# Part) Pno 20 20 20 30 30 30 35 35 35 P# 1 2 4 1 2 4 1 2 4 CPSC471 Database Management Systems 3. Find tuples in 2, which are not part of the result; that is, we need to find from 2 tuples that have been assumed but they are not reflecting the real case. These are projects that do not use all parts. ((PPno Project ) X (PP# Part)) - Project Pno 20 30 30 P# 1 2 4 4. Subtract projects in 3 from the existing projects to get projects using all the parts. (PPno Project) - (PPno(((PPno Project ) X (PP# Part)) - Project) Pno 20 30 35 Pno 20 30 = Result Pno 35 Aggregate Functions: ( Á) : to execute a certain aggregate function on tuples of a relation after the tuples are categorized (classified) by the values of certain attributes. Group-by attributes Á function list (relation) Example: Student Sid 20 35 48 51 59 60 Name Jim Susan Diana John Lara David dept CS EE CS CS EE EE Query: Find the number of students in each department dept Á count sid (Student) dept count CS 3 EE 3