Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Relational Algebra and Relational Calculus.

Similar presentations


Presentation on theme: "Chapter 5 Relational Algebra and Relational Calculus."— Presentation transcript:

1 Chapter 5 Relational Algebra and Relational Calculus

2 2 Introduction u Relational algebra & relational calculus –formal languages associated with the relational model u Relational algebra – (high-level) procedural language u Relational calculus –non-procedural language. u A language that produces a relation that can be derived using relational calculus is relationally complete

3 3 Relational Algebra u Relational algebra operations work on one or more relations to define another relation without changing original relations u Both operands and results are relations –Output from one operation can be input to another u Allows nested expressions –Closure property

4 4 Relational Algebra u Five basic operations –Selection, Projection, Cartesian product, Union, and Set Difference u Perform most required data retrieval operations u Additional operators –Join, Intersection, and Division –Can be expressed in terms of 5 basic operations

5 5 Selection (or Restriction) u  predicate (R) –Works on single relation R –Defines relation that contains only tuples (rows) of R that satisfy specified condition (predicate)

6 6 Example - Selection (or Restriction) u List all staff with a salary greater than £10,000.  salary > (Staff)

7 7 Projection u  col1,..., coln (R) –Works on a single relation R –Defines relation that contains vertical subset of R –Extracts values of specified attributes –Eliminates duplicates

8 8 Example - Projection u Produce a list of salaries for all staff, showing only staffNo, fName, lName, and salary details.  staffNo, fName, lName, salary (Staff)

9 9 Union u R  S –Defines relation that contains all tuples of R, or S, or both R and S –Duplicate tuples eliminated –R and S must be union-compatible u For relations R and S with I and J tuples, respectively –Union is concatenation of R & S into one relation with maximum of (I + J) tuples

10 10 Example - Union u List all cities where there is either a branch office or a property for rent.  city (Branch)   city (PropertyForRent)

11 11 Set Difference u R – S –Defines relation consisting of tuples in relation R, but not in S –R and S must be union-compatible

12 12 Example - Set Difference u List all cities where there is a branch office but no properties for rent.  city (Branch) –  city (PropertyForRent)

13 13 Intersection u R  S –Defines relation consisting of set of all tuples in both R and S –R and S must be union-compatible  Expressed using basic operations: R  S = R – (R – S)

14 14 Example - Intersection u List all cities where there is both a branch office and at least one property for rent.  city (Branch)   city (PropertyForRent)

15 15 Cartesian product u R X S –Binary operation –Defines relation that is concatenation of every tuple of relation R with every tuple of relation S

16 16 Example - Cartesian product u List the names and comments of all clients who have viewed a property for rent. (  clientNo, fName, lName (Client)) X (  clientNo, propertyNo, comment (Viewing))

17 17 Example - Cartesian product and Selection u Use selection operation to extract those tuples where Client.clientNo = Viewing.clientNo.  Client.clientNo = Viewing.clientNo ((  clientNo, fName, lName (Client))  (  clientNo, propertyNo, comment (Viewing))) u Cartesian product and Selection can be reduced to single operation u Join

18 18 Relational Algebra Operations

19 19 Join Operations u Join - derivative of Cartesian product u Equivalent to performing Selection, using join predicate as selection formula, over Cartesian product of two operand relations u One of most difficult operations to implement efficiently in an RDBMS u One reason RDBMSs have intrinsic performance problems

20 20 Join Operations u Various forms of join operation –Theta join –Equijoin (a particular type of Theta join) –Natural join –Outer join –Semijoin

21 21 Theta join (  -join) u R F S –Defines relation that contains tuples satisfying predicate F from Cartesian product of R and S –Predicate F is of the form R.a i  S.b i where  may be comparison operators (, , =,  )

22 22 Theta join (  -join) u Can rewrite Theta join using basic Selection and Cartesian product operations R F S =  F (R  S) u Degree of a Theta join u Sum of degrees of operand relations R and S u If predicate F contains only equality (=) u Equijoin

23 23 Example - Equijoin u List the names and comments of all clients who have viewed a property for rent. (  clientNo, fName, lName (Client)) Client.clientNo = Viewing.clientNo (  clientNo, propertyNo, comment (Viewing))

24 24 Natural join u R S –Equijoin of relations R and S over all common attributes x –One occurrence of each common attribute eliminated from result

25 25 Example - Natural join u List the names and comments of all clients who have viewed a property for rent. (  clientNo, fName, lName (Client)) (  clientNo, propertyNo, comment (Viewing))

26 26 Outer join u Used to display rows in result that do not have matching values in join column u R S –(Left) outer join »Tuples from R that do not have matching values in common columns of S included in result relation

27 27 Example - Left Outer join u Produce a status report on property viewings.  propertyNo, street, city (PropertyForRent) Viewing

28 28 Semijoin u R F S –Defines relation that contains tuples of R that participate in join of R with S u Can rewrite Semijoin using Projection and Join: R F S =  A (R F S)

29 29 Example - Semijoin u List complete details of all staff who work at the branch in Glasgow. Staff Staff.branchNo=Branch.branchNo (  city=‘Glasgow’ (Branch))

30 30 Division u R  S –Defines relation over attributes C that consists of set of tuples from R that match combination of every tuple in S  Expressed using basic operations: T 1   C (R) T 2   C ((S X T 1 ) – R) T  T 1 – T 2

31 31 Example - Division u Identify all clients who have viewed all properties with three rooms. (  clientNo, propertyNo (Viewing))  (  propertyNo (  rooms = 3 (PropertyForRent)))

32 32 Relational Algebra Operations


Download ppt "Chapter 5 Relational Algebra and Relational Calculus."

Similar presentations


Ads by Google