Download presentation

Presentation is loading. Please wait.

Published byMckenna Wayne Modified over 3 years ago

1
1 Relational Algebra Operators Expression Trees Bag Model of Data Source: Slides by Jeffrey Ullman

2
2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which new values can be constructed. wOperators --- symbols denoting procedures that construct new values from given values.

3
3 What is Relational Algebra? uAn algebra whose operands are relations or variables that represent relations. uOperators are designed to do the most common things that we need to do with relations in a database. wThe result is an algebra that can be used as a query language for relations.

4
4 Roadmap uThere is a core relational algebra that has traditionally been thought of as the relational algebra. uBut there are several other operators we shall add to the core in order to model better the language SQL --- the principal language used in relational database systems.

5
5 Core Relational Algebra uUnion, intersection, and difference. wUsual set operations, but require both operands have the same relation schema. uSelection: picking certain rows. uProjection: picking certain columns. uProducts and joins: compositions of relations. uRenaming of relations and attributes.

6
6 Selection uR1 := SELECT C (R2) wC is a condition (as in “if” statements) that refers to attributes of R2. wR1 is all those tuples of R2 that satisfy C.

7
7 Example Relation Sells: storecandyprice 7-11Twizzlers2.50 7-11Kitkat2.75 KrogerTwizzlers2.50 KrogerKitkat3.00 7-11Menu := SELECT store=“7-11” (Sells): storecandyprice 7-11Twizzlers2.50 7-11Kitkat2.75

8
8 Projection uR1 := PROJ L (R2) wL is a list of attributes from the schema of R2. wR1 is constructed by looking at each tuple of R2, extracting the attributes on list L, in the order specified, and creating from those components a tuple for R1. wEliminate duplicate tuples, if any.

9
9 Example Relation Sells: storecandyprice 7-11Twizzlers2.50 7-11Kitkat2.75 KrogerTwizzlers2.50 KrogerKitkat3.00 Prices := PROJ candy,price (Sells): candyprice Twizzlers2.50 Kitkat2.75 Kitkat3.00

10
10 Product uR3 := R1 * R2 wPair each tuple t1 of R1 with each tuple t2 of R2. wConcatenation t1t2 is a tuple of R3. wSchema of R3 is the attributes of R1 and then R2, in order. wBut beware attribute A of the same name in R1 and R2: use R1.A and R2.A.

11
11 Example: R3 := R1 * R2 R1(A,B ) 12 34 R2(B,C ) 56 78 910 R3(A,R1.B,R2.B,C ) 1256 1278 12910 3456 3478 34910

12
12 Theta-Join uR3 := R1 JOIN C R2 wTake the product R1 * R2. wThen apply SELECT C to the result. uAs for SELECT, C can be any boolean- valued condition. wHistoric versions of this operator allowed only A B, where is =, <, etc.; hence the name “theta-join.”

13
13 Example Sells(store,candy,price ) Stores(name, addr ) 7-11Twiz.2.507-11 Maple St. 7-11Kitkat2.75Kroger River Rd. KrogerTwiz.2.50 KrogerPez3.00 StoreInfo := Sells JOIN Sells.store = Stores.name Stores StoreInfo(store,candy,price,name,addr ) 7-11Twiz.2.507-11Maple St. 7-11Kitkat2.757-11Maple St. KrogerTwiz.2.50KrogerRiver Rd. KrogerPez3.00KrogerRiver Rd.

14
14 Natural Join uA frequent type of join connects two relations by: wEquating attributes of the same name, and wProjecting out one copy of each pair of equated attributes. uCalled natural join. uDenoted R3 := R1 JOIN R2.

15
15 Example Sells(store,candy,price )Stores(store,addr ) 7-11Twiz.2.507-11Maple St. 7-11Kitkat2.75KrogerRiver Rd. KrogerTwiz.2.50 KrogerPez3.00 StoreInfo := Sells JOIN Stores Note Stores.name has become Stores.store to make the natural join “work.” StoreInfo(store,candy,price,addr ) 7-11Twiz.2.50Maple St. 7-11Kitkat2.75Maple St. KrogerTwiz.2.50River Rd. KrogerPez3.00River Rd.

16
16 Renaming uThe RENAME operator gives a new schema to a relation. uR1 := RENAME R1(A1,…,An) (R2) makes R1 be a relation with attributes A1,…,An and the same tuples as R2. uSimplified notation: R1(A1,…,An) := R2.

