Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Relational Algebra & SQL Lecture 5. 2 Relational Algebra The Relational Algebra is used to define the ways in which relations (tables) can be operated.

Similar presentations


Presentation on theme: "1 Relational Algebra & SQL Lecture 5. 2 Relational Algebra The Relational Algebra is used to define the ways in which relations (tables) can be operated."— Presentation transcript:

1 1 Relational Algebra & SQL Lecture 5

2 2 Relational Algebra The Relational Algebra is used to define the ways in which relations (tables) can be operated to manipulate their data. It is used as the basis of SQL for relational databases, and illustrates the basic operations required of any DML. This Algebra is composed of Unary operations (involving a single table) and Binary operations (involving multiple tables).

3 3 SQL Structured Query Language (SQL) –Standardised by ANSI –Supported by modern RDBMSs Commands fall into three groups – Data Definition Language (DLL) Create tables, etc – Data Manipulation Language (DML) Retrieve and modify data – Data Control Language Control what users can do – grant and revoke privileges

4 4 Unary Operations Selection Projection

5 5 Selection The selection or  operation selects rows from a table that satisfy a condition:  Example:  course = ‘CM’ Students Students stud#namecourse 100FredPH stud#namecourse 200DaveCM 300BobCM

6 6 Projection The projection or  operation selects a list of columns from a table.  Example:  stud#, name Students Students stud#namecoursestud#name 100FredPH 100Fred 200 DaveCM 200 Dave 300 BobCM 300 Bob

7 7 Selection / Projection Selection and Projection are usually combined:  stud#, name (  course = ‘CM’ Students) Students stud#namecourse 100FredPH stud#name 200DaveCM 200Dave 300BobCM 300Bob

8 8 Binary Operations Cartesian Product Theta Join Inner Join Natural Join Outer Joins Semi Joins

9 9 Cartesian Product Concatenation of every row in the first relation (R) with every row in the second relation (S): R X S

10 10 Cartesian Product - Example StudentsCourses stud#namecoursecourse#name 100FredPHPHPharmacy 200DaveCMCMComputing 300BobCM Students X Courses = stud#Students.namecoursecourse#Courses.name 100FredPHPHPharmacy 100FredPHCMComputing 200DaveCMPHPharmacy 200DaveCMCMComputing 300BobCMPHPharmacy 300BobCMCMComputing

11 11 Theta Join A Cartesian product with a condition applied: R ⋈ S

12 12 Theta Join - Example StudentsCourses stud#namecoursecourse#name 100FredPHPHPharmacy 200DaveCMCMComputing 300BobCM Students ⋈ stud# = 200 Courses stud#Students.namecoursecourse#Courses.name 200DaveCMPHPharmacy 200DaveCMCMComputing

13 13 Inner Join (Equijoin) A Theta join where the is the match (=) of the primary and foreign keys. R ⋈ S

14 14 Inner Join - Example StudentsCourses stud#namecoursecourse#name 100FredPHPHPharmacy 200DaveCMCMComputing 300BobCM Students ⋈ course = course# Courses stud#Students.namecoursecourse#Courses.name 100FredPHPHPharmacy 200DaveCMCMComputing 300BobCMCMComputing

