Relational Algebra in DBMS
Relational Algebra in DBMS
Chapter Outline
Example Database Application (COMPANY)
Relational Algebra
– Unary Relational Operations
– Relational Algebra Operations From Set Theory
– Binary Relational Operations
– Additional Relational Operations
– Examples of Queries in Relational Algebra
Database State for COMPANY
All examples discussed below refer to the COMPANY database shown here.
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.
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
Unary Relational Operations
SELECT Operation Properties
– The SELECT operation <selection condition>(R) produces a relation S
that has the same schema as R
–
<list2> R)<list1> Ras long
<list1>
as<list2>contains theattributes in<list2>
Unary Relational Operations (cont.)
Unary Relational Operations (cont.)
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:
S (B1, B2, …, Bn ) ( R) is a renamed relationS based on R with column names B1, B1, …..Bn
S ( R) is a renamed relationS based on R (which does not specify column names).
(B1, B2, …, Bn ) ( R) is a renamed relationwith column names B1, B1, …..Bn which does
STUDENTINSTRUCTOR
Relational Algebra Operations From Set
Theory (cont.)
Relational Algebra Operations From Set
Theory (cont.)
INTERSECTION OPERATION
The result of this operation, denoted by R S, is a relation that includes all
tuples that are in both R and S. The two operands must be "type compatible"
STUDENT INSTRUCTOR
Relational Algebra Operations From Set
Theory (cont.)
Set Difference (or MINUS) Operation
The result of this operation, denoted by R - S, is a relation that includes all
tuples that are in R but not in S. The two operands must be "type compatible”.
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
Relational Algebra Operations From Set
Theory (cont.)
Notice that both union and intersection are commutative
operations; that is
R S = S R, and R S = S R
Example:
FEMALE_EMPS SEX=’F’(EMPLOYEE)
EMPNAMES FNAME, LNAME, SSN (FEMALE_EMPS)
DIVISION Operation
– The division operation is applied to two relations
R(Z) S(X), where X subset Z. Let Y = Z - X (and hence Z
= X Y); that is, let Y be 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.