320 likes | 428 Views
Relational algebra. Relational algebra Notation. Unary Operations. Projection stud#, name Students In SQL: Select stud#, name From Students;. Selection course = ‘Computing’ Students In SQL: Select * From Students Where course = ‘Computing’ ;. Selection & Projection
E N D
Relational algebra SHIRAJ MOHAMED M | MIS
Relational algebra Notation SHIRAJ MOHAMED M | MIS
Unary Operations Projection stud#, nameStudents In SQL: Select stud#, name From Students; Selection course = ‘Computing’Students In SQL: Select * From Students Where course = ‘Computing’; SHIRAJ MOHAMED M | MIS Selection & Projection stud#, name(course = ‘Computing’Students) In SQL: Select stud#, name From students Where course = ‘Computing’;
Binary Operations/Joins Cartesian Product: Students X Courses In SQL: Select * From Students, Courses; SHIRAJ MOHAMED M | MIS
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
Renaming Example SHIRAJ MOHAMED M | MIS • TEMP DNO=5(EMPLOYEE) • R(FIRSTNAME, LASTNAME, SALARY) • FNAME, LNAME, SALARY (TEMP)
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. SHIRAJ MOHAMED M | MIS
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] SHIRAJ MOHAMED M | MIS
Set Operators • Name (FACULY) Name (STUDENT) • Address (FACULY) Address (STUDENT) • CrsCode (CLASS) - CrsCode (TRANSCRIPT) SHIRAJ MOHAMED M | MIS
Union S1 S2 SHIRAJ MOHAMED M | MIS
Set Difference S1 S2 – S1 S2 SHIRAJ MOHAMED M | MIS
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
Join… Students ⋈ <stud# =200> Courses In SQL: Select * From Students, Courses Where stud# = 200; SHIRAJ MOHAMED M | MIS
Left Outer Join Left Outer Join : A <join condition> 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
Left Outer Join - Example StudentsCourses stud# namecoursecourse# name 100Fred PH PH Pharmacy 200 Dave CM CM Computing 400 Peter EN CH Chemistry Students<course = course#>Courses stud# Students.name course course# Courses.name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing • Peter EN NULL NULL SHIRAJ MOHAMED M | MIS
Right Outer Join Right Outer Join: A <join condition> 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
Right Outer Join - Example StudentsCourses stud# namecoursecourse# name 100Fred PH PH Pharmacy 200 Dave CM CM Computing 400 Peter EN CH Chemistry Students<course = course#>Courses stud# Students.name course course# Courses.name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing NULL NULL NULL CH Chemistry SHIRAJ MOHAMED M | MIS
Combination of Unary and Join Operations StudentsCourses stud# name address course course# name 100Fred Aberdeen PH PH Pharmacy 200 Dave Dundee CM CM Computing 300 Bob Aberdeen CM Show the names of students (from Aberdeen) and the names of their courses R1= Students ⋈<course=course#> Courses R2= <address=“Aberdeen”> R1 R3= <Students.name, Course.name> R2 SHIRAJ MOHAMED M | MIS Students.name Courses.name Fred Pharmacy Bob Computing
Full Outer Join Full Outer Join: A <join condition> B • ensures that all tuples of A and B are present in the result set SHIRAJ MOHAMED M | MIS
Exercise 1 Example: Customer • 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. SHIRAJ MOHAMED M | MIS
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
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
Solution 2: 1.Find names of sailors who’ve reserved boat #103 • Solution 1: SHIRAJ MOHAMED M | MIS
A more efficient (???) solution: 2.Find names of sailors who’ve reserved a red boat • Information about boat color only available in Boats; so need an extra join: SHIRAJ MOHAMED M | MIS
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
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
5. Find the names of sailors who’ve reserved all boats • Uses division; schemas of the input relations to / must be carefully chosen: SHIRAJ MOHAMED M | MIS • To find sailors who’ve reserved all ‘Interlake’ boats: .....
Aggregate Functions and Operations • Aggregation function takes a collection of values and returns a single value as a result. avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values • Aggregate operation in relational algebra G1, G2, …, GngF1( A1), F2( A2),…, Fn( An) (E) • E is any relational-algebra expression • G1, G2 …, Gn is a list of attributes on which to group (can be empty) • Each Fiis an aggregate function • Each Aiis an attribute name
Aggregate Operation – Example • Relation account grouped by branch-name: branch-name account-number balance Perryridge Perryridge Brighton Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 750 700 branch-nameg sum(balance) (account) branch-name balance Perryridge Brighton Redwood 1300 1500 700
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-nameg sum(balance) as sum-balance (account)
End… SHIRAJ MOHAMED M | MIS