17
17 Example Stores(name, addr ) 7-11Maple St. KrogerRiver Rd. R(store, addr ) 7-11Maple St. KrogerRiver Rd. R(store, addr) := Stores

18
18 Building Complex Expressions uCombine operators with parentheses and precedence rules. uThree notations, just as in arithmetic: wSequences of assignment statements. wExpressions with several operators. wExpression trees.

19
19 Sequences of Assignments uCreate temporary relation names. uRenaming can be implied by giving relations a list of attributes. uExample: R3 := R1 JOIN C R2 can be written: R4 := R1 * R2 R3 := SELECT C (R4)

20
20 Expressions in a Single Assignment uExample: the theta-join R3 := R1 JOIN C R2 can be written R3 := SELECT C (R1 * R2) uPrecedence of relational operators: w[SELECT, PROJECT, RENAME] (highest). w[PRODUCT, JOIN]. wINTERSECTION. w[UNION, --]

21
21 Expression Trees uLeaves are operands --- either variables standing for relations or particular, constant relations. uInterior nodes are operators, applied to their child or children.

22
22 Example uUsing the relations Stores(name, addr) and Sells(store, candy, price), find the names of all the stores that are either on Maple St. or sell Twizzlers for less than $3.

23
23 As a Tree: Stores(name,addr)Sells(store,candy,price) SELECT addr = “Maple St.” SELECT price<3 AND candy=“Twiz.” PROJECT name RENAME R(name) PROJECT store UNION

24
24 As a Sequence of Assignments R1(name) := PROJECT name (SELECT addr="Maple" (Stores)) R2(name) := PROJECT store (SELECT price<3 AND candy="Twiz." (Sells)) R3 := R1 UNION R2

25
25 Example uUsing Sells(store, candy, price), find the stores that sell two different candies at the same price. uStrategy: by renaming, define a copy of Sells, called S(store, candy1, price). The natural join of Sells and S consists of quadruples (store, candy, candy1, price) such that the store sells both candies at this price.

26
26 The Tree Sells(store,candy,price) RENAME S(store, candy1, price) JOIN PROJECT store SELECT candy != candy1

27
27 Schemas for Results uUnion, intersection, and difference: the schemas of the two operands must be the same, so use that schema for the result. uSelection: schema of the result is the same as the schema of the operand. uProjection: list of attributes tells us the schema.

28
28 Schemas for Results --- (2) uProduct: schema is the attributes of both relations. wUse R.A, etc., to distinguish two attributes named A. uTheta-join: same as product. uNatural join: union of the attributes of the two relations. uRenaming: the operator tells the schema.

29
29 Relational Algebra on Bags uA bag (or multiset ) is like a set, but an element may appear more than once. uExample: {1,2,1,3} is a bag. uExample: {1,2,3} is also a bag that happens to be a set.

30
30 Why Bags? uSQL, the most important query language for relational databases, is actually a bag language. uSome operations, like projection, are much more efficient on bags than sets.

31
31 Operations on Bags uSelection applies to each tuple, so its effect on bags is like its effect on sets. uProjection also applies to each tuple, but as a bag operator, we do not eliminate duplicates. uProducts and joins are done on each pair of tuples, so duplicates in bags have no effect on how we operate.

32
32 Example: Bag Selection R(A,B ) 12 56 12 SELECT A+B<5 (R) =AB 12

33
33 Example: Bag Projection R(A,B ) 12 56 12 PROJECT A (R) =A 1 5 1

34
34 Example: Bag Product R(A,B )S(B,C ) 1234 5678 12 R * S =AR.BS.BC 1234 1278 5634 5678 1234 1278

35
35 Example: Bag Theta-Join R(A,B )S(B,C ) 1234 5678 12 R JOIN R.B

36
36 Bag Union uAn element appears in the union of two bags the sum of the number of times it appears in each bag. uExample: {1,2,1} UNION {1,1,2,3,1} = {1,1,1,1,1,2,2,3}

37
37 Bag Intersection uAn element appears in the intersection of two bags the minimum of the number of times it appears in either. uExample: {1,2,1,1} INTER {1,2,1,3} = {1,1,2}.

38
38 Bag Difference uAn 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. wBut never less than 0 times. uExample: {1,2,1,1} – {1,2,3} = {1,1}.

