Section 10.4.1 Relational Algebras A relational algebra consists of a

!
Section 10.4.1 Relational Algebras
A relational algebra consists of a set of relations together with operations to construct new
relations. We’ll describe the three operations select, project, and join.
Select Operation: Let R be a relation, A an attribute, and a a possible element of some
tuple. Then select(R, A, a) is the subset of tuples with a in the Ath position. So we have
select(R, A, a) = {t ∈ R | t(A) = a}.
Project Operation: Let R be a relation with attribute set I and X ⊆ I. Then project(R, X) is
the set of tuples indexed by X and constructed from tuples of R by deleting components with
attributes in I – X. So we have
project(R, X) = {s | ∃ t ∈ R ∀ A ∈ X (s(A) = t(A))}.
Join Operation: Let R and S be relations with attribute sets I and J. Then the join(R, S) is
the relation with attribute set I ∪ J defined by
join(R, S) = {t | ∃ r ∈ R ∃ s ∈ S ∀ A ∈ I ∀ B ∈ J (t(A) = r(A)) ∧ (t(B) = s(B))}.
Example.
R
A B C
a b c
x b y
a r u
M M M
S
A B
a b
a b
a r
M M
D
d
w
z
M
select( R, A,a) proj( R,{A,C})
A B C
A C
a b c
a c
a r u
x y
M M M
a u
M M
join( R, S)
A B C D
a b c d
a b c w
a r u z
M M M M
1
!
!
!
!
Students
Name
ID Major Credits
B. Franklin 123
CS
75
T. Jefferson 217
CS
94
B. Franklin 123 Math
75
A. Lincoln 186 Hist
110
M
M
M
M
!
Family
Name
Mother Father
A. Lincoln Nancy Thomas
B. Franklin Abiah
Josiah
T. Jefferson
Jane
Peter
M
M
M
Example/Quiz (1 minute). Use the Students database to construct two databases C and M
! department database.
for the CS department database and the Math
Answer: C = select(Students, Major, CS) and M = select(Students, Major, Math).
Example/Quiz (1 minute). Use the Students database to construct the database D of the
names, id’s, and credits of students who are double majoring in math and CS.
Answer: Let A = project(C, {Name, ID, Credits}) and B = project(M, {Name, ID, Credits}).
Then D = join(A, B).
Example/Quiz (1 minute). From the database Family, who are the children of Thomas?
Answer: project(select(Family, Father, Thomas), {Name})
Example/Quiz (1 minute). From the database Family, who are B. Franklin’s parents?
Answer: project(select(Family, Name, B. Franklin), {Mother, Father})
Example/Quiz (1 minute). From the database Family, who are children by Jane and Peter?
2
Answer: project(select(select(Family, Father, Peter), Mother, Jane), {Name}).
Properties Notation: join(R, S) is sometimes denoted R  S.
• R  R = R
• join is associative: (R  S)  T = R  (S  T)
• select(select(R, A, a), B, b) = select(select(R, B, b), A, a).
• project(select(R, A, a), X) = select(project(R, X), A, a) if and only if A ∈ X.
Let I and J be the attribute sets for R and S.
• If I ∩ J = ∅, then join(R, S) = R × S.
• If I = J, then the following four properties hold:
(a) join(R, S) = R ∩ S.
(b) select(R ∪ S, A, a) = select(R, A, a) ∪ select(S, A, a).
(c) select(R ∩ S, A, a) = select(R, A, a) ∩ select(S, A, a).
(d) select(R – S, A, a) = select(R, A, a) – select(S, A, a).
Example. Prove that join is associative: (R  S)  T = R  (S  T).
Proof: Let I, J, K be the attribute sets of R, S, T. Let x ∈ (R  S)  T. Then there exist
tuples u ∈ (R  S) and t ∈ T such that x(A) = u(A) for all A ∈ I ∪ J and x(A) = t(A) for
all A ∈ K. Since u ∈ (R  S) there exist tuples r ∈ R and s ∈ S such that u(A) = r(A) for
all A ∈ I and u(A) = s(A) for all A ∈ J. So we have
x(A) = r(A) for all A ∈ I, x(A) = s(A) for all A ∈ J, and x(A) = t(A) for all A ∈ K.
Let w ∈ S  T be defined by w(A) = s(A) for all A ∈ J and w(A) = t(A) for all A ∈ J. So
we have x(A) = w(A) for all A ∈ J ∪ K. Since x(A) = r(A) for all A ∈ I, it follows that x ∈
R  (S  T). Therefore (R  S)  T ⊆ R  (S  T). The other containment is
3
similar. Do it as an exercise. QED.