Beruflich Dokumente
Kultur Dokumente
Lec13
Why Bags?
SQL, the most important query language for relational databases, is actually a bag language. Some operations, like projection, are much more efficient on bags than sets.
Operations on Bags
Selection applies to each tuple, so its effect on bags is like its effect on sets. Projection also applies to each tuple, but as a bag operator, we do not eliminate duplicates. Products and joins are done on each pair of tuples, so duplicates in bags have no effect on how we operate.
SELECTA+B<5 (R) = A 1
B 2
PROJECTA (R) =
R*S=
S.B 3 7 3 7
R.B<S.B
R.B 2 2 6
Bag 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} UNION {1,1,2,3,1} = {1,1,1,1,1,2,2,3}
Bag Intersection
An element appears in the intersection of two bags the minimum of the number of times it appears in either. Example: {1,2,1,1} INTER {1,2,1,3} = {1,1,2}.
Bag 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. Example: {1,2,1,1} {1,2,3} = {1,1}.
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.
1. 1.
(R) =
Sorting
R1 := L (R2). L is a list of some of the attributes of R2 . R1 is the list of tuples of R2 sorted first on the value of the first attribute on L, then on the second attribute of L, and so on. is the only operator whose result is neither a set nor a bag.
Example: Sorting
R= (A 1 3 5 B) 2 4 2 [(5,2), (1,2), (3,4)]
(R)
R=
A 1 3
B 2 4 = C 3 7 A1 1 3 A2 1 3
A+BC,AA1,AA2 (R)
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.
Example: Aggregation
R = ( A, 1 3 3 B) 3 4 2 SUM(A) = 7 COUNT(A) = 3 MAX(B) = 4 AVG(B) = 3
Grouping Operator
R1 := L (R2) L is a list of elements that are either: 1. Individual (grouping ) attributes. 2. AGG(A), where AGG is one of the aggregation operators and A is an attribute.
Applying L (R)
Group R according to all the grouping attributes on list L.
i.e., form one group for each distinct list of values for those attributes in R.
Within each group, compute AGG(A ) for each aggregation on list L. Result has one tuple for each group:
1. The grouping attributes and 2. Their groups aggregations.
Example: Grouping/Aggregation
R= A 1 4 1 B 2 5 2 (R) = ?? C 3 6 5 First, group R A B 1 2 1 2 4 5 : C 3 5 6
A,B,AVG(C)
Example: Grouping/Aggregation SpreePL (Sport, year, Player) For each player who has participated in at least three sports give the earliest year in which he or she participated . First we group, using Player as a grouping attribute. Then, we compute the MIN(year) for each group. Also, we need to compute the COUNT(Sport) aggregate for each group, for filtering out those Players with less than three Sports.
Player,minYear( ctSport 3( Player,MIN(year)minYear,COUNT(Sport)ctSport(
SpreePL)))
Outerjoin
Motivation Suppose we join R S. A tuple of R which doesn't join with any tuple of S is said to be dangling. Similarly for a tuple of S. Problem: We loose dangling tuples. Outerjoin Preserves dangling tuples by padding them with a special NULL symbol in the result.
Example: Outerjoin
R= A 1 4 B 2 5 S= B 2 6 C 3 7
(1,2) joins with (2,3), but the other two tuples are dangling. R S= A 1 4 NULL B 2 5 6 C 3 NULL 7
R S --- Full outerjoin pad dangling tuples from both the tables R and S R L S -- This left outerjoin: Only pad dangling tuples from the left table. R R S -- This right outerjoin: Only pad dangling tuples from the right table.
Relation borrower
customer_name loan_number Abhiram Kavitha Madhu L-170 L-230 L-155
Problem
Consider the relational schemas: Email (IDNO, email_id) Student (IDNO, Name, Hostel, Room) The EMAIL relation is maintained by ARC, where as the Student relation is maintained by SWD. It is required that the information in the two relations be combined into a new relation with schema: Student_email (IDNO, email_id, Name, Hostel, Room) It is observed that there are some IDNOs in the Student relation that are not in the Email relation. Write a Relational Algebra expression to create the new relation such that no IDNO from the Student relation is left out.