Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational algebra SHIRAJ MOHAMED M | MIS 1. Relational algebra Notation SHIRAJ MOHAMED M | MIS 2.

Similar presentations


Presentation on theme: "Relational algebra SHIRAJ MOHAMED M | MIS 1. Relational algebra Notation SHIRAJ MOHAMED M | MIS 2."— Presentation transcript:

1 Relational algebra SHIRAJ MOHAMED M | MIS 1

2 Relational algebra Notation SHIRAJ MOHAMED M | MIS 2

3 Unary Operations Selection  course = ‘Computing’ Students In SQL: Select * From Students Where course = ‘Computing’; Projection  stud#, name Students In SQL: Select stud#, name From Students; Selection & Projection  stud#, name (  course = ‘Computing’ Students) In SQL: Select stud#, name From students Where course = ‘Computing’; 3 SHIRAJ MOHAMED M | MIS

4 Binary Operations/Joins Cartesian Product: Students X Courses In SQL: Select * From Students, Courses; 4 SHIRAJ MOHAMED M | MIS

5 Rename RENAME operator (  ): Renames the input relation and attributes with a new relation name & attributes specified.  S(B1, B2, …, BN) (R) Example, TEMP   NAME, MAJOR (STUDENT)  STUD_INFO (FULL_NAME,M_DEPT )  TEMP SHIRAJ MOHAMED M | MIS 5

6 Renaming TEMP   DNO=5 (EMPLOYEE) R(FIRSTNAME, LASTNAME, SALARY)   FNAME, LNAME, SALARY (TEMP) Example SHIRAJ MOHAMED M | MIS 6

7 Union and Set-Difference All of these operations take two input relations, which must be union-compatible: Same number of fields. Corresponding’ fields have the same type. 7 SHIRAJ MOHAMED M | MIS

8 Set Operators Given two relations R1, R2 that are union-compatible, we have that ∆R1  R2 returns the set of tuples that are in R1 or R2. [UNION] ∆R1  R2 returns the set of tuples that are both in R1 and R2. [INTERSECTION] ∆R1 - R2 returns the set of tuples that are in R1, but not in R2. [SET DIFFERENCE] 8 SHIRAJ MOHAMED M | MIS

9 Set Operators   Name (FACULY)   Name (STUDENT)   Address (FACULY)   Address (STUDENT)   CrsCode (CLASS) -  CrsCode (TRANSCRIPT) 9 SHIRAJ MOHAMED M | MIS

10 Union S1 S2 10 SHIRAJ MOHAMED M | MIS

11 Set Difference S1 S2 S2 – S1 11 SHIRAJ MOHAMED M | MIS

12 Joins Three new join operators are introduced: Left Outer Join (denoted as ) Right Outer Join (denoted as ) Full Outer Join (denoted as ) SHIRAJ MOHAMED M | MIS 12

13 Join… SHIRAJ MOHAMED M | MIS 13 Students ⋈ Courses In SQL: Select * From Students, Courses Where stud# = 200;

14 Left Outer Join Left Outer Join : A B ensures that all tuples in the in the relation A are present in the result set. The tuples in A without matching tuples in B are filled with null values for B’s attributes SHIRAJ MOHAMED M | MIS 14

15 Left Outer Join - Example StudentsCourses stud#namecoursecourse#name 100FredPHPHPharmacy 200DaveCMCMComputing 400 PeterENCHChemistry Students Courses stud#Students.namecoursecourse#Courses.name 100FredPHPHPharmacy 200DaveCMCMComputing 400 PeterENNULLNULL 15 SHIRAJ MOHAMED M | MIS

16 Right Outer Join Right Outer Join: A B Reverse of left outer join. Retrieves all tuples of B and null values for attributes of A in non-matching tuples of B SHIRAJ MOHAMED M | MIS 16

17 Right Outer Join - Example StudentsCourses stud#namecoursecourse#name 100FredPHPHPharmacy 200DaveCMCMComputing 400 PeterENCHChemistry Students Courses stud#Students.namecoursecourse#Courses.name 100 FredPHPHPharmacy 200 DaveCMCMComputing NULL NULLNULLCHChemistry 17 SHIRAJ MOHAMED M | MIS

18 Combination of Unary and Join Operations StudentsCourses stud#nameaddresscoursecourse# name 100FredAberdeenPHPH Pharmacy 200DaveDundeeCMCM Computing 300BobAberdeenCM Show the names of students (from Aberdeen) and the names of their courses R1= Students ⋈ Courses R2=  R1 R3=  R2 Students.nameCourses.name FredPharmacy BobComputing 18 SHIRAJ MOHAMED M | MIS

19 Full Outer Join Full Outer Join: A B ensures that all tuples of A and B are present in the result set SHIRAJ MOHAMED M | MIS 19

20 Exercise 1 Query 1: List customers whose cred_lim is greater than £500. Query 2: List customers whose cred_lim is greater than £500 and lives in London. Example: Customer SHIRAJ MOHAMED M | MIS 20

21 Answers Query 1: List customers whose cred_lim is greater than £500.  (cred_lim > 500) (customer) Query 2: List customers whose cred_lim is greater than £500 and lives in London.  (cred_lim>500) AND (city=London) (customer) SHIRAJ MOHAMED M | MIS 21

22 Exercise 2 Reserves Sailors Boats 1.Find names of sailors who’ve reserved boat #103 2.Find names of sailors who’ve reserved a red boat 3.Find sailors who’ve reserved a red or a green boat 4.Find sailors who’ve reserved a red and a green boat 5. Find the names of sailors who’ve reserved all boats SHIRAJ MOHAMED M | MIS 22

23 1.Find names of sailors who’ve reserved boat #103 Solution 1: Solution 2: SHIRAJ MOHAMED M | MIS 23

24 2.Find names of sailors who’ve reserved a red boat Information about boat color only available in Boats; so need an extra join: v A more efficient (???) solution: SHIRAJ MOHAMED M | MIS 24

25 3.Find sailors who’ve reserved a red or a green boat Can identify all red or green boats, then find sailors who’ve reserved one of these boats: SHIRAJ MOHAMED M | MIS 25

26 4.Find sailors who’ve reserved a red and a green boat Previous approach won’t work! Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (note that sid is a key for Sailors): SHIRAJ MOHAMED M | MIS 26

27 5. Find the names of sailors who’ve reserved all boats Uses division; schemas of the input relations to / must be carefully chosen: v To find sailors who’ve reserved all ‘Interlake’ boats:..... SHIRAJ MOHAMED M | MIS 27

28 Aggregate Functions and Operations Aggregation function takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Aggregate operation in relational algebra G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E) E is any relational-algebra expression G 1, G 2 …, G n is a list of attributes on which to group (can be empty) Each F i is an aggregate function Each A i is an attribute name

29 Aggregate Operation – Example Relation account grouped by branch-name: branch-name g sum(balance) (account) branch-nameaccount-numberbalance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch-namebalance Perryridge Brighton Redwood 1300 1500 700

30 Aggregate Functions Result of aggregation does not have a name Can use rename operation to give it a name For convenience, we permit renaming as part of aggregate operation branch-name g sum(balance) as sum-balance (account)

31 End…  SHIRAJ MOHAMED M | MIS 31


Download ppt "Relational algebra SHIRAJ MOHAMED M | MIS 1. Relational algebra Notation SHIRAJ MOHAMED M | MIS 2."

Similar presentations


Ads by Google