Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Relation Operations

Similar presentations


Presentation on theme: "More Relation Operations"— Presentation transcript:

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

2 Relational Algebra on Bags
A bag is like a set, but an element may appear more than once. Multiset is another name for “bag.” 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. Example: {1,2,1} = {1,1,2} as bags, but [1,2,1] != [1,1,2] as lists.

3 Why Bags? SQL, the most important query language for relational databases is actually a bag language. 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. Projection: No more operation except deleting other attributes

4 Operations on Bags Selection: Projection Products and joins
Applies to each tuple, so its effect on bags is like its effect on sets. Projection As a bag operator, we do not eliminate duplicates. Products and joins 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) R S SELECTA+B<5 (R)
ProjectA(R) A B 1 2 5 6 B C 3 4 7 8 A B 1 2 A 1 5 R X S R R.B<S.B S A R.B S.B C 1 2 3 4 5 6 7 8 A R.B S.B C 1 2 3 4 7 8 5 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. 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. 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. But never less than 0 times. Example: {1,2,1} – {1,2,3} = {1}.

7 Beware: Bag Laws != Set Laws
Not all algebraic laws that hold for sets also hold for bags. 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. 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 The Extended Algebra δ: eliminate duplicates from bags.
τL: sort tuples. L is a list of attributes 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 Extended Projection Using the same Π L operator, we allow the list L to contain arbitrary expressions involving attributes, for example: Arithmetic on attributes, e.g., A+B. Duplicate occurrences of the same attribute. Example: ΠA+B,A,A (R)

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 Grouping Operator R1 := γ L (R2). Example
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) γ starName, MIN(year)minYear,Count(title)ctTitle (StarsIn) 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 Grouping Operator Title Year StarName Star Wars 1977 Carrie Fisher
Mark Hamill Harrison Ford Mighty Ducks 1991 Emilio Estevez Wayne’s World 1992 Dana Carvey My world Raiders of the Lost Ark 1981 Tiki's story StarName minYear ctTitle Carrie Fisher 1977 1 Harrison Ford 2 Emilio Estevez 1991 Dana Carvey 1992 Mark Hamill 3

13 δ (eliminating duplications) and γ (grouping)
δ is a special case of γ Suppose R(A1, A2, ..., An), then δ(R)= γA1, A2, ..., An (R ) but γA1, A2, ..., An (R ) = Π A1, A2, ..., An (R ), only if R is a set (not a bag)

14 Outerjoin Suppose we join R S.
A tuple of R that has no tuple of S with which it joins is said to be dangling. Similarly for a tuple of S. Outerjoin preserves dangling tuples by padding them with a special NULL symbol in the result. R S R S R S A B C 1 2 3 4 5 6 7 8 9 B C D 2 3 10 11 6 7 12 A B C D 1 2 3 10 11 4 5 6 7 8 9 12


Download ppt "More Relation Operations"

Similar presentations


Ads by Google