15 15 Natural Join Inner join produces redundant data (in the previous example: course and course#). To get rid of this duplication:  (Students ⋈ Courses) Or R1= Students ⋈ Courses R2=  R1 The result is called the natural join of Students and Courses

16 16 Natural Join - Example StudentsCourses stud#namecoursecourse#name 100FredPHPHPharmacy 200DaveCMCMComputing 300 BobCM R1= Students ⋈ Courses R2=  R1 stud#Students.namecourseCourses.name 100FredPHPharmacy 200DaveCMComputing 300BobCMComputing

17 17 Outer Joins Inner join + rows of one table which do not satisfy the. Left Outer Join: R S All rows from R are retained and unmatched rows of S are padded with NULL Right Outer Join: R S All rows from S are retained and unmatched rows of R are padded with NULL

18 18 Left Outer Join - Example StudentsCourses stud#namecoursecourse#name 100FredPHPHPharmacy 200DaveCMCMComputing 400 PeterENCHChemistry Students Courses stud#Students.namecoursecourse#Courses.name 100FredPHPHPharmacy 200DaveCMCMComputing 400 PeterENNULLNULL

19 19 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

20 20 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

21 21 Set Operations Union Intersection Difference

22 22 Union Takes the set of rows in each table and combines them, eliminating duplicates Participating relations must be compatible, ie have the same number of columns, and the same column names, domains, and data types RSR  S AB a1b1 a2b2 AB a2b2 a3b3 AB a1b1 a2b2 a3b3

23 23 Intersection Takes the set of rows that are common to each relation Participating relations must be compatible RSR  S AB a1b1 a2b2 AB a2b2 a3b3 AB a2b2

24 24 Difference Takes the set of rows in the first relation but not the second Participating relations must be compatible RSR - S AB a1b1 a2b2 AB a2b2 a3b3 AB a1b1

25 25 Exercise (May 2004 Exam) EmployeeWorkLoadProject empidnameempid*projid*durationprojidname E100FredE100P00117P001DB E200DaveE200P00112P002Access E300BobE300P00215P003SQL E400Peter Determine the outcome of the following operations: A natural join between Employee and WorkLoad A left outer join between Employee and WorkLoad A right outer join between WorkLoad and Project

26 26 Relational Algebra Operations written in SQL

27 27 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’;

28 28 Binary Operations/Joins Cartesian Product: Students X Courses In SQL: Select * From Students, Courses; Theta Join: Students ⋈ Courses In SQL: Select * From Students, Courses Where stud# = 200;

29 29 Binary Operations/Joins Inner Join (Equijoin): Students ⋈ Courses In SQL: Select * From Students, Courses Where course=course#; Natural Join: R1= Students ⋈ Courses R2=  R1 In SQL: Select stud#, Students.name, course, Courses.name From Students, Courses Where course=course#;

30 30 Outer Joins Left Outer Join Students Courses In SQL: Select * From Students, Courses Where course = course#(+) Right Outer Join Students Courses In SQL: Select * From Students, Courses Where course(+) = course#

31 31 Combination of Unary and Join Operations R1= Students ⋈ Courses R2=  R1 R3=  R2 In SQL: Select Students.name, Courses.name From Students, Courses Where course=course# AND address=“Aberdeen”;

32 32 Set Operations Union: R  S In SQL: Select * From R Union Select * From S; Intersection: R  S In SQL: Select * From R Intersect Select * From S; Difference: R - S In SQL: Select * From R Minus Select * From S;

33 33 SQL Operators Between, In, Like, Not

34 34 SQL Operators SELECT * FROM Book WHERE catno BETWEEN 200 AND 400; SELECT * FROM Product WHERE prod_desc BETWEEN ‘C’ AND ‘S’; SELECT * FROM Book WHERE catno NOT BETWEEN 200 AND 400;

35 35 SQL Operators SELECT Catno FROM Loan WHERE Date-Returned IS NULL; SELECT Catno FROM Loan WHERE Date-Returned IS NOT NULL;

36 36 SQL Operators SELECT Name FROM Member WHERE memno IN (100, 200, 300, 400); SELECT Name FROM Member WHERE memno NOT IN (100, 200, 300, 400);

37 37 SQL Operators SELECT Name FROM Member WHERE address NOT LIKE ‘%Aberdeen%’; SELECT Name FROM Member WHERE Name LIKE ‘_ES%’; Note: In MS Access, use * and # instead of % and _

38 38 Selecting Distinct Values Student stud#nameaddress 100FredAberdeen 200DaveDundee 300BobAberdeen SELECT Distinct address FROM Student; address Aberdeen Dundee

39 39 Exercise Employee(empid, name) Project(projid, name) WorkLoad(empid*, projid*, duration) List the names of employees working on project name ‘Databases’.


Download ppt "1 Relational Algebra & SQL Lecture 5. 2 Relational Algebra The Relational Algebra is used to define the ways in which relations (tables) can be operated."

Similar presentations


Ads by Google