Lecture 06: Relational Algebra & Relational Calculus: Dr. Dang Tran Khanh
Lecture 06: Relational Algebra & Relational Calculus: Dr. Dang Tran Khanh
Relational Calculus
SELECT Operation
SELECT operation is used to select a subset of the tuples from a relation that
satisfy a selection condition
σ <selection condition>(R)
where the symbol σ (sigma) is used to denote the select operator, and
the selection condition is a Boolean expression specified on the
attributes of relation R
PROJECT Operation
This operation selects certain columns from the table and discards the other
columns
Example: To list each employee’s first and last name and salary, the
following is used:
π LNAME, FNAME,SALARY(EMPLOYEE)
π
The general form of the project operation is <attribute list>(R) where π
(pi) is the symbol used to represent the project operation and <attribute list>
is the desired list of attributes from the attributes of relation R.
The project operation removes any duplicate tuples, so the result of the
project operation is a set of tuples
Rename Operation
– We may want to apply several relational algebra operations one after the other.
Either we can write the operations as a single relational algebra expression by
nesting the operations, or we can apply one operation at a time and create
intermediate result relations. In the latter case, we must give names to the
relations that hold the intermediate results
Example: To retrieve the first name, last name, and salary of all employees who
work in department number 5, we must apply a select and a project operation. We
can write a single relational algebra expression as follows:
π FNAME, LNAME, SALARY(σ DNO=5 (EMPLOYEE))
or we can explicitly show the sequence of operations, giving a name to each
intermediate relation:
DEP5_EMPS ← σ DNO=5(EMPLOYEE)
RESULT ← π FNAME, LNAME, SALARY (DEP5_EMPS)
UNION Operation
The result of this operation, denoted by R ∪ S, is a relation that includes all tuples that
are either in R or in S or in both R and S. Duplicate tuples are eliminated.
Example: To retrieve the social security numbers of all employees who either work in
department 5 or directly supervise an employee who works in department 5, we can use
the union operation as follows:
DEP5_EMPS ← σ DNO=5 (EMPLOYEE)
RESULT1 ← π SSN(DEP5_EMPS)
RESULT2(SSN) ← π SUPERSSN(DEP5_EMPS)
RESULT ← RESULT1 ∪ RESULT2
The union operation produces the tuples that are in either RESULT1 or RESULT2 or
both. The two operands must be “type compatible”
Type Compatibility
– The operand relations R1(A1, A2, ..., An) and R2(B1,
B2, ..., Bn) must have the same number of
attributes, and the domains of corresponding
attributes must be compatible; that is,
dom(Ai)=dom(Bi) for i=1, 2, ..., n
– The resulting relation for R1∪R2,R1 ∩ R2, or R1-R2
has the same attribute names as the first
operand relation R1 (by convention)
Dr. Dang Tran Khanh ([email protected]) 16
The set operations UNION, INTERSECTION, and MINUS
(a) Two union-compatible relations
(b) STUDENT ∪ INSTRUCTOR
(c) STUDENT ∩ INSTRUCTOR
(d) STUDENT – INSTRUCTOR
(e) INSTRUCTOR – STUDENT
Relational Algebra
Relational Algebra Operations From Set Theory
JOIN Operation
– The sequence of cartesian product followed by select is
used quite commonly to identify and select related tuples
from two relations, a special operation, called JOIN. It is
denoted by a
– This operation is very important for any relational database
with more than a single relation, because it allows us to
process relationships among relations.
– The general form of a join operation on two relations R(A1,
A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R <join condition>S
where R and S can be any relations that result from general
relational algebra expressions
EQUIJOIN Operation
The most common use of join involves join conditions with equality comparisons only.
Such a join, where the only comparison operator used is =, is called an EQUIJOIN. In
the result of an EQUIJOIN we always have one or more pairs of attributes (whose
names need not be identical) that have identical values in every tuple
The JOIN seen in the previous example was EQUIJOIN
NATURAL JOIN Operation
Because one of each pair of attributes with identical values is superfluous, a new
operation called natural join—denoted by *—was created to get rid of the second
(superfluous) attribute in an EQUIJOIN condition
The standard definition of natural join requires that the two join attributes, or each pair
of corresponding join attributes, have the same name in both relations. If this is not the
case, a renaming operation is applied first
DIVISION Operation
– The division operation is applied to two relations R(Z)÷S(X),
where Z = X ∪ Y (Y is the set of attributes of R that are not
attributes of S
– The result of DIVISION is a relation T(Y) that includes a
tuple t if tuples tR appear in R with tR [Y] = t, and with
tR [X] = ts for every tuple ts in S, i.e., for a tuple t to appear
in the result T of the DIVISION, the values in t must appear
in R in combination with every tuple in S
More details:
[1] Chapter 6
DNOEMPLOYEE)
Example: To find the first and last names of all employees whose salary is
above $50,000, we can write the following tuple calculus expression:
{t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SALARY>50000}
The condition EMPLOYEE(t) specifies that the range relation of tuple
variable t is EMPLOYEE. The first and last name (π FNAME, LNAME) of each
EMPLOYEE tuple t that satisfies the condition t.SALARY>50000 (σ SALARY
>50000) will be retrieved
{uv | (∃ q) (∃ r) (∃ s) (∃ t) (∃ w) (∃ x) (∃ y) (∃ z)
(EMPLOYEE(qrstuvwxyz) and q=’John’ and r=’B’
and s=’Smith’)}
Relational Algebra
– Unary Relational Operations
– Relational Algebra Operations From Set Theory
– Binary Relational Operations
– Additional Relational Operations
– Examples of Queries in Relational Algebra
Relational Calculus
– Tuple Relational Calculus
– Domain Relational Calculus
Pre-Midterm Exam (60’)
Reading Suggestion & Homework: do not forget !!
Next Lecture: (after Midterm Exam on April 09, 2006)
– SQL (Structured Query Language)
– [1]: Chapters 8, 9