Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

Chapter 8

The Relational
Algebra and
Calculus

Prepared By
Dr. Alaa Hosni
• Historically, the relational algebra and calculus were developed
before the SQL language.
• SQL is based on concepts from both the algebra and the calculus .

• Recall from Chapter 2 that a data model must include a set of


operations to manipulate the database, and the data model’s
concepts for defining the database’s structure and

Chapter 8-2
⚫ Relational algebra : the basic set of operations for the
relational model .

⚫ The result of a retrieval is a new relation, which may have


been formed from one or more relations.

⚫ The new relations, can be manipulated using operations of


the same algebra.
⚫ Relational algebra expression: a sequence of relational
algebra operations, whose result will also be a relation

Chapter 8-3
⚫ SQL describes the practical language for the
relational model.
⚫ SQL is based on concepts from both the algebra
and the calculus.

Chapter 8-4
⚫ Importance of relational algebra:
1. It provides a formal foundation for relational model operations.
2. It is used as a basis for implementing queries
3. Optimizing the query processing (optimization modules-
integral parts RDBMSs)

Chapter 8-5
Manages
Works_for

controls

DEPENDENT_OF

Relational database schema.


Chapter 8-6
8.1 Unary Relational Operations
⚫ SELECT operation is used to select a subset of the tuples (horizontal
partition ) from a relation that satisfy a selection condition.
⚫ In general, the SELECT operation is denoted by
 <selection condition> (R)
⚫ The relation resulting from the SELECT operation has the
same attributes as R.

Example
To select the EMPLOYEE tuples whose department number is four:
 DNO=4 (EMPLOEE)

Chapter 8-7
⚫ The selection condition is a Boolean expression specified
on the attributes of relation R

⚫ All the comparison operators in the set {=, <, ≤, >, ≥, ≠} can
apply to attributes whose domains are ordered values, such as
– numeric or
– date domains.
– Domains of strings of characters

⚫ The Boolean expression specified in <selection condition> is


made up of a number of clauses of the form
<attribute name> <comparison op> <constant value>
or
<attribute name> <comparison op> <attribute name>

Chapter 8-8
⚫ If the domain of an attribute is a set of unordered values,
then only the comparison operators in the set {=, ≠ } can be
used.
Example
The following set is an unordered domain
Color = { ‘red’, ‘blue’, ‘green’, ‘white’, ‘yellow’, ...}, where no
order is specified among the various colors.
⚫ Additional types of comparison operators:
A domain of character strings may allow the comparison
operator SUBSTRING_OF.

Chapter 8-9
⚫ Standard Boolean operators and, or, and not can be
used to form a general selection condition.

Example
To select the tuples for all employees who either work in
department 4 and make over $25,000 per year, or work in
department 5 and make over $30,000, we can specify the
following SELECT operation:
σ(Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000)(EMPLOYEE)

Chapter 8-10
Chapter 8-11
⚫ The SELECT operator is unary: applied to a single relation.

⚫ Notice that the SELECT operation is commutative; that is,


<cond1>( <cond2>(R)) =  <cond2>( <cond1>(R))

Chapter 8-12
❑SELECT operation may be applied in any order; i.e.,
 <condition1>( < condition2> ( <condition3> ( R))
=  <condition2> ( < condition3> ( < condition1> (R)))

❑A cascaded SELECT operation may be replaced by a single


selection with a conjunction of all the conditions; i.e.,
 <condition1>( < condition2> ( <condition3> ( R))
=  <condition1> AND < condition2> AND < condition3> ( R)))

Chapter 8-13
8.1.2 The PROJECT Operation
⚫ This operation selects certain columns from the table
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)
<attribute list> : the desired list of attributes from R.

⚫ The project operation removes any duplicate tuples.

Chapter 8-14
Chapter 8-15
PROJECT Operation Properties

▪ If key(R)  <attribute list> , then the number of tuples is


equal to the number of tuples in R.

⚫  <list1> ( <list2> (R) ) =  <list1> (R) as long as <list1> <list2>

Chapter 8-16
8.1.3 Sequences of Operations and the RENAME Operation

⚫ To apply several relational algebra operations :


– write the operations as a single relational algebra expression
or
– apply one operation at a time and create intermediate result relations.

Chapter 8-17
Example
To retrieve the Fname, Lname, and salary of all employees who
work in Dnumber 5, we can write a single relational algebra
expression as follows:
FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))

Chapter 8-18
OR We can explicitly show the sequence of operations, giving a
name to each intermediate relation:
D5_EMPS   DNO=5(EMPLOYEE)
RESULT   FNAME, LNAME, SALARY (D5_EMPS)

⚫ It is simpler to break down a complex sequence of operations by


specifying intermediate result relations than to write a single
relational algebra expression

⚫ To rename the attributes in a relation, we simply list the


new attribute names in parentheses:
T   DNO=5(EMPLOYEE)
R(First_name, Last_name, Salary)   FNAME, LNAME, SALARY (T)
⚫ We can also define a formal RENAME operation 

