Relational Algebra - University of Waterloo › ~cs348 › F14 › 3_algebra.pdf · 2014-09-02 ·...
Transcript of Relational Algebra - University of Waterloo › ~cs348 › F14 › 3_algebra.pdf · 2014-09-02 ·...
Relational Algebra
David R. Cheriton S hool of Computer S ien e
University of Waterloo
CS 348
Introdu tion to Database Management
Fall 2014
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 1 / 1
Database S hema Used in Examples
RespEmpDeptNoProjNo
EmPTime
Project
EmEnDate
Emp_Act
EmpNoMajProj
MidInitLastName
Employee
WorkDeptHireDateSalary
FirstName
EmpNo
DeptNameMgrNoAdmrDept
DeptNo
Department
ProjNoActNoEmStDate
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 2 / 1
Relational Algebra
�
the relational algebra onsists of a set of operators
�
relational algebra is losed
�
ea h operator takes as input one or more relations
�
ea h operator de�nes a single output relation in terms of its input
relation(s)
�
relational operators an be omposed to form expressions that
de�ne new relations in terms of existing relations.
�
Notation:
R is a relation name; E is a relational algebra expression
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 3 / 1
Primary Relational Operators
�
Relation Name: R
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 4 / 1
Primary Relational Operators
�
Relation Name: R
�
Sele tion: �
ondition
(E)
�
result s hema is the same as E 's
�
result instan e in ludes the subset of the tuples of E that ea h
satis�es the ondition
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 4 / 1
Primary Relational Operators
�
Relation Name: R
�
Sele tion: �
ondition
(E)
�
result s hema is the same as E 's
�
result instan e in ludes the subset of the tuples of E that ea h
satis�es the ondition
�
Proje tion: �
attributes
(E)
�
result s hema in ludes only the spe i�ed attributes
�
result instan e ould have as many tuples as E , ex ept that
dupli ates are eliminated
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 4 / 1
Primary Relational Operators ( ont'd)
�
Rename: �(R(F );E)
�
F is a list of terms of the form oldname ! newname
�
returns the result of E with olumns renamed a ording to F .
�
remembers the result as R for future expressions
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 5 / 1
Primary Relational Operators ( ont'd)
�
Rename: �(R(F );E)
�
F is a list of terms of the form oldname ! newname
�
returns the result of E with olumns renamed a ording to F .
�
remembers the result as R for future expressions
�
Produ t: E
1
� E
2
�
result s hema has all of the attributes of E
1
and all of the attributes
of E
2
�
result instan e in ludes one tuple for every pair of tuples (one from
ea h expression result) in E
1
and E
2
�
sometimes alled ross-produ t or Cartesian produ t
�
renaming is needed when E
1
and E
2
have ommon attributes
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 5 / 1
Cross Produ t Example
R
AAA BBB
a
1
b
1
a
2
b
2
a
3
b
3
S
CCC DDD
1
d
1
2
d
2
R � S
AAA BBB CCC DDD
a
1
b
1
1
d
1
a
2
b
2
1
d
1
a
3
b
3
1
d
1
a
1
b
1
2
d
2
a
2
b
2
2
d
2
a
3
b
3
2
d
2
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 6 / 1
Sele t,Proje t,Produ t Examples
�
Note: Use Emp to mean the Employee relation, Proj the proje t
relation
�
Find the last names and hire dates of employees who make more
than $100000.
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 7 / 1
Sele t,Proje t,Produ t Examples
�
Note: Use Emp to mean the Employee relation, Proj the proje t
relation
�
Find the last names and hire dates of employees who make more
than $100000.
�
LastName ;HireDate
(�
Salary>100000
(Emp))
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 7 / 1
Sele t,Proje t,Produ t Examples
�
Note: Use Emp to mean the Employee relation, Proj the proje t
relation
�
Find the last names and hire dates of employees who make more
than $100000.
�
LastName;HireDate
(�
Salary>100000
(Emp))
�
For ea h proje t for whi h department E21 is responsible, �nd the
name of the employee in harge of that proje t.
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 7 / 1
Sele t,Proje t,Produ t Examples
�
Note: Use Emp to mean the Employee relation, Proj the proje t
relation
�
Find the last names and hire dates of employees who make more
than $100000.
�
LastName;HireDate
(�
Salary>100000
(Emp))
�
For ea h proje t for whi h department E21 is responsible, �nd the
name of the employee in harge of that proje t.
�
ProjNo;LastName
(�
DeptNo=E21
(�
RespEmp=EmpNo
(Emp � Proj )))
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 7 / 1
Joins
�
Conditional join: E
1
⋊⋉ ondition
E
2
�
equivalent to �
ondition
(E
1
� E
2
)
�
spe ial ase: equijoin
Proj ⋊⋉(RespEmp=EmpNo)
Emp
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 8 / 1
Joins
�
Conditional join: E
1
⋊⋉ ondition
E
2
�
equivalent to �
ondition
(E
1
� E
2
)
�
spe ial ase: equijoin
Proj ⋊⋉(RespEmp=EmpNo)
Emp
�
Natural join (E
1
⋊⋉ E
2
)
�
The result of E
1
⋊⋉ E
2
an be formed by the following steps
1 form the ross-produ t of E
1
and E
2
(renaming dupli ate attributes)
2 eliminate from the ross produ t any tuples that do not have
mat hing values for all pairs of attributes ommon to s hemas E
1
and E
2
3 proje t out dupli ate attributes
�
if no attributes in ommon, this is just a produ t
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 8 / 1
Example: Natural Join
�
Consider the natural join of the Proje t and Department tables,
whi h have attribute DeptNo in ommon
�
the s hema of the result will in lude attributes ProjName, DeptNo,
RespEmp, MajProj, DeptName, MgrNo, and AdmrDept
�
the resulting relation will in lude one tuple for ea h tuple in the
Proje t relation (why?)
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 9 / 1
Set-Based Relational Operators
�
Union (R [ S):
�
s hemas of R and S must be �union ompatible�
�
result in ludes all tuples that appear either in R or in S or in both
�
Union Compatible:
�
Same number of �elds.
�
'Corresponding' �elds have the same type
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 10 / 1
Set-Based Relational Operators
�
Union (R [ S):
�
s hemas of R and S must be �union ompatible�
�
result in ludes all tuples that appear either in R or in S or in both
�
Di�eren e (R � S):
�
s hemas of R and S must be �union ompatible�
�
result in ludes all tuples that appear in R and that do not appear
in S
�
Union Compatible:
�
Same number of �elds.
�
'Corresponding' �elds have the same type
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 10 / 1
Set-Based Relational Operators
�
Union (R [ S):
�
s hemas of R and S must be �union ompatible�
�
result in ludes all tuples that appear either in R or in S or in both
�
Di�eren e (R � S):
�
s hemas of R and S must be �union ompatible�
�
result in ludes all tuples that appear in R and that do not appear
in S
�
Interse tion (R \ S):
�
s hemas of R and S must be �union ompatible�
�
result in ludes all tuples that appear in both R and S
�
Union Compatible:
�
Same number of �elds.
�
'Corresponding' �elds have the same type
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 10 / 1
Relational Division
X
A B C
a
1
b
1
1
a
1
b
1
2
a
1
b
2
2
a
2
b
1
1
a
2
b
1
2
a
2
b
2
2
a
2
b
3
3
a
3
b
1
1
S
B C
b
1
1
b
1
2
b
2
2
X =S
A
a
1
a
2
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 11 / 1
Division is the Inverse of Produ t
R
A
a
1
a
2
S
B C
b
1
1
b
1
2
b
2
2
R � S
A B C
a
1
b
1
1
a
1
b
1
2
a
1
b
2
2
a
2
b
1
1
a
2
b
1
2
a
2
b
2
2
(R � S)=S
A
a
1
a
2
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 12 / 1
Summary of Relational Operators
E ::= R
j �
ondition
(E)
j �
attributes
(E)
j �(R(F );E)
j E
1
� E
2
j E
1
⋊⋉ ondition
E
2
j E
1
⋊⋉ E
2
j E
1
[ E
2
j E
1
\ E
2
j E
1
� E
2
j E
1
= E
2
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 13 / 1
Algebrai Equivalen es
�
This:
�
ProjNo;LastName
(�
DeptNo=E21
(�
RespEmp=EmpNo
(E � P)))
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 14 / 1
Algebrai Equivalen es
�
This:
�
ProjNo;LastName
(�
DeptNo=E21
(�
RespEmp=EmpNo
(E � P)))
�
is equivalent to this:
�
ProjNo;LastName
(�
DeptNo=E21
(E ⋊⋉RespEmp=EmpNo
P))
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 14 / 1
Algebrai Equivalen es
�
This:
�
ProjNo;LastName
(�
DeptNo=E21
(�
RespEmp=EmpNo
(E � P)))
�
is equivalent to this:
�
ProjNo;LastName
(�
DeptNo=E21
(E ⋊⋉RespEmp=EmpNo
P))
�
is equivalent to this:
�
ProjNo;LastName
(E ⋊⋉RespEmp=EmpNo
�
DeptNo=E21
(P))
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 14 / 1
Algebrai Equivalen es
�
This:
�
ProjNo;LastName
(�
DeptNo=E21
(�
RespEmp=EmpNo
(E � P)))
�
is equivalent to this:
�
ProjNo;LastName
(�
DeptNo=E21
(E ⋊⋉RespEmp=EmpNo
P))
�
is equivalent to this:
�
ProjNo;LastName
(E ⋊⋉RespEmp=EmpNo
�
DeptNo=E21
(P))
�
is equivalent to this:
�
ProjNo;LastName
( ( �
LastName;EmpNo
(E)) ⋊⋉RespEmp=EmpNo
( �
ProjNo;RespEmp
(�
DeptNo=E21
(P))))
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 14 / 1
Algebrai Equivalen es
�
This:
�
ProjNo;LastName
(�
DeptNo=E21
(�
RespEmp=EmpNo
(E � P)))
�
is equivalent to this:
�
ProjNo;LastName
(�
DeptNo=E21
(E ⋊⋉RespEmp=EmpNo
P))
�
is equivalent to this:
�
ProjNo;LastName
(E ⋊⋉RespEmp=EmpNo
�
DeptNo=E21
(P))
�
is equivalent to this:
�
ProjNo;LastName
( ( �
LastName;EmpNo
(E)) ⋊⋉RespEmp=EmpNo
( �
ProjNo;RespEmp
(�
DeptNo=E21
(P))))
�
More on this topi later when we dis uss database tuning. . .
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 14 / 1
Relational Completeness
De�nition (Relationally Complete)
A query language that is at least as expressive as relational algebra is
said to be relationally omplete.
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 15 / 1
Relational Completeness
De�nition (Relationally Complete)
A query language that is at least as expressive as relational algebra is
said to be relationally omplete.
�
The following languages are all relationally omplete:
�
safe relational al ulus
�
relational algebra
�
SQL
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 15 / 1
Relational Completeness
De�nition (Relationally Complete)
A query language that is at least as expressive as relational algebra is
said to be relationally omplete.
�
The following languages are all relationally omplete:
�
safe relational al ulus
�
relational algebra
�
SQL
�
SQL has additional expressive power be ause it aptures dupli ate
tuples, unknown values, aggregation, ordering, . . .
CS 348 (Intro to DB Mgmt) Relational Algebra FALL 2014 15 / 1