Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.

Similar presentations


Presentation on theme: "More Relation Operations 2014, Fall Pusan National University Ki-Joune Li."— Presentation transcript:

1 More Relation Operations 2014, Fall Pusan National University Ki-Joune Li

2 2 Relational Algebra on Bags A bag is like a set, but an element may appear more than once. o Multiset is another name for “ bag. ” o Example:  {1,2,1,3} is a bag.  {1,2,3} is also a bag that happens to be a set. Bags also resemble lists, but order in a bag is unimportant. o Example: {1,2,1} = {1,1,2} as bags, but [1,2,1] != [1,1,2] as lists.

3 3 Why Bags? SQL, the most important query language for relational databases is actually a bag language. o SQL will eliminate duplicates, but usually only if you ask it to do so explicitly. Some operations, like projection, are much more efficient on bags than sets. o Projection: No more operation except deleting other attributes

4 4 Operations on Bags Selection: o Applies to each tuple, o so its effect on bags is like its effect on sets. Projection o As a bag operator, we do not eliminate duplicates. Products and joins o done on each pair of tuples, so duplicates in bags have no effect on how we operate.

5 Examples Given two relations R(A,B) and S(B,C) 5 AB 12 56 12 R BC 34 78 S AB 12 12 A 1 5 1 SELECT A+B<5 (R)Project A (R) AR.BS.BC 1234 56 34 1234 1278 56 78 1278 R X S R R.B<S.B S AR.BS.BC 1234 1234 1278 56 78 1278

6 6 Bag Union, Intersection, Difference Union, intersection, and difference need new definitions for bags. Union: An element appears in the union of two bags the sum of the number of times it appears in each bag. o Example: {1,2,1} U {1,1,2,3,1} = {1,1,1,1,1,2,2,3} Intersection: An element appears in the union of two bags the minimum of the number of times it appears in either. o Example: {1,2,1} ∩ {1,2,3} = {1,2}. Difference: An element appears in the difference A – B of bags as many times as it appears in A, minus the number of times it appears in B. o But never less than 0 times. o Example: {1,2,1} – {1,2,3} = {1}.

7 7 Beware: Bag Laws != Set Laws Not all algebraic laws that hold for sets also hold for bags. o Example: Commutative law for union (R UNION S = S UNION R ) does hold for bags.  Since addition is commutative, adding the number of times x appears in R and S doesn ’ t depend on the order of R and S. o Counter Example:  Set union is idempotent, meaning that S UNION S = S.  However, for bags, if x appears n times in S, then it appears 2n times in S UNION S.  Thus S UNION S != S in general

8 8 The Extended Algebra δ: eliminate duplicates from bags. τ L : sort tuples. o L is a list of attributes o Result of Sorting: a List (not set neither bag) Extended projection : arithmetic, duplication of columns. γ: grouping and aggregation. OUTERJOIN ( ):avoids “ dangling tuples ” = tuples that do not join with anything. ◦

9 9 Extended Projection Using the same PROJ L operator, we allow the list L to contain arbitrary expressions involving attributes, for example: o Arithmetic on attributes, e.g., A+B. o Duplicate occurrences of the same attribute. o Example: Π A+B,A,A (R)

10 10 Aggregation Operators Aggregation operators are not operators of relational algebra. Rather, they apply to entire columns of a table and produce a single result. The most important examples: SUM, AVG, COUNT, MIN, and MAX.

11 11 Grouping Operator R1 := γ L (R2). o L is a list of elements that are either:  Individual (grouping ) attributes.  AGG(A), where AGG is one of the aggregation operators and A is an attribute. Example: StarsIn (title, year, starName) o Find the earliest year in which they appeared in movie for each star who has appeared at least three times  R1:= γ starName, MIN(year)  minYear, Count(title)  ctTitle (StarsIn)  R2 := SELECT ctTitle >= 3 (R1)  R3 := PROJECT minYear (R2)

12 12 Outerjoin Suppose we join R C S. A tuple of R that has no tuple of S with which it joins is said to be dangling. o Similarly for a tuple of S. Outerjoin preserves dangling tuples by padding them with a special NULL symbol in the result.


Download ppt "More Relation Operations 2014, Fall Pusan National University Ki-Joune Li."

Similar presentations


Ads by Google