Presentation on theme: "1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which."— Presentation transcript:
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 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 What we will learn… uCore (or traditionally) relational algebra
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.
7 Set Operators uR U S: union, the set of elements that are in R or S or both. uR S: intersection, the set of elements that are in both R and S. uR - S: difference, the set of elements that are in R but no in S. uRequired wR and S must have schema with identical set of attributes, and wBefore calculation, the columns of R and S must be ordered.
8 RSRS A B C a1 b1 c1 a1 b2 c2 a2 b2 c1 a1 b3 c2 RSRS A B C a1 b2 c2 a2 b2 c1 R-S A B C a1 b1 c1 Ａ Ｂ Ｃ a1 b1 c1 a1 b2 c2 a2 b2 c1 R Ａ Ｂ Ｃ a1 b2 c2 a1 b3 c2 a2 b2 c1 S
9 Projection u L (R) wL is a list of attributes from the schema of R. wThe result is a new relation that has only some of R’s columns. wEliminate duplicate tuples, if any. uExample title, year, length (Movie)
10 Selection u C (R) wC is a condition (as in “if” statements) that refers to attributes of R. wThe result is a new relation with a subset of R’s tuples that satisfy C. uExample lentgh>=100 AND studioName = ‘Fox’ (Movie)
11 Cartesian Product uor just product uR S wPair each tuple t1 of R with each tuple t2 of S. wResult: a new relation with new tuples, each of them concatenation a pair of t1t2, the attributes of R and S are in ordered. wBut beware attribute A of the same name in R and S: use R.A and S.A.
13 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: R1 R2
14 Theta-Join uR C S wTake the product R x S. wThen apply C to the result. uC can be any boolean-valued condition. wHistoric versions of this operator allowed only A theta B, where theta was =, <, etc.
15 A B C a1 b1 5 a1 b2 6 a2 b3 8 a2 b4 12 R B E b1 3 b2 7 b3 10 b3 2 b5 2 S A R.B C S.B E a1 b1 5 b2 7 a1 b1 5 b3 10 a1 b2 6 b2 7 a1 b2 6 b3 10 a2 b3 8 b3 10 R C
"name": "15 A B C a1 b1 5 a1 b2 6 a2 b3 8 a2 b4 12 R B E b1 3 b2 7 b3 10 b3 2 b5 2 S A R.B C S.B E a1 b1 5 b2 7 a1 b1 5 b3 10 a1 b2 6 b2 7 a1 b2 6 b3 10 a2 b3 8 b3 10 R C
16 Outerjoin uSuppose we join R 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.
17 Example: Outerjoin R = ABS =BC 1223 4567 (1,2) joins with (2,3), but the other two tuples are dangling. R OUTERJOIN S =ABC 123 45NULL NULL67
18 Dependent and Independent Operations R C S = C (R x S) R S = L ( C (R x S)) R S = R – (R – S)
19 Combining Operations to Form Query uAlgebras allow us to express sequences of operations in a natural way. wExample: in arithmetic --- (x + 4)*(y - 3). uRelational algebra allows the same. For example title, year ( lentgh>=100 (Movie) studioName = ‘Fox’ (Movie))
20 Expressions uPrecedence of relational operators: 1.Unary operators --- select, project--- have highest precedence, bind first. 2.Then come products and joins. 3.Then intersection. 4.Finally, union and set difference bind last. wBut you can always insert parentheses to force the order you desire.
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 lentgh >= 100 Movie studioName = ‘Fox’ title, year For example: