Presentation is loading. Please wait.

Presentation is loading. Please wait.

RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science.

Similar presentations


Presentation on theme: "RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science."— Presentation transcript:

1 RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science

2

3

4

5

6 Unary Relational Operations: SELECT and PROJECT zThe PROJECT Operation zSequences of Operations and the RENAME Operation zThe SELECT Operation

7

8 Relational Algebra Operations from Set Theory zThe UNION, INTERSECTION, and MINUS Operations

9

10

11

12

13

14

15

16 Binary Relational Operations: JOIN and DIVISION zThe JOIN Operation zThe EQUIJOIN and NATURAL JOIN Variations of JOIN zA Complete Set of Relational Algebra Operations zThe DIVISION Operation

17 Additional Relational Operations zAggregate Functions and Grouping zRecursive Closure Operations zOUTER JOIN Operations zThe OUTER JOIN Operation

18 SPECIAL RELATIONAL OPERATORS The following operators are peculiar to relations: - Join operators There are several kind of join operators. We only consider three of these here (others will be considered when we discuss null values): - (1) Condition Joins - (2) Equijoins - (3) Natural Joins - Division

19 JOIN OPERATORS Condition Joins: - Defined as a cross-product followed by a selection: R ⋈ c S = σ c (R  S) ( ⋈ is called the bow-tie) where c is the condition. - Example: Given the sample relational instances S1 and R1 The condition join S ⋈ S1.sid<R1.sid R1 yields

20 Equijoin: Special case of the condition join where the join condition consists solely of equalities between two fields in R and S connected by the logical AND operator ( ∧ ). Example: Given the two sample relational instances S1 and R1 The operator S1 R.sid=Ssid R1 yields

21 Natural Join - Special case of equijoin where equalities are implicitly specified on all fields having the same name in R and S. - The condition c is now left out, so that the “bow tie” operator by itself signifies a natural join. - N. B. If the two relations have no attributes in common, the natural join is simply the cross-product.

22

23

24 DIVISION - The division operator is used for queries which involve the ‘all’ qualifier such as “Find the names of sailors who have reserved all boats”. - The division operator is a bit tricky to explain.

25 EXAMPLES OF DIVISION

26 DIVISION Example: Find the names of sailors who have reserved all boats: (1) A =  sid,bid (Reserves). A1 =  sid (Reserves) A2 =  bid (Reserves) (2) B2 =  bid (Boats) B3 is the rest of B. Thus, B2 ={101, 102, 103, 104} (3)Find the rows of A such that their A.sid is the same and their combined A.bid is the set B2. Thus we find A1 = {22} (4) Get the set of A2 corresponding to A1: A2 = {Dustin}

27 FORMAL DEFINITION OF DIVISION The formal definition of division is as follows: A/B =  x (A) -  x ((  x (A)  B) – A)

28 EXAMPLES OF ALGEBRA QUERIES In the rest of this chapter we shall illustrate queries using the following new instances S3 of sailors, R2 of Reserves and B1 of boats.

29 QUERY Q1 Given the relational instances: (Q1) Find the names of sailors who have reserved boat 103  sname ((σ bid=103 Reserves) ⋈ Sailors) The answer is thus the following relational instance {,, }

30 QUERY Q1 (cont’d) There are of course several ways to express Q1 in relational algebra. Here is another:  sname (σ bid=103 (Reserves ⋈ Sailors)) Which of these expressions should we use? That is a question of optimization. Indeed, when we describe how to state queries in SQL, we can leave it to the optimizer in the DBMS to select the nest approach.

31 QUERY Q2 (Q2) Find the names of sailors who have reserved a red boat.  sname ((σ color=‘red’ Boats) ⋈ Reserves ⋈ Sailors)

32 QUERY Q3 (Q3) Find the colors of boats reserved by Lubber.  color ((σ sname=‘Lubber’Sailors )Sailors ⋈ Reserves ⋈ Boats)

33 QUERY Q4 (Q4) Find the names of Sailors who have reserved at least one boat  sname (Sailors ⋈ Reserves)

34 QUERY Q5 (Q5) Find the names of sailors who have reserved a red or a green boat.  (Tempboats, (σ color=‘red’ Boats) ∪ (σ color=‘green’ Boats))  sname (Tempboats ⋈ Reserves ⋈ Sailors)

35 QUERY Q6 (Q6) Find the names of Sailors who have reserved a red and a green boat. It seems tempting to use the expression used in Q5, replacing simply ∪ by ∩. However, this won’t work, for such an expression is requesting the names of sailors who have requested a boat that is both red and green! The correct expression is as follows:  (Tempred,  sid ((σ color=‘red’ Boats) ⋈ Reserves))  (Tempgreen,  sid ((σ color=‘green’ Boats) ⋈ Reserves))  sname ((Tempred ∩ Tempgreen) ⋈ Sailors)