Chapter 8-19
⚫ The rename operator is 
⚫  can be used as follows:
1)  S (B1, B2, …, Bn ) ( R)
where S is the new relation based on R, B1, B1, …..Bn are the
new attribute names
2)  S (R) is a renamed relation S based on R (which does not
specify column names).
3)  (B1, B2, …, Bn ) ( R) is a renamed relation with column names
B1, B1, …..Bn (the same relation name).

Chapter 8-20
⚫In SQL,
SELECT E.Fname AS First_name, E.Lname AS
Last_name, E.Salary AS Salary
FROM EMPLOYEE AS E
WHERE E.Dno=5;

Chapter 8-21
8.2 Relational Algebra Operations From Set Theory

8.2.1 The UNION, INTERSECTION, and MINUS Operations


• R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are said to be union
compatible (or type compatible) if
1. they have the same degree n and
2. dom(Ai) = dom(Bi) for 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).

Chapter 8-22
⚫ Assume that the relations R and S are union-compatible
⚫ We can define the three operations UNION,
INTERSECTION, and SET DIFFERENCE as follows:
■ 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.
■ R ∩ S, is a relation that includes all tuples that are in both R and S.
■ R – S, is a relation that includes all tuples that are in R but not in S.

Chapter 8-23
Example
D5_EMPS  DNO=5 (EMPLOYEE)
RESULT1   SSN(D5_EMPS)
RESULT2 (SSN)  SUPERSSN(D5_EMPS)
RESULT  RESULT1  RESULT2

RESULT : the SSN of all employees who either work in department 5 or directly
supervise an employee who works in department 5.

Chapter 8-24
Chapter 8-25
⚫ Notice that
R ∪S=S∪R,R∩S=S∩R
R ∪ (S ∪ T ) = (R ∪ S ) ∪ T
(R ∩ S ) ∩ T = R ∩ (S ∩ T )

⚫ The ‘-’ operation is not commutative:


R−S≠S−R

Chapter 8-26
Chapter 8-27
8.2.2 The CARTESIAN PRODUCT Operation
⚫ CARTESIAN (or cross product) Operation
– The result of R(A1, A2, . . ., An)  S(B1, B2, . . ., Bm) is a
relation Q with degree n + m attributes
Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.

The resulting relation Q has one tuple for each combination of


tuples—one from R and one from S.
– Hence, if R has nR tuples, and S has nS tuples, then
| R  S | will have nR * nS tuples.

Chapter 8-28
⚫ Suppose that we want to retrieve a list of names of each
female employee’s dependents:

Example
FEMALE_EMPS   SEX=’F’(EMPLOYEE)
EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)
EMP_DEPENDENTS  EMPNAMES  DEPENDENT
ACTUAL_DEPENDENTS ← σSsn=Essn(EMP_DEPENDENTS)

RESULT ←  FNAME, LNAME, Dependent_name (ACTUAL_DEPENDENTS)

Chapter 8-29
Chapter 8-30
Chapter 8-31
Chapter 8-32
8.3 Binary Relational Operations: JOIN and DIVISION
8.3.1 JOIN Operation
⚫ The JOIN operation : the sequence of Cartesian product
followed by select is called JOIN.

⚫ 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.

Chapter 8-33
Example
⚫ To retrieve the name of the manager of each department:
– combine each DEPARTMENT tuple with the EMPLOYEE tuple
whose SSN value matches the MGRSSN value in the department
tuple. We do this by using the join operation.
DEPT_MGR  DEPARTMENT MGRSSN=SSN
EMPLOYEE
RESULT← Dname,Fname, Lname (DEPT_MGR)
(EQUIJOIN: '=')

Chapter 8-34
⚫ General join condition of is of the form
<condition> AND <condition> AND...AND <condition>
<condition> is of the form Ai  Bi ,
Ai is an attribute of R,
Bi is an attribute of S,
Ai and Bi have the same domain, and
 is one of the comparison operators {=, <, , , >, , }.
⚫ A JOIN operation with such a general join condition is called a
THETA JOIN.
⚫ Tuples whose join attributes are NULL or for which the join
condition is FALSE do not appear in the result.

Chapter 8-35
8.3.2 Variations of JOIN: The EQUIJOIN
and NATURAL JOIN

⚫ The use of join with equality comparisons, is called an EQUIJOIN.


⚫ With EQUIJOIN, one of each pair of attributes with identical values
is superfluous.
⚫ natural join : the 2 join attribs. have the same name in both relations.
⚫ natural join ( *) : the second (superfluous) attribute in an
EQUIJOIN condition is eliminated.
⚫ If this is not the case, a renaming operation is applied first.

Chapter 8-36
⚫ Suppose we want to combine each PROJECT tuple with the
DEPARTMENT tuple that controls the project.
⚫ First we rename the Dnumber attribute of DEPARTMENT to
Dnum :

PROJ_DEPTPROJECT (Dname , , , (DEPARTMENT)


Dnum Mgr_ssn Mgr_start_date)

