Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra - Basic Operations CS263 Lecture 11.

Similar presentations


Presentation on theme: "Relational Algebra - Basic Operations CS263 Lecture 11."— Presentation transcript:

1 Relational Algebra - Basic Operations CS263 Lecture 11

2 Relational Algebra v Relational algebra operations work on one or more relations to define another relation leaving the original intact. v Both operands and results are relations, so output from one operation can become input to another operation. v Allows expressions to be nested, just as in arithmetic. This property is called closure. v 5 basic operations in relational algebra: Selection, Projection, Cartesian product, Union, and Set Difference. v These perform most of the data retrieval operations needed. v Also have Join, Intersection, and Division operations, which can be expressed in terms of 5 basic operations.

3 Relational Algebra Operations

4 Selection (Restriction)  predicate (R) Works on a single relation R and defines a relation that contains only those tuples of R that satisfy the specified condition (predicate). Example: List all staff with a salary greater than £10,000.  salary > 10000 (Staff) 

5 Projection  col1,..., coln (R) Works on a single relation R and defines a relation that contains a vertical subset of R, extracting the values of specified attributes and eliminating duplicates. Example: Produce a list of salaries for all staff, showing only their staffNo, fName, lName, and salary details.  staffNo, fName, lName, salary (Staff) 

6 Union R  S Union of two relations R and S defines a relation that contains all the tuples of R, or S, or both R and S, duplicate tuples being eliminated. R and S must be union-compatible (i.e. same attributes).  Example: Produce a list of all staff that work in either of two departments (each department has a separate database), showing only their staffNo, and date of birth.  staffNo, dob (Staff_DepA)  staffNo, dob (Staff_DepB)  staffNodob SL1014-02-64 SA5121-11-82 DS4001-01-40 staffNodob CC1511-03-66 SA5121-11-82 staffNodob SL1014-02-64 SA5121-11-82 DS4001-01-40 CC1511-03-66  Staff_DepA Staff_DepB

7 Intersect R  S Defines a relation consisting of the set of all tuples that are in both R and S. R and S must be union-compatible.  Example: Produce a list of staff that work in both department A and department B, showing only their staffNo, and date of birth. (  staffNo, dob (Staff_DepA))  (  staffNo, dob (Staff_DepB)) staffNodob SL1014-02-64 SA5121-11-82 DS4001-01-40 staffNodob CC1511-03-66 SA5121-11-82 staffNodob SA5121-11-82 Staff_DepA Staff_DepB 

8 Set Difference R – S Defines a relation consisting of the tuples that are in relation R, but not in S. R and S must be union-compatible. staffNodob SL1014-02-64 SA5121-11-82 DS4001-01-40 staffNodob CC1511-03-66 SA5121-11-82 Example: Produce a list of all staff that only work in department A (each department has a separate database), showing only their staffNo, and date of birth.  staffNo, dob (Staff_DepA)  staffNo, dob (Staff_DepB) staffNodob SL1014-02-64 DS4001-01-40 Staff_DepA Staff_DepB

9 Cartesian product R X S Defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S. X Example: Combine details of staff and the departments they work in.  staffNo, job, dept (Staff)  dept, name (Dept) X staffNo job dept SL10 Salesman 10 SA51 Manager 20 DS40 Clerk 20 dept name 10 Stratford 20 Barking X staffNo job dept dept name SL10 Salesman 10 10 Stratford SA51 Manager 20 10 Stratford DS40 Clerk 20 10 Stratford SL10 Salesman 10 20 Barking SA51 Manager 20 20 Barking DS40 Clerk 20 20 Barking Staff Dept

10 Relational Algebra Operations

11 Join R S Defines a relation that results from a selection operation (with a join predicate) over the Cartesian product of relation R and relation S. Example: Produce a list of staff and the departments they work in. (  staffNo, job, dept (Staff)) (  dept, name (Dept)) staffNo job dept SL10 Salesman 10 SA51 Manager 20 DS40 Clerk 20 dept name 10 Stratford 20 Barking staffNo job dept dept name SL10 Salesman 10 10 Stratford SA51 Manager 20 20 Barking DS40 Clerk 20 20 Barking Staff Dept Staff.dept = Dept.dept Because the predicate operator is an ‘=‘ this is known as an Equijoin

12 Natural Join R S This performs an Equijoin of the two relations R and S over all common attributes. One occurrence of each common attribute is eliminated from the result. Example: Produce a list of staff and the departments they work in. (  staffNo, job, dept (Staff)) (  dept, name (Dept)) staffNo job dept SL10 Salesman 10 SA51 Manager 20 DS40 Clerk 20 dept name 10 Stratford 20 Barking staffNo job dept name SL10 Salesman 10 Stratford SA51 Manager 20 Barking DS40 Clerk 20 Barking Staff Dept

13 Left Outer Join R S Left outer join is a join in which tuples from R that do not have matching values in common columns of S are also included in the resulting relation. (  dept, name (Dept)) (  staffNo, job, dept (Staff)) dept name staffNo job 10 Stratford SL10 Salesman 20 Barking SA51 Manager 20 Barking DS40 Clerk 30 Watford Example: Produce a list of all departments and associated staff that work in them. staffNo job dept SL10 Salesman 10 SA51 Manager 20 DS40 Clerk 20 Staff dept name 10 Stratford 20 Barking 30 Watford Dept

14 Intersect R  S Defines a relation consisting of the set of all tuples that are in both R and S. R and S must be union-compatible.  Example: Produce a list of staff that work in both department A and department B, showing only their staffNo, and date of birth. (  staffNo, dob (Staff_DepA))  (  staffNo, dob (Staff_DepB)) staffNodob SL1014-02-64 SA5121-11-82 DS4001-01-40 staffNodob CC1511-03-66 SA5121-11-82 staffNodob SA5121-11-82 Staff_DepA Staff_DepB 

15 Division R S Defines a relation over common attributes C that consists of set of tuples from R that match a combination of every tuple in S.   Example: Show all staff that use all the company’s programming languages. Staff_Prog Prog  language COBOL BASIC staffNolanguage SL10COBOL SA51BASIC SA51COBOL SE14BASIC SE18BASIC staffNo SA51 Staff_Prog Prog 


Download ppt "Relational Algebra - Basic Operations CS263 Lecture 11."

Similar presentations


Ads by Google