36 QUERY Q7 (Q7) Find the names of sailors who have reserved at least two boats.  (Reservations,  sid,sname,bid (Sailors ⋈ Reserves))  (Reservationpairs(1  sid1, 2  sname, 3  bid1, 4  sid2, 5  sname, 6  bid2), Reservations  Reservations)  sname1 σ (sid1=sid2)  (bid1  bid2) Reservationpairs)

37 QUERY 8 (Q8) Find the sids of sailors with age over 20 who have not reserved a red boat.  sid (σ age>20 Sailors) -  sid ((σ color=‘red’ Boats) ⋈ Reserves ⋈ Sailors)

38 QUERY 9 (Q) Find the names of sailors who have reserved all boats.  (Tempsids, (  sid,bid Reserves) / (  bidBoats ))  sname (Tempsids ⋈ Sailors

39 QUERY Q10 (Q10) Find the names of sailors who have reserved all boats called Interlake.  (Tempsids, (  sid,bid Reserves)/(  bid (σ bname=‘Interlake’ Boats)))  sname (Tempsids ⋈ Sailors)

40

41 Cartesian Product z R(A 1, A 2,..., A m ) and S(B 1, B 2,..., B n ) z T(A 1, A 2,..., A m, B 1, B 2,..., B n ) = R(A 1, A 2,..., A m ) X S(B 1, B 2,..., B n ) z A tuple t is in T if and only if t[A 1,A 2,...,A m ] is in R and t[B 1, B 2,..., B n ] is in S. -If R has N 1 tuples and S has N 2 tuples, then T will have N 1 *N 2 tuples.

42 Cartesian Product AB a12 a24 R S Rx S CDE 4d1e1 3d2e1 5d3e2 ABCDE a124d1e1 a123d2e1 a125d3e2 a244d1e1 a243d2e1 a245d3e2

43 Examples of Queries in Relational Algebra

44 Natural-Join zDenoted by |x|. zBinary operation zCreates a Cartesian-product of the arguments then performs selection to force equality on attributes that appear in both relations

45 Division zDenoted by  zBinary Operation zUsed in queries that include the phrase “for all”.

46 Division (Cont’d) zDivision is an operation on schema R – S zA tuple t is in r  s if and only if: zt is in Π R – S (r) and zFor every tuple t s in s, there is a tuple t r in r satisfying both of the following: a. t r [S] = t s [R] b. t r [R – S] = t

47

48 Relational Algebra Fundamental operators  select   project  zcartesian product  zunion  zset difference - Other operators znatural join JOIN (butterfly symbol) zset intersection  zdivision 

49 A Simple DB account ac# owner ss# balance 1 bob 123 1000 2 sue 456 2000 3 jane 789 3000 transaction t# ac# type amount outcome date 1 1 W 1500 bounced 5/1/98 2 2 D 1000 ok 5/2/98 3 1 W 100 ok 5/4/98 4 3 D 500 ok 5/7/98 5 2 W 200 ok 5/9/98 account had transaction

50 Select eg:  balance>=1500 account result : ac# owner ss# balance 2 sue 456 2000 3 jane 789 3000 Project eg: π owner, ss# account result: owner ss# bob 123 sue 456 jane 789

51 Cartesian product eg: account  transaction ythis will have 15 rows like the ones shown below: ac# owner ss# balance t# type amount outcome date 1 bob 123 1000 1 W 1500 bounced 5/1/98 2 sue 456 2000 2 D 1000 ok 5/2/98 …………… Composing operations eg: “show all transactions done by account owner Bob”. σ account.ano= transaction.ano ((  owner=“Bob” account)  transaction)

52 zNatural Join - combines σ, π,  - very commonly used Natural Join forms the cross product of its two arguments, does a selection to enforce equality of columns with the same name and removes duplicate columns. Eg: “show all transactions done by account owner Bob” σ owner=“Bob” (account JOIN transaction)

53 Rename operation What if you need to access the same relation twice in a query? eg. person(ss#, name, mother_ss#, father_ss#) “Find the name of Bob’s mother” needs the “person” table to be accessed twice. The operation ρ x (r) evaluates to a second logical copy of relation r renamed to x.

54 Rename operation (contd) eg: π mother.name ( (ρ mother (person)) JOIN mother.ss# = person.mother_ss# (  name=“Bob ” (person)))

55

56

57

58


Download ppt "RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science."

Similar presentations


Ads by Google