39
39 Beware: Bag Laws != Set Laws uSome, but not all algebraic laws that hold for sets also hold for bags. uExample: the commutative law for union (R UNION S = S UNION R ) does hold for bags. wSince addition is commutative, adding the number of times x appears in R and S doesn’t depend on the order of R and S.

40
40 Example of the Difference uSet union is idempotent, meaning that S UNION S = S. uHowever, for bags, if x appears n times in S, then it appears 2n times in S UNION S. uThus S UNION S != S in general.

41
41 The Extended Algebra uDELTA = eliminate duplicates from bags. uTAU = sort tuples. uExtended projection : arithmetic, duplication of columns. uGAMMA = grouping and aggregation. uOuterjoin : avoids “dangling tuples” = tuples that do not join with anything.

42
42 Duplicate Elimination uR1 := DELTA(R2). uR1 consists of one copy of each tuple that appears in R2 one or more times.

43
43 Example: Duplicate Elimination R = (AB ) 12 34 12 DELTA(R) =AB 12 34

44
44 Sorting uR1 := TAU L (R2). wL is a list of some of the attributes of R2. uR1 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. wBreak ties arbitrarily. uTAU is the only operator whose result is neither a set nor a bag.

45
45 Example: Sorting R = ( AB ) 12 34 52 TAU B (R) =[(5,2), (1,2), (3,4)]

46
46 Extended Projection uUsing the same PROJ L operator, we allow the list L to contain arbitrary expressions involving attributes, for example: wArithmetic on attributes, e.g., A+B. wDuplicate occurrences of the same attribute.

47
47 Example: Extended Projection R = ( AB ) 12 34 PROJ A+B,A,A (R) =A+BA1A2 311 733

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

49
49 Example: Aggregation R = ( AB ) 13 34 32 SUM(A) = 7 COUNT(A) = 3 MAX(B) = 4 AVG(B) = 3

50
50 Grouping uSometimes we want to apply an aggregation operator to a subset (or group) of tuples, instead of all of them. uSo we need a way to "group" them, e.g. ABC 111111 232232 345345 2222 3232 4343 group by A: ABC 111111 223223 354354 2222 2323 3434 group by A and B:

51
51 Grouping Operator uR1 := GAMMA L (R2). L is a list of elements, each of which is either: wan individual (grouping ) attribute or wAGG(A ), where AGG is one of the aggregation operators and A is an attribute.

52
52 Applying GAMMA L (R) uGroup R according to all the grouping attributes on list L. wThat is: form one group for each distinct list of values for those attributes in R. uWithin each group, compute AGG(A ) for each aggregation on list L. uResulting relation has one tuple for each group; this tuple contains wthe values of the grouping attributes for the group wthe group’s aggregation values.

53
53 Example: Grouping/Aggregation R = ( ABC ) 123 456 125 GAMMA A,B,AVG(C) (R) = ?? First, group R by A and B : ABC 123 125 456 Then, average C within groups: ABAVG(C) 12 4 45 6

54
54 Example: Grouping/Aggregation Sells( store, candy, price ) 7-11 Kroger HEB Twiz. Kitkat Twiz. Pez Kitkat 2.00 2.75 2.50 3.00 3.25 compute GAMMA candy,AVG(price) (Sells) : storecandyprice 7-11 Kroger Twiz. 2.00 2.50 7-11 HEB Kitkat 2.75 3.25 KrogerPez3.00 candyAVG(price) Twiz. Kitkat Pez 2.25 3.00

55
55 Outerjoin uSuppose we join R JOIN C S. uA tuple of R that has no tuple of S with which it joins is said to be dangling. wSimilarly for a tuple of S. uOuterjoin preserves dangling tuples by padding them with a special NULL symbol in the result.

56
56 Example: Outerjoin R = ( AB )S = ( BC ) 1223 4567 (1,2) joins with (2,3), but the other two tuples are dangling. R OUTERJOIN S =ABC 123 45NULL NULL67

Similar presentations

OK

Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.

Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on international business law File type ppt on cybercrime convention Ppt on any one mathematician john Ppt on chemical reaction Ppt on telephone exchange in indian railways Ppt on cost centre accounting Ppt on japanese culture Ppt on resistance temperature detector failures Ppt on business planning Ppt on chromosomes and genes model