Relation Algebra
Relation Algebra
π
CS 186 Fall 2002, Lecture 7
R & G, Chapter 4
By relieving the brain of all unnecessary
work, a good notation sets it free to
concentrate on more advanced problems,
and, in effect, increases the mental power of
the race.
-- Alfred North Whitehead (1861 - 1947)
1
Formal Relational Query Languages
Two mathematical Query Languages form the
basis for “real” languages (e.g. SQL), and for
implementation:
Relational Algebra: More operational, very
useful for representing execution plans.
Preliminaries
2
Relational Algebra: 5 Basic Operations
• Selection ( σ ) Selects a subset of rows from
relation (horizontal).
• Projection ( π ) Retains only wanted columns
from relation (vertical).
• Cross-product (x) Allows us to combine two
relations.
• Set-difference (–) Tuples in r1, but not in r2.
• Union (∪ ) Tuples in r1 and/or in r2.
3
Projection
• Examples: π age(S2) ; π sname, rating(S2)
• Retains only attributes that are in the “projection
list”.
• Schema of result:
– exactly the fields in the projection list, with the
same names that they had in the input relation.
• Projection operator has to eliminate duplicates
(How do they arise? Why remove them?)
– Note: real systems typically don’t do duplicate
elimination unless the user explicitly asks for it.
(Why not?)
sname rating
Projection yuppy 9
lubber 8
guppy 5
rusty 10
sid sname rating age π sname,rating (S 2)
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0 age
58 rusty 10 35.0
35.0
S2
55.5
π age(S2)
4
Selection (σ)
5
Union
sid sname rating age
sid sname rating age
22 dustin 7 45.0
22 dustin 7 45.0
31 lubber 8 55.5
31 lubber 8 55.5 58 rusty 10 35.0
58 rusty 10 35.0 44 guppy 5 35.0
S1 28 yuppy 9 35.0
sid sname rating age S1 ∪ S 2
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
S2
Set Difference
sid sname rating age sid sname rating age
22 dustin 7 45.0 22 dustin 7 45.0
31 lubber 8 55.5 S1− S2
58 rusty 10 35.0
S1
sid sname rating age sid sname rating age
28 yuppy 9 35.0 28 yuppy 9 35.0
31 lubber 8 55.5 44 guppy 5 35.0
44 guppy 5 35.0
58 rusty 10 35.0 S2 – S1
S2
6
Cross-Product
• S1 x R1: Each row of S1 paired with each row of R1.
• Q: How many rows in the result?
• Result schema has one field per field of S1 and R1,
with field names `inherited’ if possible.
– May have a naming conflict: Both S1 and R1 have
a field with the same name.
– In this case, can use the renaming operator:
ρ (C(1→ sid1, 5 → sid 2), S1× R1)
7
Compound Operator: Intersection
Intersection
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0 sid sname rating age
31 lubber 8 55.5
S1
58 rusty 10 35.0
sid sname rating age
28
31
yuppy
lubber
9
8
35.0
55.5
S1∩ S 2
44 guppy 5 35.0
58 rusty 10 35.0
S2
8
Compound Operator: Join
• Joins are compound operators involving cross
product, selection, and (sometimes) projection.
• Most common type of join is a “natural join” (often
just called “join”). R S conceptually is:
– Compute R X S
– Select rows where attributes that appear in both relations
have equal values
– Project all unique atttributes and one copy of each of the
common ones.
• Note: Usually done much more efficiently than this.
• Useful for putting “normalized” relations back
together.
R1 S1 =
9
Other Types of Joins
• Condition Join (or “theta-join”):
R
c S = σ c ( R × S)
10
Examples of Division A/B
sno pno pno pno pno
s1 p1 p2 p2 p1
s1 p2 p4 p2
B1
s1 p3 p4
B2
s1 p4
s2 p1 sno B3
s2 p2 s1
s3 p2 s2 sno
s4 p2 s3 s1 sno
s4 p4 s4 s4 s1
11
sid bid day
Reserves
Examples 22 101 10/10/96
58 103 11/12/96
sid sname rating age
Sailors 22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
12
Find names of sailors who’ve reserved a red boat
ρ (Tempboats, (σ Boats))
color =' red ' ∨ color =' green '
π sname(Tempboats
Re serves
Sailors)
13
Find sailors who’ve reserved a red and a green boat
• Previous approach won’t work! Must identify
sailors who’ve reserved red boats, sailors who’ve
reserved green boats, then find the intersection
(note that sid is a key for Sailors):
π sname((Tempred ∩ Tempgreen)
Sailors)
14