relational algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©fall, 2001, lrx #09...
TRANSCRIPT
![Page 1: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/1.jpg)
#09 Relational Algebra 216©Fall, 2001, LRX HUST,Wuhan,China
Relational Algebra
Lecture #9Autumn, 2001
![Page 2: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/2.jpg)
#09 Relational Algebra 217©Fall, 2001, LRX HUST,Wuhan,China
Relational Algebra
l A small set of operators that allow us to manipulate relations in limited, but easilyimplementable and useful ways. The operators are:
l 1. Union, intersection, and difference: the usual set operators.» But the relation schemas must be the same.
![Page 3: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/3.jpg)
#09 Relational Algebra 218©Fall, 2001, LRX HUST,Wuhan,China
Relational Algebra (II)
l 2. Selection: Picking certain rows from a relation.
l 3. Projection: Picking certain columns.l 4. Products and joins: Composing relations
in useful ways.l 5. Renaming of relations and their
attributes.
![Page 4: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/4.jpg)
#09 Relational Algebra 219©Fall, 2001, LRX HUST,Wuhan,China
Selection (σ)
l R1 = σC(R2)where C is a condition involving the attributes of relation R2.
![Page 5: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/5.jpg)
#09 Relational Algebra 220©Fall, 2001, LRX HUST,Wuhan,China
Example
Relation Sellsbar beer price
Joe's Bud 2.50Joe's Miller 2.75Sue's Bud 2.50Sue's Coors 3.00
JoeMenu = σbar=Joe's(Sells)bar beer price
Joe's Bud 2.50Joe's Miller 2.75
![Page 6: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/6.jpg)
#09 Relational Algebra 221©Fall, 2001, LRX HUST,Wuhan,China
Projection (π)
l R1 = πL(R2)where L is a list of attributes from the schema of R2.
![Page 7: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/7.jpg)
#09 Relational Algebra 222©Fall, 2001, LRX HUST,Wuhan,China
Example
πbeer;price(Sells)beer price
Bud 2.50Miller 2.75Coors 3.00
Notice elimination of duplicate tuples.
![Page 8: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/8.jpg)
#09 Relational Algebra 223©Fall, 2001, LRX HUST,Wuhan,China
Product (×)
l R = R1×R2l pairs each tuple t1 of R1 with each tuple t2
of R2 and puts in R a tuple t1t2.
![Page 9: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/9.jpg)
#09 Relational Algebra 224©Fall, 2001, LRX HUST,Wuhan,China
Theta-Join (∞)C
l R = R1 ∞ R2C
is equivalent to R = σC(R1×R2).
![Page 10: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/10.jpg)
#09 Relational Algebra 225©Fall, 2001, LRX HUST,Wuhan,China
ExampleSells=
bar beer price
Joe's Bud 2.50Joe's Millers 2.75Sue's Bud 2.50Sue's Coors 3.00
Bars=name addr
Joe's Maple St.Sue's River Rd.
![Page 11: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/11.jpg)
#09 Relational Algebra 226©Fall, 2001, LRX HUST,Wuhan,China
Example (II)BarInfo = ∞
Sells Sells:Bar=Bar:Name Bars
bar beer price name addr
Joe's Bud 2.50 Joe's Maple St..Joe's Miller 2.75 Joe's Maple St..Sue's Bud 2.50 Sue's River Rd.Sue's Coors 3.00 Sue's River Rd.
![Page 12: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/12.jpg)
#09 Relational Algebra 227©Fall, 2001, LRX HUST,Wuhan,China
Natural Join (∞ )
l R = R1 ∞ R2calls for the theta-join of R1 and R2 with the condition that all attributes of the same name be equated. Then, one column for each pair of equated attributes is projected out.
![Page 13: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/13.jpg)
#09 Relational Algebra 228©Fall, 2001, LRX HUST,Wuhan,China
Example
l Suppose the attribute name in relation Bars was changed to bar, to match the bar name in Sells.
![Page 14: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/14.jpg)
#09 Relational Algebra 229©Fall, 2001, LRX HUST,Wuhan,China
Example (II)BarInfo = Sells ∞ Bars
bar beer price addr
Joe's Bud 2.50 Maple St.Joe's Miller 2.75 Maple St.Sue's Bud 2.50 River Rd.Sue's Coors 3.00 River Rd.
![Page 15: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/15.jpg)
#09 Relational Algebra 230©Fall, 2001, LRX HUST,Wuhan,China
Example (II)
BarInfo = Sells ∞ Bar
bar beer price addr
Joe's Bud 2.50 Maple St..Joe's Miller 2.75 Maple St..Sue's Bud 2.50 River Rd.Sue's Coors 3.00 River Rd.
![Page 16: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/16.jpg)
#09 Relational Algebra 231©Fall, 2001, LRX HUST,Wuhan,China
Renaming (ρ)
l ρS(A1,… ,An ) (R) produces a relation identical to R but named S and with attributes, in order, named A1,… ,An.
![Page 17: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/17.jpg)
#09 Relational Algebra 232©Fall, 2001, LRX HUST,Wuhan,China
ExampleBars=
name addr
Joe's Maple St.Sue's River Rd.
ρR(bar;addr)(Bars)=bar addr
Joe's Maple St.Sue's River Rd.
The name of the above relation is R.
![Page 18: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/18.jpg)
#09 Relational Algebra 233©Fall, 2001, LRX HUST,Wuhan,China
Combining Operationsl Algebra =
» 1. Basis arguments,» 2. Ways of constructing expressions.
l For relational algebra:» 1. Arguments = variables standing for relations + finite,
constant relations.» 2. Expressions constructed by applying one of the
operators + parentheses.» Query = expression of relational algebra.
![Page 19: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/19.jpg)
#09 Relational Algebra 234©Fall, 2001, LRX HUST,Wuhan,China
Operators
l σ Selectionl ρ Renamingl π Projectionl ∞ Natural joinl ∞ Theta-join
Cl ∪ Unionl ∩ Intersectionl ― Difference
![Page 20: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/20.jpg)
#09 Relational Algebra 235©Fall, 2001, LRX HUST,Wuhan,China
Operator Precedence
l The normal way to group operators is:» 1. Unary operators σ, ρ, π and have highest
precedence.» 2. Next highest are the "multiplicative"
operators, ∞ ∞ , and ×.C
» 3. Lowest are the "additive" operators, ∪, ∩, and ― .
![Page 21: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/21.jpg)
#09 Relational Algebra 236©Fall, 2001, LRX HUST,Wuhan,China
Operator Precedence(II)
l But there is no universal agreement, so we always put parentheses around the argument of a unary operator, and it is a good idea to group all binary operators with parentheses enclosing their arguments.
l Example» Group R ∪ σ S ∞ T as R ∪ (σ(S) ∞ T).
![Page 22: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/22.jpg)
#09 Relational Algebra 237©Fall, 2001, LRX HUST,Wuhan,China
Each Expression Needs a Schema
l If ∪, ∩, and ― applied, schemas are the same, so use this schema.
l Projection: use the attributes listed in the projection.
l Selection: no change in schema.
![Page 23: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/23.jpg)
#09 Relational Algebra 238©Fall, 2001, LRX HUST,Wuhan,China
Each Expression Needs a Schema (II)
l Product R×S: use attributes of R and S.» But if they share an attribute A, prefix it with the
relation name, as R.A, S.A.l Theta-join: same as product.l Natural join: use attributes from each
relation; common attributes are merged anyway.
l Renaming: whatever it says.
![Page 24: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/24.jpg)
#09 Relational Algebra 239©Fall, 2001, LRX HUST,Wuhan,China
Example
l Find the bars that are either on Maple Street or sell Bud for less than $3.
Sells(bar, beer, price)Bars(name, addr)
![Page 25: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/25.jpg)
#09 Relational Algebra 240©Fall, 2001, LRX HUST,Wuhan,China
Example (II)
∪
ρR(name)
πname π bar
σaddr = ‘Maple St.’ σprice < $3.00 AND beer = ‘Bud’
Bars Sells
![Page 26: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/26.jpg)
#09 Relational Algebra 241©Fall, 2001, LRX HUST,Wuhan,China
Example
l Find the bars that sell two different beers at the same price.
![Page 27: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/27.jpg)
#09 Relational Algebra 242©Fall, 2001, LRX HUST,Wuhan,China
Example(II)
Sells(bar, beer, price)πbar
σbeer<>beer1
∞
ρS(bar, beer1, price)
Sells Sells
![Page 28: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/28.jpg)
#09 Relational Algebra 243©Fall, 2001, LRX HUST,Wuhan,China
Linear Notation for Expressions
l Invent new names for intermediate relations, and assign them values that are algebraic expressions.
l Renaming of attributes implicit in schema of new relation.
![Page 29: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/29.jpg)
#09 Relational Algebra 244©Fall, 2001, LRX HUST,Wuhan,China
Example
l Find the bars that are either on Maple Street or sell Bud for less than $3.» Sells(bar, beer, price)» Bars(name, addr)» R1(bar) := πname(σaddr=‘Maple St’(Bars))» R2(bar) := πbar(σbeer=‘Bud’AND price<$3 (Sells))» R3(bar) := R1 ∪ R2
![Page 30: Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09 Relational Algebra HUST,Wuhan,China 217 Relational Algebra l A small set of operators](https://reader030.vdocuments.site/reader030/viewer/2022041002/5ea3b07781e51202d1108c2d/html5/thumbnails/30.jpg)
#09 Relational Algebra 245©Fall, 2001, LRX HUST,Wuhan,China
Example
l Find the bars that sell two different beers at the same price.» Sells(bar, beer, price)» S1(bar,beer1,price) := Sells» S2(bar,beer,price,beer1) := S1 ∞ Sells» S3(bar) = πbar(σbeer<> beer1(S2))