Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 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.

Similar presentations


Presentation on theme: "1 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."— Presentation transcript:

1 1 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.

2 2 Example/Quiz (1 minute). Use the Students database to construct two databases C and M for the CS department database and the Math department database. 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? Answer: project(select(select(Family, Father, Peter), Mother, Jane), {Name}).

3 3 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 similar. Do it as an exercise. QED.


Download ppt "1 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."

Similar presentations


Ads by Google