relational algebra - basic operations cs263 lecture 11
Post on 20-Dec-2015
222 views
TRANSCRIPT
Relational Algebra - Basic OperationsCS263 Lecture 11
Relational Algebra Relational algebra operations work on one or more relations to define another
relation leaving the original intact.
Both operands and results are relations, so output from one operation can become input to another operation.
Allows expressions to be nested, just as in arithmetic. This property is called closure.
5 basic operations in relational algebra: Selection, Projection, Cartesian product, Union, and Set Difference.
These perform most of the data retrieval operations needed.
Also have Join, Intersection, and Division operations, which can be expressed in terms of 5 basic operations.
Relational Algebra Operations
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)
Projectioncol1, . . . , 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)
UnionR 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)
staffNo dobSL10 14-02-64SA51 21-11-82DS40 01-01-40
staffNo dobCC15 11-03-66SA51 21-11-82
staffNo dobSL10 14-02-64SA51 21-11-82DS40 01-01-40CC15 11-03-66
Staff_DepA Staff_DepB
Intersect
R SDefines 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))
staffNo dobSL10 14-02-64SA51 21-11-82DS40 01-01-40
staffNo dobCC15 11-03-66SA51 21-11-82
staffNo dobSA51 21-11-82
Staff_DepA Staff_DepB
Set DifferenceR – S
Defines a relation consisting of the tuples that are in relation R, but not in S. R and S must be union-compatible.
staffNo dobSL10 14-02-64SA51 21-11-82DS40 01-01-40
staffNo dobCC15 11-03-66SA51 21-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)
staffNo dobSL10 14-02-64DS40 01-01-40
Staff_DepA Staff_DepB
Cartesian productR 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 deptSL10 Salesman 10SA51 Manager 20DS40 Clerk 20
dept name 10 Stratford20 Barking
XstaffNo job dept dept nameSL10 Salesman 10 10 StratfordSA51 Manager 20 10 StratfordDS40 Clerk 20 10 StratfordSL10 Salesman 10 20 BarkingSA51 Manager 20 20 BarkingDS40 Clerk 20 20 Barking
Staff Dept
Relational Algebra Operations
JoinR S
Defines a relation that results from a selection operation (with a join predicate) over the Cartesian product of relation R and relation S.
<join condition>
<join condition>
Example: Produce a list of staff and the departments they work in.
( staffNo, job, dept (Staff)) ( dept, name (Dept))
staffNo job deptSL10 Salesman 10SA51 Manager 20DS40 Clerk 20
dept name 10 Stratford20 Barking
staffNo job dept dept nameSL10 Salesman 10 10 StratfordSA51 Manager 20 20 BarkingDS40 Clerk 20 20 Barking
Staff Dept
Staff.dept = Dept.dept
Because the predicate operator is an ‘=‘ this is known as an Equijoin
Natural JoinR 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 deptSL10 Salesman 10SA51 Manager 20DS40 Clerk 20
dept name 10 Stratford20 Barking
staffNo job dept nameSL10 Salesman 10 StratfordSA51 Manager 20 BarkingDS40 Clerk 20 Barking
Staff Dept
Left Outer JoinR 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 Salesman20 Barking SA51 Manager20 Barking DS40 Clerk30 Watford
Example: Produce a list of all departments and associated staff that work in them.
staffNo job deptSL10 Salesman 10SA51 Manager 20DS40 Clerk 20
Staffdept name 10 Stratford20 Barking30 Watford
Dept
Intersect
R SDefines 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))
staffNo dobSL10 14-02-64SA51 21-11-82DS40 01-01-40
staffNo dobCC15 11-03-66SA51 21-11-82
staffNo dobSA51 21-11-82
Staff_DepA Staff_DepB
Division
R SDefines 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 languageCOBOLBASIC
staffNo languageSL10 COBOLSA51 BASICSA51 COBOLSE14 BASICSE18 BASIC
staffNoSA51
Staff_Prog Prog