Professional Documents
Culture Documents
Relational Algebra
Relational Algebra
Example Instances
• “Sailors” and “Reserves” relations for our
Relational Algebra
examples. • Basic operations:
• We’ll use positional or named field notation, – Selection (ς) Selects a subset of rows from relation.
assume that names of fields in query results – Projection (π ) Deletes unwanted columns from relation.
are `inherited’ from names of fields in query – Cross-product (X) Allows us to combine two relations.
input relations. R1 SID BID Day
– Set-difference (-) Tuples in reln. 1, but not in reln. 2.
22 101 10/10/96 – Union(U) Tuples in reln. 1 and in reln. 2.
58 103 11/12/96 • Additional operations:
– Intersection, join, division, renaming: Not essential, but
S2 SID SName Rating Age (very!) useful.
S1 SID SName Rating Age
22 Dustbin 7 45.0
28 Yuppy 9 35.0 • Since each operation returns a relation, operations can
31 Lubber 8 55.5 be composed! (Algebra is “closed”.)
31 Lubber 8 55.5 44 Guppy 5 35.0
58 Rusty 10 35.0 58 Rusty 10 35.0
University of Botswana. CSI262 Lecture University of Botswana. CSI262 Lecture
5 6
Notes 2014-2015 Notes 2014-2015
1
2/20/2015
Projection Selection
• Deletes attributes that are not in projection list. • Selects rows that satisfy selection condition.
• Schema of result contains exactly the fields in the • No duplicates in result! (Why?)
projection list, with the same names that they had in • Schema of result identical to schema of (only) input
the (only) input relation. relation.
• Projection operator has to eliminate duplicates! • Result relation can be the input for another relational
(Why??) algebra operation! (Operator composition.)
– Note: real systems typically don’t do duplicate elimination
unless the user explicitly asks for it. (Why not?) ς Rating > 8 (S2)
π SName, Rating (S2) π Age(S2) π SName, Rating(ς Rating > 8 (S2))
SID SName Rating Age
SName Rating Age
28 Yuppy 9 35.0 SName Rating
Dustbin 7 35.0
58 Rusty 10 35.0 Yuppy 9
Lubber 8 55.5
Rusty 10
Guppy 5
Rusty 10
University of Botswana. CSI262 Lecture University of Botswana. CSI262 Lecture
7 8
Notes 2014-2015 Notes 2014-2015
Joins Joins
• Condition Join R cS = ς c (R X S) • Equi-Join: A special case of condition join where
(SID) SName Rating Age (SID) BID Day the condition c contains only equalities.
22 Dustbin 7 45.0 58 103 11/12/96
SID SName Rating Age BID Day
31 Lubber 8 55.5 58 103 11/12/96
22 Dustbin 7 45.0 101 10/10/96
• S1 S1.SID < R1.SID R1 58 Rusty 10 35.0 103 11/12/96
2
2/20/2015
3
2/20/2015
Find sailors who’ve reserved a red and a green boat Find the names of sailors who’ve reserved all boats
• Previous approach won’t work! Must identify • Uses division; schemas of the input relations
to / must be carefully chosen:
sailors who’ve reserved red boats, sailors
who’ve reserved green boats, then find the ρ(Tempsids, (π SID, BID Reserves) / (π BID Boats))
intersection (note that SID is a key for Sailors):
ρ(Tempred, π SID((ς color ‘red’ Boats) Reserves)) π Sname (Tempsids Sailors)
ρ(Tempgreen, π SID((ς color ‘green’ Boats) Reserves)) To find sailors who’ve reserved all ‘Interlake’
π SName((Tempred ∩ Tempgreen) Sailors) boats:
... / (π BID ς Bname = ‘Interlake’Boats))
Summary
• The relational model has rigorously defined
query languages that are simple and powerful.
• Relational algebra is more operational; useful
as internal representation for query evaluation
plans.
• Several ways of expressing a given query; a
query optimizer should choose the most
efficient version.