Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Operations in Relational Algebra & SQL

Similar presentations


Presentation on theme: "Binary Operations in Relational Algebra & SQL"— Presentation transcript:

1 Binary Operations in Relational Algebra & SQL

2 Set Based: UNION, INTERSECTION, DIFFERENCE

3 UNION operation Example SQL for previous example Fig 6.4:
Suppose names of people are distinct A RESULT=STUDENT INSTRUCTOR B SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) UNION (SELECT Fname, Lname FROM INSTRUCTOR);

4 Union Compatibility Requirement for the traditional set operators
Strong requirement Same number of columns Each corresponding column is compatible Positional correspondence How are rows compared? - Chapter 3 material: present again for review if desired - Can instead present material in Chapter 4 and skip when initially covering chapter 3 - Join: compares rows on the join column(s) - Traditional set operators compare on all columns Strong requirement: - Usually on identical tables (geographically dispersed tables) - Compatible columns: data types are comparable (numbers cannot be compared to strings) - Positional: 1st column of table A to 1st column of table B, 2nd column etc Can be applied to similar tables (faculty and student) by removing columns before traditional set operator

5 INTERSECTION operation
Example Suppose names of people are distinct A B RESULT=STUDENT INSTRUCTOR SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) INTERSECT (SELECT Fname, Lname FROM INSTRUCTOR);

6 SET DIFFERENCE operation
Example STUDENT - INSTRUCTOR INSTRUCTOR - STUDENT Suppose names of people are distinct (d) RESULT=INSTRUCTOR - STUDENT A B (e) RESULT=STUDENT - INSTRUCTOR B SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) MINUS (SELECT Fname, Lname FROM INSTRUCTOR);

7 CARTESIAN PRODUCT operation
Example B Relational Algebra: RESULT= Faculty ✕ Student SQL: SELECT * FROM Faculty, Student;

8 EQUI-Join EQUI-Join EQUI-Join Example: RESULT= Faculty (Faculty.FacSSM=Offering.FacSSN)Offering; SELECT * FROM Faculty, Offering WHERE Faculty.FacSSN=Offering.FacSSN;

9 Exercise 1 for Equi-Join
SQL query Result T (T1.P=T2.A)T2 T (T1.Q=T2.B)T2

10 NATURAL-Join Example: RESULT= Faculty * Offering; SELECT *
FROM EMPLOYEE NATURAL JOIN DEPARTMENT

11 THETA Join Example: RESULT=Car {CarPrice>BoatPrice} Boat;
Result=R1 {Condition} R2; Condition: {<, >, =, ≤, ≥, ≠}; EquiJoin when “=“. SELECT * FROM Car, Boat WHERE CarPrice>BoatPrice;

12 THETA Join Example: SELECT * FROM Faculty, Offering
EQUI-Join Example: SELECT * FROM Faculty, Offering WHERE Faculty.FacSSM=Offering.FacSSN;

13 Exercise 2 Department Student Faculty
Dno Dname DHeadSsn Location SID Sname Dno SAge Student FSsn Fname Dno FAge Faculty Write Relational Algebra and SQL queries for following questions: What are the names of students who are from department ‘Computer Science’? What are the names of faculties who are younger than a student? What are the names of faculties who works in ‘Keller Hall’?

14 Summary Binary Operation Operation from Set Theory Join Operation
UNION INTERSECTION DIFFERENCE CARTESIAN PRODUCT Join Operation Equi-Join Natural Join Theta Join

15 Reference Materials in the slides are from Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley and Michael V. Mannino, Database: Design, Application Development & Administration, Third Edition, McGraw Hill


Download ppt "Binary Operations in Relational Algebra & SQL"

Similar presentations


Ads by Google