Chapter2-algebra
Chapter2-algebra
Relational algebra
1
Contents
What is a data model?
Basics of the relational model
How to define?
How to query?
Constraints on relations
2
An algebraic query language
What is an “Algebra”?
Mathematical system consisting of:
Operands --- variables or values from
which new values can be constructed.
Operators --- symbols denoting
procedures that construct new values
from given values.
Eg. how many students in my
classroom? E= (x+y)
3
What is Relational Algebra?
An algebra whose operands are
relations or variables that represent
relations.
Operators are designed to do with
relations in a database.
The result is used as a query language
for relations.
how many students in my classroom?
(x union y)
4
Core Relational Algebra
5
Selection
R1 := σC (R2)
C is a condition (as in “if” statements)
that refers to attributes of R2.
R1 is all those tuples of R2 that
satisfy C.
6
Example: Selection
Relation Sells:
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
Sue’s Bud 2.50
Sue’s Miller 3.00
JoeMenu := σbar=‘Joe’s’(Sells):
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
7
Projection
R1 := πL (R2)
L is a list of attributes from the schema
of R2.
R1 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.
Eliminate duplicate tuples, if any.
8
Example: Projection
Relation Sells:
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
Sue’s Bud 2.50
Sue’s Miller 3.00
Prices := πbeer,price(Sells):
beer price
Bud 2.50
Miller 2.75
Miller 3.00 9
Extended Projection
10
Example: Extended Projection
R= (A B)
1 2
3 4
πA+B->C,A,A (R) = C A1 A2
3 1 1
7 3 3
11
Product (cross join)
R3 := R1 Χ R2
Pair each tuple t1 of R1 with each
tuple t2 of R2.
Concatenation t1t2 is a tuple of R3.
Schema of R3 is the attributes of R1
and then R2, in order.
If attribute A has the same name in
R1 and R2: use R1.A and R2.A.
12
Example: R3 := R1 Χ R2
13
Theta-Join
R3 := R1 ⋈C R2
Take the product R1 Χ R2.
Then apply σC to the result.
σ, C can be any boolean-valued
condition.
Historic versions of this operator
allowed only A B, where is =, <,
etc.; hence the name “theta-join.”
14
Example: Theta Join
Sells( bar, beer, price ) Bars( name, addr )
Joe’s Bud 2.50 Joe’s Maple St.
Joe’s Miller 2.75 Sue’s River Rd.
Sue’s Bud 2.50
Sue’s Coors 3.00
16
Example: Natural Join
A B C D A B F A B C D F
a1 b1 c1 d1
⋈
a1 b1 f1 = a1 b1 c1 d1 f1
a1 b1 c2 d2 a1 b2 f2 a1 b1 c2 d2 f1
18
Renaming
19
Example: Renaming
R( bar, addr )
Joe’s Maple St.
Sue’s River Rd.
20
Building Complex Expressions
21
Three notations:
1.Sequences of assignment
statements. :=
2.Expressions with several
operators.
3.Expression trees
22
Example: a Query
Bars(name, addr)
Sells(bar, beer, price)
Bar: =
лname (σaddress=‘Maper str.’ (Bars) ) U
ρ name ( лbar (σbeer=‘Bud’ and price<3 (Sells))
25
3. Expression Trees
∪
ρ R(name)
π name π bar
Bars Sells
27
Self Join
Sometimes, conditions and query
results are in the same table.
Recursion situation:
Parents (Parents, child) in DB
We want to know grandparents
information.
28
Example: Self-Join
Using Sells(bar, beer, price), find the
bars that sell two different beers at
the same price.
Joe’s Bud 2.5
Joe’s Coors 3.0
Joe’s Miller 2.5
Sue’s Bud 2.5
Sue’s Coors 3.5
Marry’s Miller 2.5
29
Example: Self-Join (cont.)
Strategy: by renaming, define a copy
of Sells, called S(bar, beer1, price).
π bar
σ
beer != beer1
⋈
ρ S(bar, beer1, price)
Sells Sells
31
Query expression: Пbar( σ beer <> beer1 (ρS(bar,beer1,price)(sells) ⋈ sells))
<> Change to <
Bar Beer1 price Bar Beer price
Joe’s Bud 2.5 Joe’s Bud 2.5
Joe’s Coors 3.0 Joe’s Coors 3.0
Joe’s Miller 2.5 ⋈ Joe’s Miller 2.5
Sue’s Bud 2.5 Sue’s Bud 2.5
Sue’s Coors 3.5 Sue’s Coors 3.5
Marry’s Miller 2.5
Marry’s Miller 2.5
35
Why Bags?
36
Operations on Bags
R( A B )
1 2
5 6
1 2
38
Example: Bag Projection
R( A B )
1 2
5 6
1 2
πA (R) = A
1
5
1
39
Example: Bag Product
R( A B ) S( B C )
1 2 3 4
5 6 7 8
1 2
R( A B ) S( B C )
1 2 3 4
5 6 7 8
1 2
42
Bag Intersection
43
Bag Difference
44
Beware: Bag Laws != Set Laws
47
Constraints on Relations
48
Two ways to express constraints
49
For example
Beers (name, manf)
Bars (name, addr, license)
Sells (bar, beer, price)
ftp://public.sjtu.edu.cn to public-files/upload/chapter2
User name: fli Password: public
Name of your homework is your studentID
53