Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.

Similar presentations


Presentation on theme: "Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11."— Presentation transcript:

1 Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11

2 Nov 18, 2003Murali Mani Basics Relational Algebra is defined on bags, rather than relations. Bag or multiset allows duplicate values; but order is not significant. We can write an expression using relational algebra operators with parentheses: we need closure – an operator on bag returns a bag. Relational algebra includes set operators, and other operators specific to relational model.

3 Nov 18, 2003Murali Mani Set Operators Union, Intersection, Difference, cross product Union, Intersection and Difference are defined only for union compatible relations. Two relations are union compatible if they have the same set of attributes and the types (domains) of the attributes are the same. Eg of two relations that are not union compatible: Student (sNumber, sName) Course (cNumber, cName)

4 Nov 18, 2003Murali Mani Union: ∪ Consider two bags R 1 and R 2 that are union- compatible. Suppose a tuple t appears in R 1 m times, and in R 2 n times. Then in the union, t appears m + n times. AB 12 34 12 R1R1 AB 12 34 56 R2R2 AB 12 12 12 34 34 56 R 1 ∪ R 2

5 Nov 18, 2003Murali Mani Intersection: ∩ Consider two bags R 1 and R 2 that are union- compatible. Suppose a tuple t appears in R 1 m times, and in R 2 n times. Then in the intersection, t appears min (m, n) times. AB 12 34 12 R1R1 AB 12 34 56 R2R2 AB 12 34 R 1 ∩ R 2

6 Nov 18, 2003Murali Mani Difference: - Consider two bags R 1 and R 2 that are union- compatible. Suppose a tuple t appears in R 1 m times, and in R 2 n times. Then in R 1 – R 2, t appears max (0, m - n) times. AB 12 34 12 R1R1 AB 12 34 56 R2R2 AB 12 R 1 – R 2

7 Nov 18, 2003Murali Mani Bag semantics vs Set semantics Union is idempotent for sets: (R1 ∪ R2) ∪ R2 = R1 ∪ R2 Union is not idempotent for bags. Intersection and difference are idempotent for sets and bags. For sets and bags, R 1  R 2 = R 1 – (R 1 – R 2 ).

8 Nov 18, 2003Murali Mani Cross Product (Cartesian Product): Ⅹ Consider two bags R 1 and R 2. Suppose a tuple t 1 appears in R 1 m times, and a tuple t 2 appears in R 2 n times. Then in R 1 X R 2, t 1 t 2 appears mn times. AB 12 12 R1R1 BC 23 45 45 R2R2 AR 1.BR 2.BC 1223 1223 1245 1245 1245 1245 R 1 X R 2

9 Nov 18, 2003Murali Mani Basic Relational Operations Select, Project, Join Select: denoted σ C (R): selects the subset of tuples of R that satisfies selection condition C. C can be any boolean expression, its clauses can be combined with AND, OR, NOT. ABC 125 346 127 127 R σ (C ≥ 6) (R) ABC 346 127 127

10 Nov 18, 2003Murali Mani Select Select is commutative: σ C2 (σ C1 (R)) = σ C1 (σ C2 (R)) Select is idempotent: σ C (σ C (R)) = σ C (R) We can combine multiple select conditions into one condition. σ C1 (σ C2 (… σ Cn (R)…)) = σ C1 AND C2 AND … Cn (R)

11 Nov 18, 2003Murali Mani Project: π A1, A2, …, An (R) Consider relation (bag) R with set of attributes A R. π A1, A2, …, An (R), where A1, A2, …, An  A R returns the tuples in R, but only with columns A1, A2, …, An. ABC 125 346 127 128 R π A, B (R) AB 12 34 12 12

12 Nov 18, 2003Murali Mani Project: Bag Semantics vs Set Semantics For bags, the cardinality of R = cardinality of π A1, A2, …, An (R). For sets, cardinality of R ≥ cardinality of π A1,A2, …, An (R). For sets and bags project is not commutative project is idempotent

13 Nov 18, 2003Murali Mani Natural Join: R ⋈ S Consider relations (bags) R with attributes A R, and S with attributes A S. Let A = A R ∩ A S. R ⋈ S can be defined as π A R – A, A, A S - A (σ R.A1 = S.A1 AND R.A2 =S.A2 AND … R.An=S.An (R X S)) where A = {A1, A2, …, An} The above expression says: select those tuples in R X S that agree in values for each of the A attributes, and project the resulting tuples such that we have only one value for each A attribute.

