Introduction to Relational Algebra
by K. Yue
1. Introduction
2. Basic Operations
Cartesian Product
Example:
R(A,B,C) has three tuples. S(A,D) has four tuples.
The result of R x S always has 12 tupes with the schema (R.A, B, C, S.A, D).
Select
σcond(R) = {t | t ε R and cond}
Project
πc1, .., cm(R) = {s | there exists t ε R (t(ci) = s(ci), for 1 <= i <= m)}
Union
R U S = {t | t ε R or t ε S}
Example:
Suppose StaffID and FacultyID are uniion compatible.
πStaffID(Staff) U πFacultyID(Faculty)
Difference (Minus)
R - S = {t | t ε R and not (t ε S)}
Rename
3. Common Derived Operations
Theta-join
R1 |x|Θ R2
Equi-join
Natural Join
Let C1, C2, ... Cm be the common attributes of R1 and R2.
R |x| S = πA1, A2, .. Al(σR.C1=S.C1,.., R.Cm=S.Cm(RxS)
where A1, A2, ... Al is the list of components in RxS except S.C1, S.C2,.. S.Cm.
Example:
The schema of R(A,B) |x| S(A,C) is ABC. The schema of R(A,B) x S(A,C) is {R.A, B, S.A, C}.
Exercise
Let the cardinality of R(A,B) be 5 and the cardinality of S(A,C) be 6. What is the range of the cardinality of R(A,B) |x| S(A,C)?
Other Joins
Division
Example:
Consider:
Supplier(SNum, SName, SCity, Status)
Part(PNum, PName, Color, Weight)
Supply(SNum, PNum, Quantity)
An example of relation instance:
Supplier:
SNum | SName |
SCity |
Status |
S1 |
ABC |
Dallas |
10 |
S2 |
DEF |
Houston |
20 |
S3 |
Go go |
Houston |
12 |
S4 |
P&G |
Dallas |
2 |
S5 |
Yap |
Phoenix |
5 |
S6 |
Yue |
Dallas |
1 |
Part:
PNum | PName |
Color |
Weight |
P1 |
Drum |
Green |
10 |
P2 |
Hammer |
Green |
20 |
P3 |
Minipod |
Red |
4 |
P4 |
Micropod |
Red |
4 |
P5 |
Blue Spur |
Blue |
3 |
P6 |
Musical Box |
Blue |
13 |
P7 |
Bear |
Blue |
9 |
P8 |
Panda |
White |
10 |
Supply:
SNum | PNum |
Quantity |
S1 |
P1 |
10 |
S1 |
P2 |
3 |
S2 |
P1 |
11 |
S2 |
P2 |
1 |
S2 |
P4 |
6 |
S3 |
P4 |
1 |
S3 |
P5 |
2 |
S3 |
P6 |
12 |
S3 |
P7 |
5 |
S4 |
P2 |
1 |
S4 |
P5 |
10 |
S4 |
P7 |
4 |
S4 |
P8 |
10 |
S5 |
P1 |
11 |
S5 |
P3 |
5 |
S5 |
P4 |
10 |
S5 |
P5 |
14 |
π SNUM (SUPPLIER):
SNum |
S1 |
S2 |
S3 |
S4 |
S5 |
S6 |
π SNUM (SUPPLY):
SNum |
S1 |
S2 |
S3 |
S4 |
S5 |
σ STATUS >= 10 (SUPPLIER):
SNum | SName |
SCity |
Status |
S1 |
ABC |
Dallas |
10 |
S2 |
DEF |
Houston |
20 |
S3 |
Go go |
Houston |
12 |
(π SNUM (SUPPLIER)) * (σ STATUS >= 10 (SUPPLIER)):
L.SNum | R.SNum | SName |
SCity |
Status |
S1 | S1 |
ABC |
Dallas |
10 |
S1 | S2 |
DEF |
Houston |
20 |
S1 | S3 |
Go go |
Houston |
12 |
S2 | S1 |
ABC |
Dallas |
10 |
S2 | S2 |
DEF |
Houston |
20 |
S2 | S3 |
Go go |
Houston |
12 |
S3 | S1 |
ABC |
Dallas |
10 |
S3 | S2 |
DEF |
Houston |
20 |
S3 | S3 |
Go go |
Houston |
12 |
S4 | S1 |
ABC |
Dallas |
10 |
S4 | S2 |
DEF |
Houston |
20 |
S4 | S3 |
Go go |
Houston |
12 |
S5 | S1 |
ABC |
Dallas |
10 |
S5 | S2 |
DEF |
Houston |
20 |
S5 | S3 |
Go go |
Houston |
12 |
S6 | S1 |
ABC |
Dallas |
10 |
S6 | S2 |
DEF |
Houston |
20 |
S6 | S3 |
Go go |
Houston |
12 |
(π SNUM (SUPPLIER)) |X| (σ STATUS >= 10 (SUPPLIER)):
SNum | SName |
SCity |
Status |
S1 |
ABC |
Dallas |
10 |
S2 |
DEF |
Houston |
20 |
S3 |
Go go |
Houston |
12 |
π SNUM, PNUM (SUPPLY)):
SNum | PNum |
S1 |
P1 |
S1 |
P2 |
S2 |
P1 |
S2 |
P2 |
S2 |
P4 |
S3 |
P4 |
S3 |
P5 |
S3 |
P6 |
S3 |
P7 |
S4 |
P2 |
S4 |
P5 |
S4 |
P7 |
S4 |
P8 |
S5 |
P1 |
S5 |
P3 |
S5 |
P4 |
S5 |
P5 |
π PNUM (σ COLOR = ‘Green’ (PART)):
PNum |
P1 |
P2 |
π SNUM, PNUM (SUPPLY)) / π PNUM (σ COLOR = ‘Green’ (PART)):
SNum |
S1 |
S2 |
Exercise:
Work on some of the query questions listed in the Supply database example.
4. Epilog
Some shortcomings of Relational Algebra: