Plan of the lecture • Some properties of relational algebra operators G53RDB: Theory of Relational Databases • Relational algebra on multisets Lecture 3 Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk Lecture 3 Some properties of ∪ Relational algebra operators • We defined operations on relations which take relations as operands and return a relation. • Commutativity: R ∪ S = S ∪ R • By relational algebra we will mean the following set of operators: (×, −, ∪, π, σ, ρ). Sometimes we also consider ∩ and ><, but they are definable using the rest. • Next we look at the algebraic properties of our operations (we need them because we are going to use them in query rewriting). • If R ⊆ S then R ∪ S = S Lecture 3 2 • Associativity: R ∪ (P ∪ S) = (R ∪ P ) ∪ S 3 Lecture 3 Some properties of ∩ Some properties of ∪ and ∩ • Commutativity: R ∩ S = S ∩ R • Distributivity: R ∩ (P ∪ S) = (R ∩ P) ∪ (R ∩ S) 4 R ∪ (P ∩ S) = (R ∪ P) ∩ (R ∪ S) • Associativity: R ∩ (P ∩ S) = (R ∩ P ) ∩ S • If R ⊆ S then R ∩ S = R Lecture 3 5 Lecture 3 6 1 Some properties of difference Some properties of × • R − (P ∪ S) = (R − P) ∩ (R − S) • Associativity: R × (P × S) = (R × P) × S) • R − (P ∩ S) = (R − P) ∪ (R − S) • Distributivity: R × (P ∪ S) = (R × P) ∪ (R × S) • R−R=∅ R × (P ∩ S) = (R × P) ∩ (R × S) • R− ∅=R • R ×∅= ∅ • ∅× R= ∅ Lecture 3 7 Lecture 3 Some properties of σ Some properties of π • σα (R) ∩ σβ (R) = σα ∧β (R) • If all attributes of R and S involved in the join condition are in X, then • σα (R) ∪ σβ (R) = σα ∨β (R) π X(R><S) = π X (R) >< π X (S) • If R and S are union compatible, • If condition α only involves attributes of R, then σα (R×S) = σα (R) × S π X(R ∪ S) = π X (R) ∪ π X (S) • If condition α only involves attributes of R, then σα (R><S) = σα (R) >< S • Give a counterexample to: • If condition α only involves attributes from set X, then π X(R ∩ S) = π X (R) ∩ π X (S) for union-compatible R and S. • πX σα (R) = σα πX (R) Lecture 3 8 9 Lecture 3 10 Relational algebra and duplicates Multisets • Relational algebra assumes that operations are defined on relations which are sets of tuples. • A multiset is a collection of elements, where each element has a specified number of occurrences. • Tables in a database are not exactly sets of rows: a table may contain duplicate rows. • For example, A = {x,x,y,z,z,z} is a multiset; x occurs 2 times, y once, z occurs 3 times. • Solution: define operations of relational algebra on bags or multisets (sets where each element may occur a number of times) instead of sets. • Multisets are unordered, so {x,x,y,z,z,z} is the same as {x,y,z,x,z,z}. Lecture 3 11 • The number of occurrences matters, so {x,x,y,z,z,z} is different multiset from {x,y,z}. Lecture 3 12 2 ∪, ∩, −, × for multisets Set theory for multisets • For two multisets A and B: – A = B if they have the same elements and each elements x occurs in A the same number of times as it occurs in B. – A ⊆ B if each element of A occurs in B at least as many times as it occurs in A. A ⊂ B if A ⊆ B and A ≠ B. – For example, {x,x,x,y} ⊆ {x,x,x,y,y,z} but not {x,x,x,y} ⊄{x,y,z} Lecture 3 • Let A, B be multisets. • If there are i copies of x in A and j copies of x in B, then there are i+j copies of x in A ∪ B • If there are i copies of x in A and j copies of x in B, then there are min(i,j) copies of x in A ∩ B • If there are i copies of x in A and j copies of x in B, then there are max(i-j, 0) copies of x in A − B • If there are i copies of x in A and j copies of y in B, then there are ij copies of (x,y) in A × B 13 Lecture 3 14 Example Projection and selection for multisets • Let A = {x,y,z,z} and B = {x,y,z,w}. Then • If a relation R is a multiset, and there are i tuples in R with the same value for X, then πX R contains i copies of those values. (For normal sets, πX R would have just one tuple with those values). • A ∪ B = {x,x,y,y,z,z,z,w} • A ∩ B = {x,y,z} • A − B = {z} • σα works the same way as for normal relations: if there were i duplicate tuples in R satisfying property α, then σαR contains all those i tuples. • A × B = { (x,x), (x,y), (x,z), (x,w), (y,x), (y,y), (y,z), (y,w), (z,x), (z,y), (z,z), (z,w), (z,x), (z,y), (z,z), (z,w) } Lecture 3 15 Example: projection for multisets 16 Example: selection for multisets πName R R Lecture 3 σAge>20R R Name Age Name Name Age Name Age John Dave Tom John Dave 21 30 20 22 30 John Dave Tom John Dave John Dave Tom John Dave 21 30 20 22 30 John Dave John Dave 21 30 22 30 Lecture 3 17 Lecture 3 18 3 Relational algebra for multisets Exercise • All operations of relational algebra can be reformulated for multisets. • Exercise: show that for multisets A, B and C A × (B ∪ C) = (A × B) ∪ (A × C) • We know (from set theory) that the two multisets contain the same elements, we need to check if they have the same number of occurrences for each element. • Suppose x occurs i times in A, y occurs j times in B and k times in C. How many copies of (x,y) in A × (B ∪ C) and in (A × B) ∪ (A × C)? • y occurs j+k times in B ∪ C, so i(j+k) times in A× (B ∪ C) • (x,y) occurs ij times in A × B, ik times in A × C, so ij+ik = i(j+k) times in (A × B) ∪ (A × C). • Most equivalences (rewriting rules) still hold and algorithms for evaluating relational algebra expressions can be easily modified for multisets. • Note: some don’t. For example, A ∪ A ≠ A. • Exercise: show that for multisets A, B and C A × (B ∪ C) = (A × B) ∪ (A × C) Lecture 3 19 Lecture 3 Exercises Recommended reading • Prove distributivity of Cartesian product over union: R × (P ∪ S) = (R × P) ∪ (R × S) using definitions of union and product. • Ullman, Widom: Chapter 5.1 – 5.3 20 • Does selection satisfy the following properties: – σα (σα (R)) = σα ( R) – σβ (σα (R)) = σα&β ( R) – σβ (σα (R)) = σα (σβ ( R)) • Does projection satisfy the following properties: – π X ( π X (R)) = π X (R) – π X ( π Y (R)) = π Y ( π X (R)) Lecture 3 21 Lecture 3 22 4