14 Nov 18, 2003Murali Mani Natural Join example AB 12 12 R1R1 BC 23 45 45 R2R2 ABC 123 123 R 1 ⋈ R 2

15 Nov 18, 2003Murali Mani Theta Join: R ⋈ C S Theta Join is similar to natural join, except that we can specify any condition C. It is defined as R ⋈ C S = (σ C (R X S)) AB 12 12 R1R1 BC 23 45 45 R2R2 R 1 ⋈ R1.B<R2.B R 2 AR 1.BR 2.BC 1245 1245 1245 1245

16 Nov 18, 2003Murali Mani Outer Join: R ⋈ o S Similar to natural join, however, if there is a tuple in R, that has no “matching” tuple in S, or a tuple in S that has no matching tuple in R, then that tuple also appears, with null values for attributes in S (or R). ABC 123 456 789 R1R1 BCD 2310 2311 6712 R2R2 R 1 ⋈ o R 2 ABCD 12310 12311 456null 789 6712

17 Nov 18, 2003Murali Mani Left Outer Join: R ⋈ o L S Similar to natural join, however, if there is a tuple in R, that has no “matching” tuple in S, then that tuple also appears, with null values for attributes in S (note: a tuple in S that has no matching tuple in R does not appear). ABC 123 456 789 R1R1 BCD 2310 2311 6712 R2R2 R 1 ⋈ o L R 2 ABCD 12310 12311 456null 789

18 Nov 18, 2003Murali Mani Right Outer Join: R ⋈ o R S Similar to natural join, however, if there is a tuple in S, that has no “matching” tuple in R, then that tuple also appears, with null values for attributes in R (note: a tuple in R that has no matching tuple in S does not appear). ABC 123 456 789 R1R1 BCD 2310 2311 6712 R2R2 R 1 ⋈ o R R 2 ABCD 12310 12311 null6712

19 Nov 18, 2003Murali Mani Renaming: ρ S(A1, A2, …, An) (R) Rename relation R to S, attributes of R are renamed to A1, A2, …, An ρ S (R) renames relation R to S, keeping the attributes same. BCD 2310 2311 6712 R2R2 XCD 2310 2311 6712 ρ S(X, C, D) (R 2 ) S BCD 2310 2311 6712 ρ S (R 2 ) S

20 Nov 18, 2003Murali Mani Example: Introducing new relations Find the semijoin of 2 relations R, S. Semijoin denoted R ⋉ S is defined as the tuples in R, such that for a tuple t1 in R, if there exists a tuple t2 in S that t1 and t2 agree in all attributes common to R and S, then t1 appears in the result. R1 = R ⋈ S R2 = π AR (R1) R ⋉ S = R2 ⋂ R

21 Nov 18, 2003Murali Mani Duplicate Elimination:  (R) Convert a bag to a set. R AB 12 34 12 12  (R) AB 12 34

22 Nov 18, 2003Murali Mani Extended Projection: π L (R) Here L can be An attribute (just like simple projection) An expression x → y, where x and y are names of attributes, this renames attribute x to y. An expression E → z, where E is any expression involving attributes, constants, and arithmetic and string operators. This has an attribute called z whose values are given by E. BCD 2310 2311 6712 R π B→A, C+D→X, C, D (R) AXCD 213310 214311 619712

23 Nov 18, 2003Murali Mani Aggregation operators MIN, MAX, COUNT, SUM, AVG The aggregate operators aggregate the values in one column of a relation. R AB 12 34 12 12 MIN (B) = 2 MAX (B) = 4 COUNT (B) = 4 SUM (B) = 10 AVG (B) = 2.5

24 Nov 18, 2003Murali Mani Grouping Operator:  GL, AL (R) Usually we want to perform aggregation not on all the values of a column, but on a subgroup of the values of a column.  GL, AL (R) groups all attributes in GL, and performs the aggregation specified in AL. titleyearstarName SW177HF Matrix99KR 6D&7N93HF SW279HF Speed94KR StarsIn  starName, MIN (year)→year, COUNT(title) →num (StarsIn) starNameyearnum HF773 KR942

25 Nov 18, 2003Murali Mani Sorting Operator:  L (R) It sorts the tuples in R. If L is list A1, A2, …, An, it first sorts by A1, then by A2, and so on. Sort is used as a last operator in an expression. ABC 125 316 127 138 R ABC 125 127 138 316  A,B (R)


Download ppt "Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11."

Similar presentations


Ads by Google