Relational Algebra

603 Database Systems
Senior Lecturer: Laurie Webster II,
M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E.
Lecture 10
A First Course in Database Systems
Relational Algebra
Chapter 5
SELECTION 
:
Movie
title
year length
Star Wars
1977 124
Mighty Duck 1971 104
inColor studioName producerC#
yes
Fox
12345
yes
Disney
67890
Suppose we want the Set of tuples in the relation
Movie(title, year, length, inColor, studioName, producerC#)
that represent Fox movies at least 100 minutes
long.
c(R) = length
100 AND studioName = ‘Box’(Movie)
Relational Algebra
c(R) = length
100 AND studioName = ‘Box’(Movie)
==>
Result
title
Star Wars
year
1977
length inColor
124 Yes
studioName
Fox
producerC#
12345
Relational Algebra
Chapter 5
PROJECTION  :
Attribute list (R)
Movie
title
Star Wars
Mighty Duck
Wayne’s W.
year
1977
1991
1992
length
124
104
95
inColor
yes
yes
yes
title, year, length (Movie)
studioName producerC#
Fox
12345
Disney
67890
Paramount 99999
Relational Algebra
Chapter 5
PROJECTION  :
title, year, length (Movie)
Movie
title
Star Wars
Mighty Duck
Wayne’s W.
year length
1977 124
1991 104
1992 95
Relational Algebra
Chapter 5
Cartesian Product:
The Cartesian Product (Cross Product) of two sets R
and S is the set of pairs that can be formed by
choosing the first element of the pair to be any
element of R and the second any element of S.
This product is denoted by R  S.
Relational Algebra
Cartesian Product:
R(A1, A2, …., Am,B1, B2, ….Bn)
R1(A1, A2, …., Am) R2(B1, B2, ….Bn)
* A tuple t exists in R for each combination of tuples t1
from R1 and t2 for R2 such that:
t[A1, A2, …., Am ] = t1 and t[B1, B2, ….Bn ]= t2
If R1 has n1 tuples and R2 has n2 tuples, then R will
have n1 * n2 tuples
Relational Algebra
CARTESIAN PRODUCT is a meaningless operation
on its own.
It can combine related tuples from two relations if
followed by the appropriate SELECT operation.
Example: Combine each DEPARTMENT tuple with
the EMPLOYEE tuple of the manager.
DEP_EMP  DEPARTMENT  EMPLOYEE
DEP_MANAGER  MGRSSN=SSN(DEP_EMP)
Relational Algebra
Chapter 5
Natural Joins:
More often than we want to take the product of two
relations, we find a need to join them by pairing only
those tuples that match in some way.
The simplest match is the natural join of two relations R
and S., denoted R  S.
Relational Algebra
Chapter 5
Combining Relational Algebra Operations to form
Queries:
If all we could do was to write single operations on
one or two relations as queries, then relational algebra
would not be as useful as it is.
One can construct expressions of relational algebra by
applying operators to subexpressions, using
parentheses when necessary to indicate grouping of
Relational Algebra
Chapter 5
Combining Relational Algebra Operations to form
Queries:
Consider the Movie relation. Suppose we want to
know “What are the titles and years of movies made
by Fox that are at least 100 minutes long?”
Relational Algebra
Chapter 5
One way to compute the answer to this query is the
following steps:
1. Select those Movie tuples that have length  100.
2. Select those Movie tuples that have
studioName = ‘Fox’.
3. Compute the intersection of (1) and (2).
4. Project the relation form (3) onto attributes title
and year.
See the Expression tree for a relational algebra
expression on Next Slide.
Relational Algebra
Chapter 5
Relational Algebra Expression representing the above
steps:
title, year (Movie)

 length >= 100(Movie)
Movie
 studioName = ‘Fox’ (Movie)
Movie
Relational Algebra
Chapter 5
The two selection nodes in the expression tree corresponds to steps (1)
and (2). The intersection node corresponds to step (3), and the
projection node is step (4).
Conventional Representation:
title, year
( length >= 100(Movie)   studioName = ‘Fox’ (Movie))
Alternatively :
title, year
( length >= 100
AND
 studioName = ‘Fox’ (Movie))
Relational Algebra
Chapter 5
Linear Notation for Algebraic Expressions:
Represent the previous expression tree R(t, y, l, i, s, p) :=  length >= 100(Movie)
S(t, y, l, i, s, p) :=  studioName = ‘Fox’ (Movie)
T(t, y, l, i, s, p) := R  S
Answer (title, year) :=  t , i ( T ) => result of final step
Relational Algebra
Chapter 5
Combining Steps:
Represent the previous expression tree R(t, y, l, i, s, p) :=  length >= 100(Movie)
S(t, y, l, i, s, p) :=  studioName = ‘Fox’ (Movie)
Answer (title, year) := t, i, (R  S ) => result of final step
Relational Algebra
Chapter 5
Company Database:
EMPLOYEE
FNAME MINIT
John
B
Franklin T
Alicia
J
Jennifer S
Ramesh K
Joyce
A
Ahmad V
James
E
LNAME
Smith
Wong
Zelaya
Wallace
Narayan
English
Jabbar
Borg
SSN
123456789
333445555
999887777
987654321
666884444
453453453
987987987
888665555
SEX
M
M
F
F
M
F
M
M
SALARY
30000
40000
25000
43000
38000
25000
25000
55000
DNO
5
5
4
4
5
5
4
1
Relational Algebra
Chapter 5
DEPT_LOCTIONS
`DNUMBER
1
4
5
5
5
LOCATION
Houston
Stafford
Bellaire
Sugarland
Houston
DEPARTMENT
DNAME
Carrie Fisher
Harrison Ford
DNUMBER
123 Maple St., Holl.
789 Palm Dr., B. H.
MGRSSN
F
M
MGRSTRDT
9/9/99
7/7/77
Relational Algebra
Retrieve SSN of all employees who either work in DEP5 or
directly supervise an employee who works in DEP5.
Solution: Use union operation on Result1 and Result2
DEP5_EMPS   DNO = 5 (EMPLOYEE)
RESULT1   SSN (DEP5_EMPS )
RESULT2(SSN)   SUPPERSSN (DEP5_EMPS )
RESULT  RESULT1  RESULT2
Relational Algebra
Chapter 5 - Next Lecture
•
•
•
•
Projection
Selection
Cartesian Product
Natural Joins
Relational Algebra
Complete Set of Relational Algebra Operations:
All the operations discussed so far can be described as
a sequence of only the operations SELECT,
PROJECT, UNION, SET, DIFFERENCE, and
CARTESIAN PRODUCT.
Hence, the set {  , ,  , - ,  } is called a complete
set of relational algebra operations.
Relational Algebra
Any query language equivalent to
these operations is called relationally
complete!
Relational Algebra
NEXT LECTURE
SQL