Or
DEPT ← ρ(Dname, Dnum, Mgr_ssn, Mgr_start_date)(DEPARTMENT)
PROJ_DEPT ← PROJECT  DEPT

Chapter 8-37
Chapter 8-38
Example
To apply a natural join on the DNUMBER attributes of DEPARTMENT and
DEPT_LOCATIONS, it is sufficient to write:
DEPT_LOCS  DEPARTMENT * DEPT_LOCATIONS

Chapter 8-39
8.3.3 Complete Set of Relational Operations
⚫ The set of operations { ,  , , , - , } is called a complete set
because any other relational algebra expression can be
expressed by a combination of these five operations.

Example
1. R  S = (R  S ) – ((R - S)  (S - R))
2. R <join condition>S =  <join condition> (R  S)
3. NATURAL JOIN can be specified as a CARTESIAN
PRODUCT preceded by RENAME and followed by SELECT
and PROJECT operations.

Chapter 8-40
8.4 Additional Relational Operations
8.4.1 Generalized Projection
⚫ The generalized projection operation allows functions of attributes
to be included in the projection list.

⚫ The generalized form :


F , F , ..., F (R)
1 2 n

F1, F2, ..., Fn : functions over the attributes in R and may


involve arithmetic operations and constant values.

Chapter 8-45
Example
Consider the relation
EMPLOYEE (Ssn, Salary, Deduction, Years_service)

⚫ A report may be required to show


– Net Salary = Salary – Deduction,
– Bonus = 2000 * Years_service, and
– Tax = 0.25 * Salary.

Chapter 8-46
⚫ Then a generalized projection combined with renaming may
be used as follows:

EMPLOYEE (Ssn, Salary, Deduction, Years_service)

REPORT ← ρ(Ssn, Net-salary, Bonus, Tax) (


Ssn, Salary – Deduction, 2000 * Years_service, 0.25 * Salary(EMPLOYEE)
).

Chapter 8-47
8.4.2 Aggregate Functions and Grouping
• Such functions include retrieving the average or total salary of
all employees or the total number of employee tuples.

• Functions applied to collections of numeric values include


SUM, AVERAGE, MAXIMUM, and MINIMUM. The COUNT
function is used for counting tuples or values.

Chapter 8-48
⚫ AGGREGATE FUNCTION operation is defined as
<grouping attributes> ℑ <function list> (R )

- <grouping attributes> : is a list of attributes of R,


<function list> is a list of (<function> <attribute>) pairs.
⚫ <function> is one of the allowed functions

Chapter 8-49
Example
⚫ To retrieve each department number, the number of employees in
the department, and their average salary, while renaming the
resulting attributes as indicated below:

Agg  Dno ℑCOUNT Ssn, AVERAGE Salary (EMPLOYEE)


ρR(Dno, No_of_employees, Average_sal)(Agg)
⚫ If no renaming: then the attributes corresponding to the function list will
each be the concatenation of the function name with the attribute name in
the form
<function>_<attribute>

Chapter 8-50
⚫ If no grouping attributes are specified:
– the functions are applied to all the tuples
– the resulting relation has a single tuple only.

⚫ Figure 8.10(c) shows the result of the following


operation:
ℑ COUNT Ssn, AVERAGE Salary(EMPLOYEE)

Chapter 8-51
Chapter 8-52
8.4.4 OUTER JOIN Operations
▪ In NATURAL JOIN
▪ tuples without a matching tuple are eliminated from the join result.
▪ Tuples with null in the join attributes are also eliminated.

▪ outer joins can be used when we want to keep all the tuples in R, or all
those in S, or all those in both relations in the result of the join .

▪ The left outer join operation keeps every tuple in the first or left relation R in
R S; if no matching tuple is found in S, then the attributes of S in the join
result are filled or “padded” with null values.

Chapter 8-54
Suppose that we want a list of all employee names as well as the
name of the departments they manage if they happen to manage
a department; if they do not manage one, we can indicate it with a
NULL value

Chapter 8-55
⚫ A similar operation, right outer join, keeps every tuple in
the second or right relation S in the result of R S.
⚫ A third operation, full outer join, denoted by keeps all
tuples in both the left and the right relations when no
matching tuples are found, padding them with null values
as needed.

Chapter 8-56
8.5 Examples of Queries in Relational Algebra
⚫ Q1: Retrieve the name and address of all employees who work for the
‘Research’ department.
RESEARCH_DEPT  DAME=‘Research’ (DEPARTMENT)
RESEARCH_EMPS  (RESEARCH_DEPT DNUMBER= DNOEMPLOYEE EMPLOYEE)

RESULT  FNAME, LNAME, ADDRESS (RESEARCH_EMPS)

⚫ Q8: Retrieve the names of employees who have no dependents.


ALL_EMPS  SSN (EMPLOYEE)

EMPS_WITH_DEPS(SSN)  ESSN (DEPENDENT)


EMPS_WITHOUT_DEPS  (ALL_EMPS - EMPS_WITH_DEPS)

RESULT   LNME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)


• Read examples Q2, Q3, Q4, Q 5, Q7
Chapter 8-59

You might also like