DB Lec 19
DB Lec 19
Database Systems
Relational Algebra
Database Management
Systems
2
Database Management S
ystems
Today’s agenda
• Example Database Application (COMPANY)
• Relational Algebra
▫ Unary Relational Operations
▫ Relational Algebra Operations From Set Theory
3
Database Management S
ystems
Database Management S
ystems
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 S
ystems
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:
DNO = 4 (EMPLOYEE)
SALARY > 30,000 (EMPLOYEE)
In general, the select operation is denoted by <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
6
Database Management S
ystems
Database Management S
ystems
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 S
ystems
Database Management S
ystems
- S (B1, B2, …, Bn ) ( R) is a renamed relation S based on R with column names B1, B2,
…..Bn.
- (B1, B2, …, Bn ) ( R) is a renamed relation with column names B1, B2, …..Bn
Database Management S
ystems
FIGURE 6.2
Results of a sequence of operations. (a) pFNAME, LNAME, SALARY
(sDNO=5(EMPLOYEE)). (b) Using intermediate relations and
renaming of attributes.
11
Database Management S
ystems
• 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”.
12
Database Management S
ystems
Database Management S
ystems
STUDENTINSTRUCTOR
14
Database Management S
ystems
STUDENT INSTRUCTOR
15
Database Management S
ystems
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
16
Database Management S
ystems
Database Management S
ystems
FIGURE≈6.5a
The CARTESIAN PRODUCT (CROSS PRODUCT) operation.
18
Database Management S
ystems
FIGURE≈6.5b
The CARTESIAN PRODUCT (CROSS PRODUCT) operation.
19
Database Management S
ystems
FIGURE≈6.5c
The CARTESIAN PRODUCT (CROSS PRODUCT) operation.