Professional Documents
Culture Documents
Database Systems
Chapter 8
The Relational Algebra
8.1
Relation Schema
8.2
Relation Instance (1)
8.3
Basic Relational Algebra Operations
8.4
Brief History of Origins of Algebra
Muhammad ibn Musa al-Khwarizmi (800-847 CE) –
from Morocco wrote a book titled al-jabr about
arithmetic of variables
Book was translated into Latin.
Its title (al-jabr) gave Algebra its name.
8.5
Relational Algebra Overview
Relational Algebra consists of several groups of operations
Unary Relational Operations
SELECT (symbol: (sigma))
PROJECT (symbol: (pi))
RENAME (symbol: (rho))
Relational Algebra Operations From Set Theory
UNION ( ), INTERSECTION ( ), DIFFERENCE (or
MINUS, – )
CARTESIAN PRODUCT ( x )
Binary Relational Operations
JOIN (several variations of JOIN exist)
DIVISION
Additional Relational Operations
OUTER JOINS, OUTER UNION
AGGREGATE FUNCTIONS (These compute summary of
information: for example, SUM, COUNT, AVG, MIN, MAX)
8.6
Database State for COMPANY
All examples discussed below refer to the COMPANY database
shown here.
8.7
SELECT Operation
8.8
Formats of Selection Conditions
A op v
A is an attribute
op is an operator (=, , <, , >, )
v is a constant
Age 20
Name = 'Bill'
A op B
A and B are two attributes in R.
Person(SSN, Name, Birthplace, Residence)
Birthplace = Residence
8.9
An Example of SELECT (1)
Find all students who are 20 years old or younger, and whose
birthplace is the same as his/her residence.
Age 20 and Birthplace = Residence(Student)
8.10
An Example of SELECT (2)
Student Result
SID Name GPA Major SID Name GPA Major
456 John 3.4 CS 456 John 3.4 CS
457 Carl 3.2 CS 457 Carl 3.2 CS
678 Ken 3.5 Math
8.12
An Example of Select (3)
SSN Name Age GPA Birthplace Residence
123456789 John 20 3.2 Vestal Vestal
234567891 Mary 18 2.9 Binghamton Vestal
345678912 Bill 19 2.7 Endwell Endwell
456789123 Nancy 24 3.6 Binghamton NYC
8.13
SELECT Operation
Commutativity of select:
condition-1(condition-2(R))
= condition-2(condition-1(R))
= condition-1 and condition-2(R)
8.14
PROJECT Operation
attribute-list(R)
where attribute-list is a subset of all attributes in R
Semantics:
Returns all tuples of relation R but for each tuple,
only values under attribute-list are returned
8.15
Project (2)
8.16
Project (3)
Input Relation
Output Relation
8.17
Projection (4)
Major(Student)
Student Result
SID Name GPA Major Major
456 John 3.4 CS CS
457 Carl 3.2 CS Math
678 Ken 3.5 Math
8.18
Project (5)
If attribute-list-1 attribute-list-2,
then
attribute-list-1(attribute-list-2(R))
= attribute-list-1(R)
8.19
Project (6)
Renaming of attributes
8.20
Select and Project
Find the name and GPA of all students who are 20 years
old or younger and whose birthplace and residence are
the same
8.21
RENAME Operation
8.22
Renaming
Student CS_Student
SID Name GPA Major SID Name GPA Major
456 John 3.4 CS 456 John 3.4 CS
457 Carl 3.2 CS 457 Carl 3.2 CS
678 Ken 3.5 Math
8.23
Set Theoretic Operations
UNION, INTERSECTION, DIFFERENCE.
binary (applied to two relations at a time)
Two relations
R(A1, A2, …, An) and
S(B1, B2, …, Bm) are said to be union-compatible if
they have the same degree (n = m) and
dom(Ai) = dom(Bi) for 1 i n.
8.24
Union (1)
--- union
Format:
RS
Semantics:
Returns all tuples that belong to either R or S.
Formally:
R S = {t | t r(R) or t r(S)}
Condition of union:
R and S must be union compatible.
Example:
R S RS
A B C A B C A B C
a1 b1 c1 a0 b0 c0 a1 b1 c1
a2 b2 c2 a1 b1 c1 a2 b2 c2
a3 b3 c3 a2 b2 c2 a3 b3 c3
a4 b4 c4 a0 b0 c0
a4 b4 c4
8.26
Set Difference (1)
set difference
Format:
RS
Semantics:
Returns all tuples that belong to R but not S.
Formally:
R S = {t | t r(R) and t r(S)}
8.27
Set Difference (2)
Example:
R S RS
A B C A B C A B C
a1 b1 c1 a0 b0 c0 a3 b3 c3
a2 b2 c2 a1 b1 c1
a3 b3 c3 a2 b2 c2
a4 b4 c4
8.28
INTERSECTION
RS
Semantics:
Returns all tuples that belong to both R and S.
Formally:
R S = {t | t r(R) and t r(S)}
8.29
INTERSECTION
8.30
Examples of Set Operations
TAs RAs
SID Name GPA Major SID Name GPA Major
456 John 3.4 CS 456 John 3.4 CS
457 Carl 3.2 CS 223 Bob 2.95 Ed
678 Ken 3.5 Math
8.31
Cartesian Product (1)
RS
Semantics:
Returns every tuple that can be formed by
concatenating a tuple in R with a tuple in S
8.32
Cartesian Product (2)
Example: R A B C S B D E
a1 b1 c1 b1 d1 e1
a2 b2 c2 b2 d2 e2
a3 b3 c3
RS A R.B C S.B D E
a1 b1 c1 b1 d1 e1
a1 b1 c1 b2 d2 e2
a2 b2 c2 b1 d1 e1
a2 b2 c2 b2 d2 e2
a3 b3 c3 b1 d1 e1
a3 b3 c3 b2 d2 e2
8.33
Example of Cross Product
Student Award
SID Name GPA Major SID Amount Year
456 John 3.4 CS 456 1500 1998
457 Carl 3.2 CS 678 3000 2000
678 Ken 3.5 Math
Student Award
Student.SID Award.SID
SID Name GPA Major SID Amount Year
456 John 3.4 CS 456 1500 1998
456 John 3.4 CS 678 3000 2000
457 Carl 3.2 CS 456 1500 1998
457 Carl 3.2 CS 678 3000 2000
678 Ken 3.5 Math 456 1500 1998
678 Ken 3.5 Math 678 3000 2000
8.34
Cartesian Product (3)
Commutativity: R S = S R
8.35
Cartesian Product (4)
RESULT = Fname,Lname,Dependent-name(ACTUAL-DEP)
8.37
Relational Algebra Example (1)
8.38
Relational Algebra Example (2)
8.39
Relational Algebra Example (3)
8.40
Join (1)
--- join
Format:
R join-condition S
Semantics:
Returns all tuples in R S which satisfy the join condition
8.42
Join (3)
8.43
Join (4)
8.44
Joins
Theta Join.
Format: R
join-condition S
Returns tuples in
join-condition(R S)
Equijoin.
Same as Theta Join except the join-condition
contains only equalities.
Natural Join.
Same as Equijoin except that equality
conditions are on common attributes and
duplicate columns are eliminated.
8.45
Equijoin
8.46
Natural Join (1)
Definition:
A join between R and S is a natural join if
There is an equality comparison between
every pair of identically named attributes
from the two relations
8.47
Natural Join (2)
Example:
R(A, B, C) S(A, C, D)
has attributes (A, B, C, D) in the result
Questions:
How to express natural join in terms of equijoin
and other relational operator?
RS=
R.A, B, S.C, D
(R R.A=S.A and R.C=S.C S)
8.48
Examples of Joins
Student Professor
SID Name GPA Age Prof PID Pname Age Dept
456 John 3.4 29 123 123 John 35 CS
457 Carl 3.2 35 123 154 Scott 28 Math
678 Ken 3.5 25 154
Theta Join.
Student Student.Age <= Professor.Age Professor
Result Student.Age Professor.Age
SID Name GPA Age Prof PID Pname Age Dept
456 John 3.4 29 123 123 John 35 CS
457 Carl 3.2 35 123 123 John 35 CS
678 Ken 3.5 25 154 123 John 35 CS
678 Ken 3.5 25 154 154 Scott 28 Math
8.49
Examples of Joins (cont.)
Equijoin.
Student Prof=PID AND Name=Pname Professor
Result Student.Age Professor.Age
SID Name GPA Age Prof PID Pname Age Dept
456 John 3.4 29 123 123 John 35 CS
Natural Join.
Student Professor
Result
SID Name GPA Age Prof PID Pname Dept
457 Carl 3.2 35 123 123 John CS
8.50
Some Questions About Joins
Consider relations
Students(SSN, Name, GPA, Major, Age, PSSN)
Profs(PSSN, Name, Office, Age, Dept)
8.51
A Complete Set of Relational Algebra
Operations
The relational algebra is a set of expressions as
defined below:
A relation is an expression.
8.52
Aggregate Functions & Grouping
8.53
Examples
8.54
Examples
8.55
Examples
COUNT_SSN AVERAGE_SALARY
8 35125
8.56
Outerjoin (1)
R S RS
A B C C D E A B C D E
a1 b1 c1 c1 d1 e1 a1 b1 c1 d1 e1
a4 b3 c2 c6 d3 e2
R O S
A B C D E
a1 b1 c1 d1 e1
a4 b3 c2 null null
null null c6 d3 e2
8.58
Left Outerjoin and Right Outerjoin
8.59
Left Outerjoin and Right Outerjoin (2)
R S RR
A B C C D E A B C D E
a1 b1 c1 c1 d1 e1 a1 b1 c1 d1 e1
a4 b3 c2 c6 d3 e2
8.60
Left Outerjoin and Right Outerjoin (3)
R LO S
A B C D E
a1 b1 c1 d1 e1
a4 b3 c2 null null
R RO S
A B C D E
a1 b1 c1 d1 e1
null null c6 d3 e2
8.61
Examples of Queries in Relational
Algebra (1)
Many relational algebra queries can be expressed
using selection, projection and join operators by
following steps:
(1) Determine necessary relations to answer the
query.
If R , ..., R are all the relations needed,
1 n
8.62
Relational Algebra Example (2)
8.63
Relational Algebra Example (3)
8.64
Relational Algebra Example (5)
8.65
Relational Algebra Example (6)
(2) Replace
Students by
Enrollment by
CNO='CIS328'(Enrollment)
8.66
Relational Algebra Example (7)
(3) Replace
Student Enrollment by
8.67
Relational Algebra Example (8)
8.68
Relational Algebra Example (9)
8.69
Relational Algebra Summary (1)
8.70
Relational Algebra Summary (2)
Some identities:
C1(C2(R)) = C2(C1(R)) = C1 and C2(R)
L1(L2(R)) = L1(R) , if L1 L2
R1 R2 = R2 R1
R1 R2 = R2 R1