Professional Documents
Culture Documents
03 - RAlgebra - CSC 343 PDF
03 - RAlgebra - CSC 343 PDF
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 1 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 2
1
Union Intersection
Graduates Graduates
Number Surname Age Number Surname Age
7274 Robinson 37 7274 Robinson 37
7432 O'Malley 39 Graduates ∪ Managers 7432 O'Malley 39
9824 Darkes 38 Number Surname Age 9824 Darkes 38 Graduates ∩ Managers
7274 Robinson 37 Number Surname Age
7432 O'Malley 39 7432 O'Malley 39
Managers 9824 Darkes 38 Managers 9824 Darkes 38
Number Surname Age 9297 O'Malley 56 Number Surname Age
9297 O'Malley 56 9297 O'Malley 56
7432 O'Malley 39 7432 O'Malley 39
9824 Darkes 38 9824 Darkes 38
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 5 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 6
Graduates
Paternity Maternity
Number Surname Age Father Child Mother Child
7274 Robinson 37 Adam Cain Eve Cain
7432 O'Malley 39 Adam Abel Eve Seth
9824 Darkes 38 Graduates - Managers Abraham Isaac Sarah Isaac
Number Surname Age Abraham Ishmael Hagar Ishmael
Managers 7274 Robinson 37
Number Surname Age
9297 O'Malley 56 Paternity ∪ Maternity ???
7432 O'Malley 39
9824 Darkes 38
The problem: Father and Mother are different
names, but both represent a parent.
The solution: rename attributes!
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 7 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 8
2
Renaming
Example of Renaming
This is a unary operator which changes attribute
Paternity ρFather−> Parent(Paternity)
names for a relation without changing any Father Child Parent Child
values. Adam Cain Adam Cain
Adam Abel Adam Abel
Renaming removes the limitations associated Abraham Isaac Abraham Isaac
with set operators. Abraham Ishmael Abraham Ishmael
Notation: ρOldName→NewName(r)
For example, ρFather→Parent(Paternity) The textbook allows positions rather than attribute
names, e.g., 1 → Parent
If there are two or more attributes involved in a
renaming operation, then ordering is meaningful: Textbook also allows renaming of the relation
itself,e.g.,Paternity,1→ Parenthood,Parent
e.g., ρBranch,Salary → Location,Pay(Employees)
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 9 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 10
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 11 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 12
3
Selection and Projection Selection
These are unary operators, in a sense
orthogonal:
This is a unary operation which returns a relation
selection for "horizontal" decompositions;
with the same schema as the operand;
projection for "vertical" decompositions.
but, with a subset of the tuples of the operand,
A B C A B C
Selection i.e., only those that satisfy a condition.
Notation: σF(r)
Semantics: σF(r) = { t | t ∈r s.t. t satisfies F, I.e., F(t)}
A B C A B
Projection
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 13 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 14
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 15 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 16
4
Projection Example of Projection
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 17 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 18
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 19 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 20
5
Tuples that Collapse Tuples that do not Collapse,
"by Chance"
Students Students
RegNum Surname FirstName BirthDate DegreeProg RegNum Surname FirstName BirthDate DegreeProg
284328 Smith Luigi 29/04/59 Computing
296328 Smith John 29/04/59 Computing
296328 Smith John 29/04/59 Computing
587614 Smith Lucy 01/05/61 Engineering
587614 Smith Lucy 01/05/61 Engineering
934856 Black Lucy 01/05/61 Fine Art 934856 Black Lucy 01/05/61 Fine Art
965536 Black Lucy 05/03/58 Fine Art 965536 Black Lucy 05/03/58 Engineering
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 21 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 22
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 23 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 24
6
Definition of Natural Join Natural Join: Comments
r1 (X1), r2 (X2) The tuples in the resulting relation are obtained by
r1 ⋈ r2 (natural join of r1 and r2) is a relation on combining tuples in the operands with equal
X1X2 (the union of the two sets): values on the common attributes
{ t on X1X2 | t [X1] ∈ r1 and t [X2] ∈ r2 } The common attributes often form a key of one
of the operands (remember: references are
or, equivalently realized by means of foreign keys, and we join in
order to follow references)
Not always! Consider Person(Name,Addr,PostalC) and let
{ t on X1X2 | exist t1 ∈ r1 and t2 ∈ r2 with t [X1] = t1 us define Neighbour(Name,Addr,Name1,Addr1,PostalC)
and t [X2] = t2 } by joining Person with ρName,Addr→Name1,Addr1(Person);
What is criterion for neighbourhood here?
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 25 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 26
7
Joins can be Incomplete Joins can be Empty
If a tuple does not have a "counterpart" in the As an extreme, we might have that no tuple has
other relation, then it does not contribute to the a counterpart, and all tuples are dangling
join ("dangling" tuple)
r2
r1
Department Head
r1 r2
production Mori
Employee Department Department Head
Employee Department purchasing Brown
Smith sales marketing Mori
Smith sales purchasing Brown
Black production Black production
White production White production
r1 ⋈ r2 r1 ⋈ r2
Employee Department Head Employee Department Head
Black production Mori
White production Mori
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 29 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 30
8
Outer Join Outer Join Operations
r1 r2
A variant of the join, to keep all pieces of Employee Department Department Head
Smith sales
information from the operands. Black production
production
purchasing
Mori
Brown
White production
An outer join operation "pads with nulls" the
tuples in one operant relation that have no Employee Department Head
Smith Sales
r1 ⋈ LEFTr2
NULL
counterpart in the other relation. Black production Mori
White production Mori
Three variants:
Employee Department Head
LEFT — only tuples of left operand are
r1 ⋈ RIGHT r2
Black production Mori
padded; White
NULL
production
purchasing
Mori
Brown
RIGHT — only tuples of right operand are
Employee Department Head
padded; Smith Sales NULL
r1 ⋈ FULL r2 Black production Mori
FULL — tuples of both operands are padded. White production Mori
NULL purchasing Brown
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 33 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 34
r1 ⋈ r2 ⋈ r3
Employee Department Division Head
Black production A Mori
Brown marketing B Brown
White production A Mori
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 35 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 36
9
Join and Intersection Natural Join as Cartesian Product
We have made no assumptions about the sets The natural join is defined also when the
of attributes X1 and X2 on which the operands operands have no attributes in common.
of a join operation are defined; the two sets In this case no condition is imposed on tuples,
could even be equal or disjoint. and therefore the result contains tuples obtained
If X1 = X2 then r1 ⋈ r2 = r1 ∩ r2 since, by by combining the tuples of the operands in all
definition, the result is a relation which includes possible ways.
tuples t such that t[X1] ∈ r1 and t[X2] ∈ r2, and
X1 = X2.
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 37 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 38
Employes ⋈ Projects
Employee Project Code Name if F is a conjunction of equalities, then we
Smith A A Venus have an equi-join
Black A A Venus
Black B A Venus
Smith A B Mars
Black A B Mars
Black B B Mars
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 39 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 40
10
Equi-join: example Division
Employees Consider two relations A(x,y), B(y) and suppose
Employee Project we want to specify the query
Smith A
Black A Projects "Find all A's that are associated with all B's"
Black B Code Name This can be expressed as
A Venus
B Mars A/B = πx(A) - πx((πx(A) ⋈ B) - A)
This means that division does not extend the
Employes ⋈ Project=Code Projects expressiveness of Relational Algebra, but it is a
Employee
Smith
Project
A
Code
A
Name
Venus
convenient operation to use in many situations.
Black A A Venus
Black B B Mars
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 41 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 42
11
A Sample Database Example 1
“Find the numbers, names and ages of employees
Supervision earning more than 40k.”
Head Employee Employees(Number,Name,Age,Salary)
210 101
Employees 210 103 Supervision(Head,Emp)
Number Name Age Salary 210 104
101 Mary Smith 34 40 231 105
103 Mary Bianchi 23 35 301 210 Try it!
πNumber,Name,Age(σSalary>40Employees)
104 Luigi Neri 38 61 301 231
105 Nico Bini 44 38 375 252 Number Name Age
210 Marco Celli 49 60 104 Luigi Neri 38
231 Siro Bisi 50 60 210 Marco Celli 49
252 Nico Bini 44 70 231 Siro Bisi 50
301 Steve Smith 34 70 252 Nico Bini 44
375 Mary Smith 50 65 301 Steve Smith 34
375 Mary Smith 50
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 45 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 46
Example 2 Example 3
“Find the registration numbers of the supervisors “Find the names and salaries of the supervisors
of the employees earning more than 40M.” of the employees earning more than 40M.”
Employees(Number,Name,Age,Salary) Employees(Number,Name,Age,Salary)
Supervision(Head,Emp) Supervision(Head,Emp)
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 47 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 48
12
Example 4 Example 5
“Find the employees earning more than their “Find registration numbers and names of
respective supervisors, return registration supervisors, all of whose employees earn more
numbers, names and salaries of the than 40M.”
employees and their supervisors.” Employees(Number,Name,Age,Salary)
Employees(Number,Name,Age,Salary) Supervision(Head,Emp)
Supervision(Head,Emp) (Employees ⋈Number=Head (πHead (Supervision) -
Try it!
πNumber,Name
πNumber,Name,Salary,NumberH,Nam3H, SalaryH
Try it! Definitely
(σSalary>SalaryH challenging ☺
(ρNum,Name,Sal,Age→NumH,NameH,SalH,AgeH Employees πHead (Supervision ⋈Number=Emp (σSalary≤40Employees))))
⋈NumberH=Head (Supervision ⋈Emp=Number Employees)))
Number Name
Number Name Salary NumberH NameH SalaryH
301 Steve Smith
104 Luigi Neri 61 210 Marco Celli 60
252 Nico Bini 70 375 Mary Smith 65 375 Mary Smith
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 49 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 50
Films(Film#,Title,Director,Year,ProdCost) Films(Film#,Title,Director,Year,ProdCost)
Artists(Actor#,Surname,FirsName,Sex,Birthday, Artists(Actor#,Surname,FirsName,Sex,Birthday,
Nationality) Nationality)
Roles(Film#,Actor#,Character) Roles(Film#,Actor#,Character)
13
Example 3 Example 4
Films(Film#,Title,Director,Year,ProdCost)
Artists(Actor#,Surname,FirsName,Sex,Birthday, Films(Film#,Title,Director,Year,ProdCost)
Nationality) Artists(Actor#,Surname,FirsName,Sex,Birthday,
Roles(Film#,Actor#,Character) Nationality)
Roles(Film#,Actor#,Character)
Find “The actors who have played two characters in
the same film; show the title of each such film, first “The titles of the films in which the actors are all of
name and surname of the actor and the two the same sex”
characters” πTitleTry
(Films) –
it!
πTitle,FirstName,Surname,Character1,Character
Try it! πTitle(Films) ⋈ σ sex≠sex1((Artists ⋈ Roles) ⋈
(ρFilm#,Actor#,Character→Film#1,Actor#1,Character1(Roles) ρ Ac#,Char,Sur,Fir,Sex,BD,N →Ac#1,Char1,Sur1,Fir1,Sex1,BD1,N1
⋈(Film#1=Film#)∧(Actor#1=Actor#)∧ (Character1≠Character) (Artists ⋈ Roles))
Roles ⋈ Artists ⋈ Films)
CSC343 Introduction to Databases — University of Toronto Relational Algebra — 53 CSC343 Introduction to Databases — University of Toronto Relational Algebra — 54
14
Blackboard Example II
15