0% found this document useful (0 votes)
32 views62 pages

Database Lect6 Algebra

Relational algebra is a system used to manipulate relations in a database. It consists of operands like relations and variables, and operators that perform common operations on relations like selection, projection, union, and join. The core operators allow retrieving and combining data from one or more relations to form new relations.

Uploaded by

mennah samy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views62 pages

Database Lect6 Algebra

Relational algebra is a system used to manipulate relations in a database. It consists of operands like relations and variables, and operators that perform common operations on relations like selection, projection, union, and join. The core operators allow retrieving and combining data from one or more relations to form new relations.

Uploaded by

mennah samy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Relational Algebra

Database Systems

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

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

PROJECT requires a attribute list

Syntax:

<attribute list> (R)


Duplicates are not kept in result
result is a relation, which is a set
Projection
R1 := PROJL (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.

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 >

Price ( Description="Ice Cream" (STOREITEM))


SELECT result: < "I075", "Ice Cream", $1.49, false >

PROJECT result: < $1.49 >


Product
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
R2, in order.
But beware attribute A of the same name in
R1 and R2: use R1.A and R2.A.

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

BarInfo := Sells JOIN Sells.bar = Bars.name Bars

BarInfo( 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. 20
Natural Join
A frequent type of join connects two
relations by:
Equating attributes of the same name, and
Projecting out one copy of each pair of
equated attributes.
Called natural join.
Denoted R3 := R1 JOIN R2.

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

BarInfo := Sells JOIN Bars


Note Bars.name has become Bars.bar to make the natural
join “work.”
BarInfo( bar, beer, price, addr )
Joe’s Bud 2.50 Maple St.
Joe’s Milller 2.75 Maple St.
Sue’s Bud 2.50 River Rd.
Sue’s Coors 3.00 River Rd. 22
Renaming
The RENAME operator gives a new
schema to a relation.
R1 := RENAMER1(A1,…,An)(R2) makes
R1 be a relation with attributes
A1,…,An and the same tuples as
R2.
Simplified notation: R1(A1,…,An)
:= R2. 23
Example
Bars( name, addr )
Joe’s Maple St.
Sue’s River Rd.

R(bar, addr) := Bars

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

(B1, B2, …, Bn )(R) changes:


• the column (attribute) names only to B1, B1, …..Bn
RENAME (shorthand)
shorthand for renaming attributes :
R1   FNAME, LNAME, SALARY (DEP5_EMPS)

R1 has same attribute names as DEP5_EMPS

R2   (F,L,S) ( FNAME, LNAME, SALARY (DEP5_EMPS))

R2 has attributes renamed to F, L and S

R3 (F,L,S)   FNAME, LNAME, SALARY (DEP5_EMPS)

R3 also has attributes renamed to F, L and S


Building Complex Expressions
Algebras allow us to express
sequences of operations in a natural
way.
Example: in arithmetic --- (x + 4)*(y - 3).
Relational algebra allows the same.
Three notations, just as in arithmetic:
1. Sequences of assignment statements.
2. Expressions with several operators.
3. Expression trees.
27
Sequences of Assignments
Create temporary relation names.
Renaming can be implied by giving
relations a list of attributes.
Example: R3 := R1 JOINC R2 can be
written:
R4 := R1 * R2
R3 := SELECTC (R4)

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

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

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

R3 = <Description, Price> (R2)

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.

R1 = STORESTOCK ⋈<Item = ItemId> STOCKITEM


R2 = StoreId = "S002" (R1)
R3 = <Description, Price> (R2)

<Description, Price>(StoreId = "S002" (STORESTOCK ⋈<Item = ItemId> STOCKITEM))


UNION: example
STORESTOCK WAREHOUSESTOCK
StoreId Item Quantity StoreId Item Quantity
S002 I075 120 W998 I075 1200
S047 I333 267 W087 I001 5000
S002 I333 267 W222 I188 11500
W023 I075 300
STORESTOCK ⋃
WAREHOUSESTOCK
Id Item Quantity
S002 I075 120
S047 I333 267
S002 I333 267
W998 I075 1200
W087 I001 5000
W222 I188 11500
W023 I075 300
INTERSECTION: example
STORESTOCK WAREHOUSESTOCK
StoreId Item Quantity StoreId Item Quantity
S002 I075 120 W998 I075 1200
S047 I333 267 W087 I001 5000
S002 I333 267 W222 I188 11500
W023 I075 300

ItemID (STORESTOCK) ⋂ Item (WAREHOUSESTOCK)


Item
Item
I075 ⋂ I075
I001
= Item
I075
I333
I188
DIFFERENCE: 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 S002 I333 267

ItemID (STOCKITEM) - Item (STORESTOCK)


ItemId Item Item
I075
I345
- I075 = I345
I333
I333
Operator Precedence
The normal way to group operators is:
1. Unary operators , , and  have highest precedence.
2. Next highest are the “multiplicative” operators, , C ,
and .
3. Lowest are the “additive” operators, , , and —.
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.

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

SELECTaddr = “Maple St.” SELECT price<3 AND beer=“Bud”

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

RENAMES(bar, beer1, price)

Sells Sells
44
Query Trees

Query trees are representations of queries that can be


manipulated by query optimizers, according to
mathematical properties of the operators.
Schema-Defining Rules 1
For union, intersection, and difference,
the schemas of the two operands must
be the same, so use that schema for
the result.
Selection: schema of the result is the
same as the schema of the operand.
Projection: list of attributes tells us the
schema.

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

TAUB (R) = [(5,2), (1,2), (3,4)]

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:

1.First and last name of employees


who have no supervisor.
2.First and last name of employees
supervised by Franklin Wong.
3.Last name of employees who have dependents.
4.Last name of employees who have daughters.
5.Last name of employees in department 5 who
work more than 10 hours/week on ProductX.
6.Last name of supervisors of employees
in department 5 who work more than
10 hours/week on ProductX.
EXERCISE 1: Schema
EXERCISE 2: Queries
1. First and last names of all department managers.

2. Salaries of all employees who have worked on the


Reorganization project.

3. SSN of all employees who have worked on a project


that is controlled by a department different
than the department that they are assigned to.

4. Last name of all employees who are not married.


Exercise 3: Schema
EXERCISE 3: Queries
1.List all airplane types that can land
at any airport in San Francisco.
2.List the ids and number of seats for all airplanes
that can land at any airport in Chicago.
3.List the name and phone number of all customers
with a seat reserved on a flight
that leaves Chicago O’Hara airport (ORD)
on October 31, 2008.
4.List all airlines that have seats available for flights
leaving Los Angeles (LAX) on September 25, 2008.
5.List all airlines that operate
at San Jose International Airport (SJC).
EXERCISE 4:
Schema
EXERCISE 4: Queries
1.Count the number of overdue books.
2.How many books by author Harry Crews are in
the
database?
3.Determine the number of library cards assigned
to each borrower phone number.
4.Find names of all borrowers
who do not have any book loans.
5.Do any library branches have every book?

You might also like