









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Relational Algebra
— 1
CSC343 Introduction to Databases
—
University of Toronto
Relational Algebra
— 2
CSC343 Introduction to Databases
—
University of Toronto
Operations on databases:
Queries
— read data from the database;
Updates
— change the content of the database.
In this lecture unit we discuss the relational algebra,a procedural language that defines databaseoperations in terms of algebraic expressions.
[The Relational Calculus is a declarative languagefor database operations based on Predicate Logic;we will not discuss it here.]
Relational Algebra
— 3
CSC343 Introduction to Databases
—
University of Toronto
A collection of
algebraic operators
that
Are defined on relations;
Produce relations as results,
and
therefore
can
be
combined
to
form
complex algebraic expressions.
Operators
Union, intersection, difference;
Renaming;
Selection and Projection;
Join
(natural
join,
Cartesian
product,
theta
join).
Relational Algebra
— 4
CSC343 Introduction to Databases
—
University of Toronto
Relations are sets, so we can apply set-theoreticoperators
However, we want the results to be relations(that is, homogeneous sets of tuples)
It is therefore meaningful to only apply union,intersection,
difference
to
pairs
of
relations
defined over the same attributes.
Relational Algebra
— 5
CSC343 Introduction to Databases
—
University of Toronto
Number
Surname
Age
7274
Robinson
37
7432
O'Malley
39
9824
Darkes
38
Number
Surname
Age
9297
O'Malley
56
7432
O'Malley
39
9824
Darkes
38
Graduates
Managers
Number
Surname
Age
7274
Robinson
37
7432
O'Malley
39
9824
Darkes
38
9297
O'Malley
56
Graduates
∪
Managers
Relational Algebra
— 6
CSC343 Introduction to Databases
—
University of Toronto
Number
Surname
Age
7274
Robinson
37
7432
O'Malley
39
9824
Darkes
38
Number
Surname
Age
9297
O'Malley
56
7432
O'Malley
39
9824
Darkes
38
Graduates
Managers
Number
Surname
Age
7432
O'Malley
39
9824
Darkes
38
Graduates
∩
Managers
Relational Algebra
— 7
CSC343 Introduction to Databases
—
University of Toronto
Number
Surname
Age
7274
Robinson
37
7432
O'Malley
39
9824
Darkes
38
Number
Surname
Age
9297
O'Malley
56
7432
O'Malley
39
9824
Darkes
38
Graduates
Managers
Number
Surname
Age
7274
Robinson
37
Graduates - Managers
Relational Algebra
— 8
CSC343 Introduction to Databases
—
University of Toronto
Paternity
∪
Maternity ???
Father
Child
Adam
Cain
Adam
Abel
Abraham
Isaac
Abraham
Ishmael
Paternity
Mother
Child
Eve
Cain
Eve
Seth
Sarah
Isaac
Hagar
Ishmael
Maternity
The problem:
Father
and
Mother
are different
names, but both represent a parent.
The solution: rename attributes!
Relational Algebra
— 13
CSC343 Introduction to Databases
—
University of Toronto
These are unary operators, in a senseorthogonal:
selection for "horizontal" decompositions;
projection for "vertical" decompositions.
A B C A B C
Selection
A B C
Projection
A B
Relational Algebra
— 14
CSC343 Introduction to Databases
—
University of Toronto
This is a unary operation which returns a relation
with the same schema as the operand;
but, with a
subset of the tuples
of the operand,
i.e., only those that satisfy a condition.
Notation:
F
Semantics:
σ
F
(r) = { t | t
∈
r s.t. t satisfies F, I.e., F(t)}
Relational Algebra
— 15
CSC343 Introduction to Databases
—
University of Toronto
We can use
the or
symbol (
v),
Not symbol
(
¬
) &
and
symbol
(ʌ)
as we would
for
specifying any logical
condition
Surname
FirstName
Age
Salary
Smith
Mary
25
2000
Black
Lucy
40
3000
Verdi
Nico
36
4500
Smith
Mark
40
3900
Employees
Surname
FirstName
Age
Salary
Smith
Mary
25
2000
Verdi
Nico
36
4500
σ
Age<30 v Salary>
(Employees)
Relational Algebra
— 16
CSC343 Introduction to Databases
—
University of Toronto
Surname
FirstName
PlaceOfBirth
Residence
Smith
Mary
Rome
Milan
Black
Lucy
Rome
Rome
Verdi
Nico
Florence
Florence
Smith
Mark
Naples
Florence
Citizens
σ
PlaceOfBirth=Residence
(Citizens)
Surname
FirstName
PlaceOfBirth
Residence
Black
Lucy
Rome
Rome
Verdi
Nico
Florence
Florence
Relational Algebra
— 17
CSC343 Introduction to Databases
—
University of Toronto
Projection returns a relation which includes a
subset
of the attributes
of the operand.
Notation: Given a relation r(X) and a subset Y of X:
π
Y
(r)
Semantics:
π
Y
(r) = { t[Y] | t
r }
Relational Algebra
— 18
CSC343 Introduction to Databases
—
University of Toronto
Surname
FirstName
Department
Head
Smith
Mary
Sales
De Rossi
Black
Lucy
Sales
De Rossi
Verdi
Mary
Personnel
Fox
Smith
Mark
Personnel
Fox
Employees
Surname
FirstName
Smith
Mary
Black
Lucy
Verdi
Mary
Smith
Mark
π
Surname, FirstName
(Employees)
Relational Algebra
— 19
CSC343 Introduction to Databases
—
University of Toronto
Surname
FirstName
Department
Head
Smith
Mary
Sales
De Rossi
Black
Lucy
Sales
De Rossi
Verdi
Mary
Personnel
Fox
Smith
Mark
Personnel
Fox
Employees
Department
Head
Sales
De Rossi
Personnel
Fox
π
Department, Head
(Employees)
Relational Algebra
— 20
CSC343 Introduction to Databases
—
University of Toronto
Note that the result of a projection contains at most as many tuples as the operand relation.
However, it may contain fewer, if several tuples collapse, i.e., they are identical in all their values.
Theorem:
π
Y
(r) contains as many tuples as r if
and only if Y is a superkey for r.
This property holds even if Y is "by chance” a superkey, i.e., it is not defined as a superkey in theschema,
but
it
is
a
superkey
for
the
current
database, see the example.
Relational Algebra
— 25
CSC343 Introduction to Databases
—
University of Toronto
r
1
1
), r
2
2
r
1
r
2
(natural join of r
1
and r
2
) is a relation on
1
2
(the
union
of the two sets):
{ t on X
1
2
| t [X
1
r
1
and t [X
2
r
2
or, equivalently
{ t on X
1
2
| exist t
1
r
1
and t
2
r
2
with t [X
1
] = t
1
and t [X
2
] = t
2
Relational Algebra
— 26
CSC343 Introduction to Databases
—
University of Toronto
The tuples in the resulting relation are obtained bycombining
tuples
in
the
operands
with
equal
values on the common attributes
The common attributes
often form a key of one
of
the
operands
(remember:
references
are
realized by means of foreign keys, and we join inorder to follow references)
Not always! Consider Person(Name,Addr,PostalC) and letus
define
Neighbour(Name,Addr,Name1,Addr1,PostalC)
by
joining
Person
with
ρ
Name,Addr
→
Name1,Addr
(Person);
What is criterion for neighbourhood here?
Relational Algebra
— 27
CSC343 Introduction to Databases
—
University of Toronto
Registration
Dept
Owner
…
6544 XY
75
Cordon Edouard
…
7122 HT
75
Cordon Edouard
…
5694 FR
75
Latour Hortense
…
6544 XY
47
Mimault Bernard
…
Code
Date
Officer
Dept
Registration
Owner
…
143256
25/10/
567
75
5694 FR
Latour Hortense
…
987554
26/10/
456
75
5694 FR
Latour Hortense
…
987557
26/10/
456
75
6544 XY
Cordon Edouard
…
630876
15/10/
456
47
6544 XY
Cordon Edouard
…
539856
12/10/
567
47
6544 XY
Mimault Bernard
…
Offences
⋈
Cars
Offences
Code
Date
Officer
Dept
Registartion
143256
25/10/
567
75
5694 FR
987554
26/10/
456
75
5694 FR
987557
26/10/
456
75
6544 XY
630876
15/10/
456
47
6544 XY
539856
12/10/
567
47
6544 XY
Cars
Relational Algebra
— 28
CSC343 Introduction to Databases
—
University of Toronto
In this example, join gives very different resultsfrom union (see earlier example)
Father
Child
Adam
Cain
Adam
Abel
Abraham
Isaac
Abraham
Ishmael
Paternity
Mother
Child
Eve
Cain
Eve
Seth
Sarah
Isaac
Hagar
Ishmael
Maternity
Paternity
Maternity
Father
Child
Mother
Adam
Cain
Eve
Abraham
Isaac
Sarah
Abraham
Ishmael
Hagar
Relational Algebra
— 29
CSC343 Introduction to Databases
—
University of Toronto
If a tuple does not have a "counterpart" in theother relation, then it does not contribute to thejoin ("dangling" tuple)
Employee
Department
Smith
sales
Black
production
White
production
Department
Head
production
Mori
purchasing
Brown
Employee
Department
Head
Black
production
Mori
White
production
Mori
r
1
r
2
r
1
r
2
Relational Algebra
— 30
CSC343 Introduction to Databases
—
University of Toronto
As an extreme, we might have that no tuple hasa counterpart, and all tuples are dangling
Employee
Department
Smith
sales
Black
production
White
production
Department
Head
marketing
Mori
purchasing
Brown
Employee
Department
Head
r
1
r
2
r
1
r
2
Relational Algebra
— 31
CSC343 Introduction to Databases
—
University of Toronto
If each tuple of each operand can be combinedwith all the tuples of the other, then the join has acardinality that is the product of the cardinalitiesof the operands
Employee
Project
Smith
A
Black
A
White
A
Project
Head
A
Mori
A
Brown
Employee
Project
Head
Smith
A
Mori
Black
A
Brown
White
A
Mori
Smith
A
Brown
Black
A
Mori
White
A
Brown
r
1
r
2
r
1
r
2
Relational Algebra
— 32
CSC343 Introduction to Databases
—
University of Toronto
Given r
1
1
), r
2
2
) the join has cardinality
| r
1
⋈
r
2
| r
1
| r
2
where | r | is the cardinality of relation r.
Moreover:
if the join is complete, then its cardinality is atleast the maximum of | r
1
| and | r
2
if X
1
2
contains a key for r
2
then | r
1
⋈
r
2
| r
1
if X
1
2
is the primary key for r
2
, and there is a
referential constraint between X
1
2
in r
1
and
such a key, then | r
1
⋈
r
2
| = | r
1
Relational Algebra
— 37
CSC343 Introduction to Databases
—
University of Toronto
We have made no assumptions about the setsof attributes X
1
and X
2
on which the operands
of a join operation are defined; the two setscould even be equal or disjoint.
If X
1
2
then r
1
r
2
= r
1
r
2
since, by
definition, the result is a relation which includestuples t such that t[X
1
r
1
and t[X
2
r
2
, and
1
2
Relational Algebra
— 38
CSC343 Introduction to Databases
—
University of Toronto
The natural join is defined also when theoperands have no attributes in common.
In this case no condition is imposed on tuples,and therefore the result contains tuples obtainedby combining the tuples of the operands in allpossible ways.
Relational Algebra
— 39
CSC343 Introduction to Databases
—
University of Toronto
Employee
Project
Smith
A
Black
A
Black
B
Code
Name
A
Venus
B
Mars
Employee
Project
Code
Name
Smith
A
A
Venus
Black
A
A
Venus
Black
B
A
Venus
Smith
A
B
Mars
Black
A
B
Mars
Black
B
B
Mars
Employees
Projects
Employes
Projects
Relational Algebra
— 40
CSC343 Introduction to Databases
—
University of Toronto
In most cases, a Cartesian product is meaningfulonly if followed by a selection:
theta-join
: a derived operator
r
1
⋈
F
r
2
σ
F
(r
1
⋈
r
2
if F is a conjunction of equalities, then wehave an
equi-join
Relational Algebra
— 41
CSC343 Introduction to Databases
—
University of Toronto
Employee
Project
Smith
A
Black
A
Black
B
Code
Name
A
Venus
B
Mars
Employee
Project
Code
Name
Smith
A
A
Venus
Black
A
A
Venus
Black
B
B
Mars
Employees
Projects
Employes
Project=Code
Projects
Relational Algebra
— 42
CSC343 Introduction to Databases
—
University of Toronto
Consider two relations A(x,y), B(y) and supposewe want to specify the query"Find all A's that are associated with all B's"
This can be expressed as
π
x
π
x
π
x
This
means
that
division
does
not
extend
the
expressiveness of Relational Algebra, but it is aconvenient operation to use in many situations.
Relational Algebra
— 43
CSC343 Introduction to Databases
—
University of Toronto
Assume
Take(x,y) - "student x has taken course y",
CS(y) - "y is a CS course"
We want "All students who have taken all CScourses"
x
Table of all students, CS
courses
x
Table of all
students and the CS courses they have nottaken
x
x
All
students who have not taken a CS course
Relational Algebra
— 44
CSC343 Introduction to Databases
—
University of Toronto
A query is a function from database instances torelations.
Queries are formulated in relational algebra bymeans of expressions over relations.
Relational Algebra
— 49
CSC343 Introduction to Databases
—
University of Toronto
Number
Name
Salary
NumberH
NameH
SalaryH
104
Luigi Neri
61
210
Marco Celli
60
252
Nico Bini
70
375
Mary Smith
65
π
Number,Name,Salary,NumberH,Nam3H, SalaryH
(
σ
Salary>SalaryH
(
ρ
Num,Name,Sal,Age
→
NumH,NameH,SalH,AgeH
Employees
⋈
NumberH=Head
(Supervision
⋈
Emp=Number
Employees)))
“Find the employees earning more than their respective
supervisors,
return
registration
numbers,
names
and
salaries
of
the
employees and their supervisors.”
Employees(Number,Name,Age,Salary)Supervision(Head,Emp)
Try it! Definitely challenging
☺
Relational Algebra
— 50
CSC343 Introduction to Databases
—
University of Toronto
“Find
registration
numbers
and
names
of
supervisors,
all
of whose employees earn more
than 40M.”
Employees(Number,Name,Age,Salary)Supervision(Head,Emp)
Number
Name
301
Steve Smith
375
Mary Smith
π
Number,Name
(Employees
⋈
Number=Head
(
π
Head
(Supervision) -
π
Head
(Supervision
⋈
Number=Emp
(
σ
Salary
≤
40
Employees))))
Try it!
Relational Algebra
— 51
CSC343 Introduction to Databases
—
University of Toronto
Films(Film#,Title,Director,Year,ProdCost)Artists(Actor#,Surname,FirsName,Sex,Birthday,
Nationality)
Roles(Film#,Actor#,Character)
Find “The titles of films starring Henry Fonda
π
Title
(Films
σ
(FirstName=“Henry”)
∧
(Surname=“Fonda”)
(Artists
Roles)))
Try it!
Relational Algebra
— 52
CSC343 Introduction to Databases
—
University of Toronto
Films(Film#,Title,Director,Year,ProdCost)Artists(Actor#,Surname,FirsName,Sex,Birthday,
Nationality)
Roles(Film#,Actor#,Character)
Find “The titles of all films in which the director isalso an actor”
π
Title
σ
(Director=Actor#)
(Films
Roles))
Try it!
Relational Algebra
— 53
CSC343 Introduction to Databases
—
University of Toronto
Films(Film#,Title,Director,Year,ProdCost)Artists(Actor#,Surname,FirsName,Sex,Birthday,
Nationality)
Roles(Film#,Actor#,Character)
Find “The actors who have played two characters inthe same film; show the title of each such film, firstname
and
surname
of
the
actor
and
the
two
characters”
π
Title,FirstName,Surname,Character1,Character
ρ
Film#,Actor#,Character
→
Film#1,Actor#1,Character
(Roles)
(Film#1=Film#)
∧
(Actor#1=Actor#)
∧
(Character
≠
Character)
Roles
Artists
Films)
Try it!
Relational Algebra
— 54
CSC343 Introduction to Databases
—
University of Toronto
Films(Film#,Title,Director,Year,ProdCost)Artists(Actor#,Surname,FirsName,Sex,Birthday,
Nationality)
Roles(Film#,Actor#,Character)
“The titles of the films in which the actors are all ofthe same sex” π
Title
(Films) –
π
Title
(Films)
σ
sex
≠
sex
((Artists
Roles)
ρ
Ac#,Char,Sur,Fir,Sex,BD,N
→
Ac#1,Char1,Sur1,Fir1,Sex1,BD1,N
(Artists
Roles))
Try it!
Relational Algebra
— 55
CSC343 Introduction to Databases
—
University of Toronto
Consider
σ
Age>
(People)
Which tuples belong to the result?
The first yes, the second no, but the third??
Name
Age
Salary
Aldo
35
15
Andrea
27
21
Maria
NULL
42
People
Relational Algebra
— 56
CSC343 Introduction to Databases
—
University of Toronto
Emp(Num,Name,Age,Sal)
"Find all ages for which there are at least twoemployees earning more than 40M" σ
sal>
(Emp)
σ
sal1>
ρ
Num,Name,Sal
→
Num1,Name1,Sal
Emp)
This results in a relation R with schema
??(Num,Name,Age,Sal,Num1,Name1,Sal1)
Now we select tuples where Num
Num1 and project
on Age
π
Age
σ
Num
≠
Num
In a similar fashion, you can find ages for which thereare
3 employees earning >40M, etc.