The Relational Algebra and Calculus
The Relational Algebra and Calculus
The Relational Algebra and Calculus
Slide 2
Relational Algebra Overview
Slide 3
Relational Algebra Overview (continued)
Slide 4
Slide 5
The following
query results
refer to this
database
state
Slide 6
Unary Relational Operations: SELECT
DNO = 4 (EMPLOYEE)
– Select the EMPLOYEEs whose salary > $30,000:
Slide 7
SELECT Operation Properties
Slide 8
Unary Relational Operations: PROJECT
LNAME, FNAME,SALARY(EMPLOYEE)
Slide 9
PROJECT (cont.)
<attribute list>(R)
<attribute list> is the desired list of attributes from relation
R.
The project operation removes any duplicate tuples
– This is because the result of the project operation is still a relation, that is, a set
of tuples
Slide 10
PROJECT Operation Properties
Slide 11
Examples of applying SELECT and PROJECT
operations
Slide 12
Single expression versus sequence of
relational operations (Example)
Task: To retrieve the first name, last name, and salary of all
employees who work in department number 5.
Solution 1: A single relational algebra expression:
– FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))
Slide 13
Example of applying multiple operations and
RENAME
LNAME, FNAME,SALARY(Dno=5 (EMPLOYEE))
Slide 15
Unary Relational Operations: RENAME
Slide 16
Example of applying multiple operations and
RENAME
Temp = Dno=5 (EMPLOYEE)
Slide 17
Relational Algebra Operations from
Set Theory
Type Compatibility of operands is required for the binary set
operation UNION , (also for INTERSECTION , and
SET DIFFERENCE –, see next slides)
R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are type
compatible if:
– they have the same number of attributes, and
– the domains of corresponding attributes are type compatible
(i.e. dom(Ai)=dom(Bi) for i=1, 2, ..., n).
The resulting relation for R1R2 (also for R1R2, or R1–R2)
has the same attribute names as the first operand relation
R1 (by convention)
Slide 18
Example Uses of UNION
Slide 19
Union
INS - STU
Slide 20
Troubles with Attribute Names
We define the domain Date (mm/dd/yyyy).
R1 (Birth_date, SSN), where Birth_date is of domain Date
R2 (Death_date, SSN), where Death_date is also of
domain Date.
R3 = R1 R2 will have the attribute list (Birth_date, SSN),
which sounds weird.
R3 is best named as
R3(BorD_date, SSN) = R1 R2
Or more formally,
R3 = (BorD_date, SSN) (R1 R2)
Slide 21
Exercises
Give the SSNs of those managers have dependent(s).
Slide 22
Properties of Set Operations
Notice that both union and intersection are commutative;
that is
– R S = S R, and R S = S R
Slide 23
Relational Algebra Operations from Set
Theory: CARTESIAN PRODUCT
CARTESIAN (or CROSS) PRODUCT Operation
– This operation is used to combine tuples from two relations
in a combinatorial fashion.
– Denoted by R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm)
– Result is a relation Q with degree n + m attributes:
• Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
– The resulting relation state has one tuple for each
combination of tuples—one from R and one from S.
– Hence, if R has nR tuples (denoted as |R| = nR ), and S has
nS tuples, then R x S will have nR * nS tuples.
– The two operands do NOT have to be "type compatible”
Slide 24
CARTESIAN PRODUCT (cont.)
Slide 25
Example of applying
CARTESIAN PRODUCT
Slide 26
More Exercises
Slide 27
JOIN Operator ( )
The sequence of CARTESIAN PRODECT followed by
SELECT is used quite commonly to identify and select
related tuples from two relations
A special operation, called JOIN combines this sequence
into a single operation
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.
Slide 28
Exercise
Slide 29
Exercises
List the names of department managers.
Use Cartesian Product and Select
Use Join
Slide 30
Binary Relational Operations: EQUIJOIN
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 an EQUIJOIN.
Slide 31
NATURAL JOIN Operations
Slide 32
Exercise
Slide 33
Results of
Natural JOINs
Slide 34
NATURAL JOIN: Example 2
Because of the same attribute name Dnumber, Natural Join
can be applied directly to DEPARTMENT and
DEP_LOCATION
– DEPT_LOCS DEPARTMENT *
DEPT_LOCATIONS
Slide 35
NATURAL JOIN (contd.)
Slide 36
Slide 37
Exercise
List names and SSNs of those EMPLOYEE who work on
some project(s) located in Houston.
Slide 38
Exercise
List the names and birth dates of all female dependents
born after 1980.
Slide 39
Exercise
List the names of all employees who earns
salary $10,000 but does not have a
supervisor.
Slide 40
Exercises
List the names of all employees who earns
salary $10,000 but does not supervise
anyone.
Slide 41
Representation of Relations
STUDENTLOAN
loanNo dateIssued Amount interest Institute StudentId
STUDENT
Institute Stdid Fathername
Mapping One-to-Many/Many-
to-One Binary Relationships
• Let S be the participating entity on the many-side and T
the other entity. Let S' and T' be the corresponding
relations.
• Include primary key of T' as foreign key in S‘.
• Include any simple attribute (and simple components of
composite attributes) of reltionship set as attributes of S‘.
Example
Mapping Many-to- Many
Binary Relationships
• Make a separate relation T for this relationship R
between entity sets E1 and E2.
• Let R1 and R2 be the relations corresponding to E1 and
E2 .
• Include primary key attributes of R1 and R2 as foreign
keys in T. Their combination is the primary key in T.
Example
1 M
SUPPLY
Sname Part_no Proj_name quantity
VEHICLE
Example
Relational Schema
Acknowledgements