Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Relational Algebra
Lecturer Dr Pavle Mogin
COMP302
Database Systems
Relational Algebra 1
Relational Algebra
One of the relational data model languages
The others are Tuple Calculus, Domain Calculus,
SQL
Important for query optimization
A SQL query is transformed into a relational algebra
equivalent, optimized and executed
Relational Algebra 2
Relational Algebra 3
Select operation
It is used to select such a subset of tuples from a
relation that satisfy given condition
Notation: C (r (N ))
Condition C is a Boolean expression on attributes of
R in N(R, C)
Boolean expression is made up of clauses of the form
A a or A B, where adom (A ), { =, <, >, <, >, },
and A, B R, ( is equivalent to <>)
Clauses can be connected by Boolean operators , ,
to form new clauses
COMP302 Database Systems 2007
Relational Algebra 4
Major
Smith
Susan
131313 Comp
Bond
James
007007 Math
Smith
Susan
555555 Comp
Cecil
John
010101 Math
Course
PName CourId
Points
Dept
DB Sys C302
15
Comp
SofEng C301
15
Comp
DisMat
M214
22
Math
Pr&Sys C201
22
Comp
Grades
StudId
CourId
Grade
007007
C302
A+
555555
C302
007007
C301
007007
M214
A+
131313
C201
B-
555555
C201
131313
C302
007007
C201
010101
C201
Relational Algebra 5
James
007007 Math
Student2
LName FName StudId
Major
Smith
Susan
131313 Comp
Smith
Susan
555555 Comp
Relational Algebra 6
Project Operation
Project operation produces a new relation by
choosing some of the columns from the starting
relation, and dropping all the others
Notation:
ALr (N ),
where AL is a attribute sublist from R in N (R, C)
If AL = (Al,, Ak), then ALr (R ) = r (N )[Al,, Ak]
Student1
e.g.
Lname
FName
Smith
Susan
Bond
James
Cecil
John
Relational Algebra 7
AL (C r (R ))
e.g.
Student3
FName
LName
James
Bond
Relational Algebra 8
Join Operation
Join operation joins two relations by merging those
tuples from two relations that satisfy a given condition
The condition is defined on attributes belonging to relations to
be joined
Relational Algebra 9
Relational Algebra 10
Equijoin Operation
e.g.
Let X = Student.StudId and Y = Grades.StudId and
r (Stud_Grades) = r (Student ) X = Y r (Grades ),
then
Relational Algebra 11
Stud_Grades
StudId
StudId
Susan
131313
131313
Comp
C201
B-
Smith
Susan
131313
131313
Comp
C302
Bond
James
007007
007007
Math
C302
A+
Bond
James
007007
007007
Math
C301
Bond
James
007007
007007
Math
M214
A+
Bond
James
007007
007007
Math
C201
Smith
Susan
555555
555555
Comp
C201
Smith
Susan
555555
555555
Comp
C302
Cecil
John
010101
010101
Math
C201
LName
FName
Smith
Major
CourId
Grade
Relational Algebra 12
Relational Algebra 13
Cartesian product
R = R1 R2, degree(r(N1) r(N2)) = degree(r(N1)) +
degree(r(N2 )),
|r (N1) r (N2)| = |r (N1) | |r (N2)|
COMP302 Database Systems 2007
Relational Algebra 14
r (BC )
Relational Algebra 15
Relational Algebra 16
where:
<grouping attributes> is a list of attributes from R,
Y (pronounced as "script F") is the symbol used to denote
aggregate operation, and
<(function, attribute)_list> is a list of pairs (aggregate function
from {SUM, AVERAGE, COUNT, MIN, MAX }, attribute from
R)
Relational Algebra 17
Outer Join
Introduced to insert those tuples that don't match, or
contain null values for join attributes into join relation
Notation:
LEFT, RIGHT, and FULL outer join
Example:
r (AB )
A
B = B
r (BC )
r (ABBC )
Relational Algebra 18
Summary
Relational algebra:
Relational database operations,
Set theoretic operations
Additional operations
Relational Algebra 19
Next topic
Relational Algebra 20