Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09...

Post on 23-Apr-2020

5 views 0 download

Transcript of Relational Algebraidc.hust.edu.cn/~rxli/teaching/database/slides/09... · ©Fall, 2001, LRX #09...

#09 Relational Algebra 216©Fall, 2001, LRX HUST,Wuhan,China

Relational Algebra

Lecture #9Autumn, 2001

#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.

#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.

#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.

#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

#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.

#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.

#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.

#09 Relational Algebra 224©Fall, 2001, LRX HUST,Wuhan,China

Theta-Join (∞)C

l R = R1 ∞ R2C

is equivalent to R = σC(R1×R2).

#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.

#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.

#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.

#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.

#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.

#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.

#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.

#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.

#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.

#09 Relational Algebra 234©Fall, 2001, LRX HUST,Wuhan,China

Operators

l σ Selectionl ρ Renamingl π Projectionl ∞ Natural joinl ∞ Theta-join

Cl ∪ Unionl ∩ Intersectionl ― Difference

#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 ― .

#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).

#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.

#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.

#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)

#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

#09 Relational Algebra 241©Fall, 2001, LRX HUST,Wuhan,China

Example

l Find the bars that sell two different beers at the same price.

#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

#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.

#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

#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))