Relational Algebra
Relational Algebra
Database Systems
Lecture 19
Relational Algebra
Database Management
Systems
2
Database Management
Systems
Today’s agenda
• Example Database Application (COMPANY)
• Relational Algebra
▫ Unary Relational Operations
▫ Relational Algebra Operations From Set Theory
3
Database Management
Systems
Database Management
Systems
Relational Algebra
• The basic set of operations for the relational model is known
as the relational algebra. These operations enable a user to
specify basic retrieval requests.
Database Management
Systems
SELECT operation is used to select a subset of the tuples from a relation that
satisfy a selection condition. It is a filter that keeps only those tuples that
satisfy a qualifying condition – those satisfying the condition are selected
while others are discarded.
Example: To select the EMPLOYEE tuples whose department number is
four or those whose salary is greater than $30,000 the following notation is
used:
sDNO = 4 (EMPLOYEE)
sSALARY > 30,000 (EMPLOYEE)
In general, the select operation is denoted by s <selection condition>(R) where the
symbol s (sigma) is used to denote the select operator, and the selection
condition is a Boolean expression specified on the attributes of relation R
6
Database Management
Systems
Database Management
Systems
FIGURE 6.1
Results of SELECT and PROJECT operations. (a) s(DNO=4 AND SALARY>25000) OR (DNO=5 AND
SLARY>30000)(EMPLOYEE). (b) pSEX, SALARY(EMPLOYEE).
8
Database Management
Systems
Database Management
Systems
Database Management
Systems
FIGURE 6.1
Results of SELECT and PROJECT operations. (a) s(DNO=4 AND SALARY>25000) OR (DNO=5 AND
SLARY>30000)(EMPLOYEE). (b) pSEX, SALARY(EMPLOYEE).
11
Database Management
Systems
Database Management
Systems
- r S (B1, B2, …, Bn ) ( R) is a renamed relation S based on R with column names B1, B2,
…..Bn.
- r (B1, B2, …, Bn ) ( R) is a renamed relation with column names B1, B2, …..Bn which
Database Management
Systems
FIGURE 6.2
Results of a sequence of operations. (a) pFNAME, LNAME, SALARY
(sDNO=5(EMPLOYEE)). (b) Using intermediate relations and
renaming of attributes.
14
Database Management
Systems
RESULT2(SSN) ¬ p 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”.
15
Database Management
Systems
Database Management
Systems
STUDENTÈINSTRUCTOR
17
Database Management
Systems
Example: The result of the intersection operation (figure below) includes only
those who are both students and instructors.
STUDENT Ç INSTRUCTOR
18
Database Management
Systems
Example: The figure shows the names of students who are not instructors,
and the names of instructors who are not students.
STUDENT-INSTRUCTOR
INSTRUCTOR-
STUDENT
19
Database Management
Systems
Database Management
Systems
Database Management
Systems
FIGURE≈6.5a
The CARTESIAN PRODUCT (CROSS PRODUCT) operation.
22
Database Management
Systems
FIGURE≈6.5b
The CARTESIAN PRODUCT (CROSS PRODUCT) operation.
23
Database Management
Systems
FIGURE≈6.5c
The CARTESIAN PRODUCT (CROSS PRODUCT) operation.