In this exercise, we would like you to figure out which relational
algebra operations were used to obtain each of the following tables.
student
Relational Algebra Cheatsheet
Create a relational algebra cheatsheet.
The cheatsheet should include each of the following operations:
selection, projection, Cartesian product, union, intersection, difference,
theta-join, equi-join, and natural join.
For each operation you should
For the example of the use of the operation,
use one or more of the following relations.
student(id, name, address)
JCUsubjects(code, lecturer)
TAFEsubjects(code, lecturer)
enrolledIn(id, code)
You may break the cheatsheet into two (or more) tables if the rows get
too big.
Here is an example to get you started.
English
operation
arity
description
selection
θ(R) = T
unary
select rows that satisfy θ
union
R
S = T
binary
create set of tuples from relations R and S
English
example
restrictions
selection
code=CP1500 OR code=cp3010(JCUsubjects)
none
union
JCUsubjects
TAFEsubjects
R and S must be union-compatible
English
degree
cardinality
selection
degree(R) = degree(T)
card(R) >= card(T)
union
degree(R) = degree(S) = degree(T)
card(R) + card(S) >= card(T) >= max(card(R),card(S))
Solution:
English
operation
arity
description
selection
θ(R) = T
unary
select rows that satisfy θ
projection
A1, A2, ..., An(R)
unary
project named columns from R
Cartesian product
R X S
binary
glue together all possible combinations of tuples
from R and S
union
R
S = T
binary
everything in R plus everything in S
intersection
R
S = T
binary
only tuples in both R and S
difference
R - S = T
binary
everying in R take away everything in S
theta-join
R
θ S = T
binary
do Cartesian product then selection using θ
equi-join
R
θ
S = T
binary
do Cartesian product then selection using θ
natural join
R
S = T
binary
equi-join on common attributes, but project only
one copy of attribute values!
English
example
restrictions
selection
code=CP1500 OR code=cp3010(JCUsubjects)
none
projection
code(JCUsubjects)
none
Cartesian product
JCUsubjects X TAFEsubjects
none
union
JCUsubjects
TAFEsubjects
R and S must be union-compatible
intersection
JCUsubjects
TAFEsubjects
R and S must be union-compatible
difference
JCUsubjects - TAFEsubjects
R and S must be union-compatible
theta-join
student
id <> id enrolledIn
none
equi-join
student
id = id enrolledIn
join condition can only involve AND and equality comparisons
natural join
student
enrolledIn
is a kind of equi-join
English
degree
cardinality
selection
degree(R) = degree(T)
card(R) >= card(T)
projection
degree(R) >= degree(T)
card(R) >= card(T)
Cartesian product
degree(R) + degree(S) = degree(T)
card(R) * card(S) = card(T)
union
degree(R) = degree(S) = degree(T)
card(R) + card(S) >= card(T) >= max(card(R),card(S))
intersection
degree(R) = degree(S) = degree(T)
min(card(R),card(S)) >= card(T)
difference
degree(R) = degree(S) = degree(T)
card(R) >= card(T)
theta-join
degree(R) + degree(S) = degree(T)
card(R) * card(S) >= card(T)
equi-join
degree(R) + degree(S) = degree(T)
card(R) * card(S) >= card(T)
natural join
degree(R) + degree(S) >= degree(T)
card(R) * card(S) >= card(T)
Slicing and dicing tables
Examine the following tables.
student enrolledIn subject
id | name id | code code | lecturer
------------------ -------------------- ---------------------
1234 | joe 1234 | cp1500 cp1500 | curtis
4000 | hector 1234 | cp1200 cp2001 | dave
2000 | ling 1234 | cp2001 cp3010 | curtis
4000 | cp3010 cp2001 | olivier
4000 | ma3000 ma3000 | roger
name
---------
joe
hector
ling
Solution:
name(student)
lecturer
---------
curtis
dave
olivier
roger
Solution:
lecturer(subject)
code | lecturer
---------------------
cp3010 | curtis
cp1500 | curtis
Solution:
lecturer=curtis(subject)
code=cp1500 OR code=cp3010(subject)
id | name
------------------
1234 | joe
4000 | hector
Solution:
name=joe OR name=hector(student)
id, name(student
enrolledIn)
name=ling(student)
id | name | id | code
--------------------------------------
1234 | joe | 1234 | cp1500
1234 | joe | 1234 | cp1200
1234 | joe | 1234 | cp2001
1234 | joe | 4000 | cp3010
1234 | joe | 4000 | ma3000
Solution:
Many ways are possible. Here are two.
name = joe(student
enrolledIn)
name = joe(student)
enrolledIn
id | name | id | code
--------------------------------------
1234 | joe | 1234 | cp1500
1234 | joe | 1234 | cp1200
1234 | joe | 1234 | cp2001
Solution:
Many ways are possible. Here is one.
student.id = enrolledIn.id enrolledIn
id | name | code
----------------------------
1234 | joe | cp1500
1234 | joe | cp1200
1234 | joe | cp2001
Solution:
Many ways are possible. Here is one.
name=joe(student
enrolledIn)
id | code
-------------------
1234 | cp1500
1234 | cp1200
1234 | cp2001
Solution:
id,code(
name=joe(student
enrolledIn)
id=1234(enrolledIn)
id | name | code | lecturer
--------------------------------------------------
4000 | hector | cp3010 | curtis
4000 | hector | ma3000 | roger
Solution:
(
name=hector(student))
enrolledIn
subject
name | lecturer
-------------------------
joe | curtis
hector | curtis
Solution:
name, lecturer(
lecturer=curtis(subject
enrolledIn
student))