Download presentation
Presentation is loading. Please wait.
1
RELATIONAL ALGEBRA (II)
6
Unary Relational Operations: SELECT and PROJECT
The PROJECT Operation Sequences of Operations and the RENAME Operation The SELECT Operation
8
Relational Algebra Operations from Set Theory
The UNION, INTERSECTION, and MINUS Operations
16
Binary Relational Operations: JOIN and DIVISION
The JOIN Operation The EQUIJOIN and NATURAL JOIN Variations of JOIN A Complete Set of Relational Algebra Operations The DIVISION Operation
17
Additional Relational Operations
Aggregate Functions and Grouping Recursive Closure Operations OUTER JOIN Operations The 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 S 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.
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 {<Dustin>, <Lubber>, <Horatio>}
30
sname(σbid=103(Reserves⋈ Sailors))
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(1sid1, 2sname, 3bid1, 4sid2, 5sname, 6bid2), ReservationsReservations) sname1σ(sid1=sid2)(bid1bid2)Reservationpairs)
37
QUERY 8 (Q8) Find the sids of sailors with age over 20 who have not reserved a red boat. sid(σage>20Sailors) - sid((σcolor=‘red’Boats) ⋈ Reserves ⋈ Sailors)
38
QUERY 9 (Q) Find the names of sailors who have reserved all boats.
(Tempsids, (sid,bidReserves) / (bidBoats)) sname(Tempsids ⋈ Sailors
39
QUERY Q10 (Q10) Find the names of sailors who have reserved all boats called Interlake. (Tempsids, (sid,bidReserves)/(bid(σbname=‘Interlake’Boats))) sname(Tempsids ⋈ Sailors)
41
Cartesian Product R(A1, A2, ..., Am) and S(B1, B2, ... , Bn)
T(A1, A2, ... , Am, B1, B2, ..., Bn) = R(A1, A2, ..., Am) X S(B1, B2, ..., Bn) A tuple t is in T if and only if t[A1,A2, ... ,Am] is in R and t[B1, B2, ..., Bn] is in S. - If R has N1 tuples and S has N2 tuples, then T will have N1*N2 tuples.
42
Cartesian Product A B a1 2 a2 4 A B C D E a1 2 4 d1 e1 3 d2 5 d3 e2 a2
Rx S A B a1 2 a2 4 A B C D E a1 2 4 d1 e1 3 d2 5 d3 e2 a2 S C D E 4 d1 e1 3 d2 5 d3 e2
43
Examples of Queries in Relational Algebra
44
Natural-Join Denoted by |x|. Binary operation
Creates a Cartesian-product of the arguments then performs selection to force equality on attributes that appear in both relations
45
Division Denoted by Binary Operation
Used in queries that include the phrase “for all”.
46
Division (Cont’d) Division is an operation on schema R – S
A tuple t is in r s if and only if: t is in ΠR – S(r) and For every tuple ts in s, there is a tuple tr in r satisfying both of the following: a. tr[S] = ts[R] b. tr[R – S] = t
48
Relational Algebra Fundamental operators Other operators select s
project p cartesian product union set difference Other operators natural join JOIN (butterfly symbol) set intersection division
49
A Simple DB account ac# owner ss# balance 1 bob 123 1000 account
sue jane transaction t# ac# type amount outcome date W bounced 5/1/98 D ok /2/98 W ok /4/98 D ok /7/98 W ok /9/98 account had transaction
50
Select Project eg: s balance>=1500 account
result : ac# owner ss# balance sue jane Project eg: π owner, ss# account result: owner ss# bob sue jane
51
Cartesian product Composing operations eg: account transaction
this will have 15 rows like the ones shown below: ac# owner ss# balance t# type amount outcome date bob W bounced 5/1/98 sue D ok /2/98 …………… Composing operations eg: “show all transactions done by account owner Bob”. σ account.ano= transaction.ano((s owner=“Bob” account) transaction)
52
σ owner=“Bob” (account JOIN transaction)
Natural 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# (s name=“Bob” (person)))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.