Database Lect6 Algebra
Database Lect6 Algebra
Database Systems
1
What is an “Algebra”
2
What is Relational Algebra?
An algebra whose operands are
relations or variables that represent
relations.
Operators are designed to do the most
common things that we need to do with
relations in a database.
The result is an algebra that can be used
as a query language for relations.
3
Core Relational Algebra
Union, intersection, and difference.
Usual set operations, but require both
operands have the same relation schema.
Selection: picking certain rows.
Projection: picking certain columns.
Products and joins: compositions of
relations.
Renaming of relations and attributes.
4
Relational Algebra
Procedural language
Six basic operators
select:
project:
union:
set difference: –
Cartesian product: x
rename:
The operators take one or two
relations as inputs and produce a new
relation as a result.
The SELECT Operation
SELECT extracts tuples from a relation
result has same relation schema as operand
SELECT requires a selection condition
selection condition is a boolean expression
to filter tuple values
Syntax:
<selection condition> (R)
Selection condition may contain
AND, OR, NOT, =, <, , >, ,
Selection
R1 := SELECTC (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.
7
Example
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 := SELECTbar=“Joe’s”(Sells):
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
8
SELECT Examples
< "S002", "I065", 120 >,
r2(STORESTOCK) = < "S333", "I954", 198 >,
< "S047", "I099", 267 >,
< "S047", "I954", 300 >
StoreId = "S047" (STORESTOCK)
< "S047", "I099", 267 >,
< "S047", "I954", 300 >
quantity < 200 (STORESTOCK)
< "S002", "I065", 120 >,
< "S333", "I954", 198 >
The PROJECT Operation
PROJECT extracts attributes from a relation
result schema attributes are a subset
of the operand schema
Syntax:
11
Example
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 := PROJbeer,price(Sells):
beer price
Bud 2.50
Miller 2.75
Miller 3.00
12
Composing Operations
r(STOCKITEM) =
< "I075", "Ice Cream", $1.49, false >,
< "I345", "Cupcakes", $1.99, false >,
< "I333", "Twinkies", $1.98, false >
14
Example: R3 := R1 * R2
R1( A, B) R3( A, R1.B, R2.B, C )
1 2 1 2 5 6
3 4 1 2 7 8
1 2 9 10
R2( B, C ) 3 4 5 6
5 6 3 4 7 8
7 8 3 4 9 10
9 10
15
CROSS PRODUCT: example
STOCKITEM STORESTOCK
ItemId Description Price Taxable StoreId Item Quantity
I075 Ice Cream $1.49 FALSE S002 I075 120
I345 Cup Cakes $1.99 FALSE S047 I333 267
I333 Twinkies $1.98 FALSE
STOCKITEM STORESTOCK
ItemId Description Price Taxable StoreId Item Quantity
I075 Ice Cream $1.49 FALSE S002 I075 120
I345 Cup Cakes $1.99 FALSE S002 I075 120
I333 Twinkies $1.98 FALSE S002 I075 120
I075 Ice Cream $1.49 FALSE S047 I333 267
I345 Cup Cakes $1.99 FALSE S047 I333 267
I333 Twinkies $1.98 FALSE S047 I333 267
JOIN = XPROD and SELECT
STORESTOCK ⋈ <Item = ItemId> STOCKITEM
ItemId Description Price
Taxable StoreId Item Quantity
I075 Ice Cream $1.49
FALSE S002 I075 120
I333 Twinkies $1.98
FALSE S047 I333 267
ItemId=Item (STOCKITEM STORESTOCK)
ItemId Description Price Taxable StoreId Item Quantity
I075 Ice Cream $1.49 FALSE S002 I075 120
I345 Cup Cakes $1.99 FALSE S002 I075 120
I333 Twinkies $1.98 FALSE S002 I075 120
I075 Ice Cream $1.49 FALSE S047 I333 267
I345 Cup Cakes $1.99 FALSE S047 I333 267
I333 Twinkies $1.98 FALSE S047 I333 267
The JOIN Operation
JOIN combines tuples from two tables based
on values of related attributes (usually a FK)
JOIN requires a join condition
boolean expression comparing attributes from each
operand
Syntax:
R⋈ <join condition> S
The join condition may contain
AND, =, <, , >, ,
Theta-Join
R3 := R1 JOINC R2
Take the product R1 * R2.
Then apply SELECTC to the result.
As for SELECT, C can be any boolean-
valued condition.
Historic versions of this operator allowed
only A theta B, where theta was =, <, etc.;
hence the name “theta-join.”
19
Example
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
21
Example
Sells( bar, beer, price ) Bars( bar, 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
R( bar, addr )
Joe’s Maple St.
Sue’s River Rd.
24
RENAME
Rename the attributes of a relation
or change the relation name
The general RENAME operation :
S (B1, B2, …, Bn )(R) changes both:
• the relation name to S, and
• the column (attribute) names to B1, B1, …..Bn
S(R) changes:
• the relation name only to S
28
Writing Queries
STORESTOCK
StoreId Item Quantity Query:
S002 I075 120 Get the Description and Price
S047 I333 267 for all Items stocked
S002 I333 1200 by Store S002
STOCKITEM
ItemId Description Price Taxable result only
I075 Ice Cream $1.49 FALSE includes tuples
I345 Cup Cakes $1.99 FALSE with
I333 Twinkies $1.98 FALSE certain ItemIds
result has these attributes
Writing Queries
STORESTOCK Query:
StoreId Item Quantity Get the Description and Price
S002 I075 120 for all Items stocked
S047 I333 267 by Store S002
We need a join
S002 I333 1200
to merge data
STOCKITEM across relations
ItemId Description Price Taxable
I075 Ice Cream $1.49 FALSE
I345 Cup Cakes $1.99 FALSE
I333 Twinkies $1.98 FALSE
Writing Queries
STORESTOCK ⋈<Item = ItemId> STOCKITEM
Query:
Get the Description and Price
for all Items stocked Now we can select and project
by Store S002 to extract the information we want
Writing Queries
R1 = STORESTOCK ⋈<Item = ItemId> STOCKITEM
StoreId Item Quantity ItemId Description Price Taxable
S002 I075 120 I075 Ice Cream $1.49 FALSE
S047 I333 267 I333 Twinkies $1.98 FALSE
S002 I333 1200 I333 Twinkies $1.98 FALSE
R2 = StoreId = "S002" (R1)
StoreId Item Quantity ItemId Description Price Taxable
S002 I075 120 I075 Ice Cream $1.49 FALSE
S002 I333 1200 I333 Twinkies $1.98 FALSE
Writing Queries
R2 = StoreId = "S002" (R1)
StoreId Item Quantity ItemId Description Price Taxable
S002 I075 120 I075 Ice Cream $1.49 FALSE
S002 I333 1200 I333 Twinkies $1.98 FALSE
Description Price
Ice Cream $1.49
Twinkies $1.98
Composing Queries
Since the operands and results of every query are
relations, we can compose or chain queries.
Example
Group R S T as R ((S ) T ).
Expression Trees
Leaves are operands --- either variables
standing for relations or particular,
constant relations.
Interior nodes are operators, applied to
their child or children.
40
Example
Using the relations Bars(name, addr)
and Sells(bar, beer, price), find the
names of all the bars that are either on
Maple St. or sell Bud for less than $3.
41
As a Tree:
UNION
RENAMER(name)
PROJECTname PROJECTbar
Bars Sells
42
Example
Using Sells(bar, beer, price), find the bars
that sell two different beers at the same
price.
Strategy: by renaming, define a copy of
Sells, called S(bar, beer1, price). The
natural join of Sells and S consists of
quadruples (bar, beer, beer1, price) such
that the bar sells both beers at this price.
43
The Tree
PROJECTbar
SELECTbeer != beer1
JOIN
Sells Sells
44
Query Trees
46
Schema-Defining Rules 2
Product: the schema is the attributes of
both relations.
Use R.A, etc., to distinguish two attributes
named A.
Theta-join: same as product.
Natural join: use attributes of both
relations.
Shared attribute names are merged.
Renaming: the operator tells the schema.
47
Duplicate Elimination
R1 := DELTA(R2).
R1 consists of one copy of each tuple
that appears in R2 one or more times.
48
Example: Duplicate Elimination
R= A B
1 2
3 4
1 2
DELTA(R) = A B
1 2
3 4
49
Sorting
R1 := TAUL (R2).
L is a list of some of the attributes of R2.
R1 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.
Break ties arbitrarily.
TAU is the only operator whose result is
neither a set nor a bag.
50
Example: Sorting
R= A B
1 2
3 4
5 2
51
Aggregation Operators
Aggregation operators are not
operators of relational algebra.
Rather, they apply to entire columns
of a table and produce a single
result.
The most important examples:
SUM, AVG, COUNT, MIN, and MAX.
52
Example: Aggregation
R= A B
1 3
3 4
3 2
SUM(A) = 7
COUNT(A) = 3
MAX(B) = 4
AVG(B) = 3
53
Grouping Operator
R1 := GAMMAL (R2). L is a list of
elements that are either:
1. Individual (grouping ) attributes.
2. AGG(A ), where AGG is one of the
aggregation operators and A is an
attribute.
54
Example: Grouping/Aggregation
R= A B C
1 2 3 Then, average C within
4 5 6 groups:
1 2 5
A B AVG(C)
GAMMAA,B,AVG(C) (R) = ?? 1 2 4
4 5 6
First, group R:
A B C
1 2 3
1 2 5
4 5 6 55
EXERCISE 1: Queries
Express the following queries in the algebra: