Elational Lgebra
Elational Lgebra
RELATIONAL ALGEBRA
There are two types of operations in RDBMS
⚫ Retrieval
⚫ Update
Select σ is commutative:
σ <condition1>(σ < condition2> (R)) = σ <condition2> (σ < condition1> (R))
Cascade of Select operations
σ<cond1>(σ< cond2> (σ<cond3>(R)) = σ <cond1> AND < cond2> AND < cond3>(R)))
PROJECT OPERATION (UNARY OPERATION)
Renaming of attributes
⚫ D5 ← σ DNO=5(EMPLOYEE)
⚫ R (FirstName,LastName,Salary) ← π FNAME, LNAME, SALARY (D5)
EXAMPLE OF APPLYING MULTIPLE OPERATIONS AND
RENAME
D5 ← σ DNO=5(EMPLOYEE)
R (First_name,Last_name,Salary) ← π Fname, Lname, Salary (D5)
RENAME OPEARATION
Rename operator is denoted by ρ (rho)
Rename operation ρ can be expressed as:
⚫ ρS(R) rename the relation to S
⚫ ρ(B1, B2, …, Bn )(R) rename the attributes to B1, B2, …..Bn
⚫ ρS (B1, B2, …, Bn )(R) rename both relation to S, and attributes to B1, B1,
…..Bn
Example:
⚫ ρ RESULT (First_Name,Last_Name, DNO)(D5)
UNION (BINARY OPERATION)
The result of R ∪ S, is a relation that includes all tuples that are
either in R or in S or in both R and S
16
RELATIONAL ALGEBRA OPERATIONS FROM SET
THEORY
Problem:
P
Retrieve a list of each female E_D
employee’s dependents
Example (meaningful):
A_DP ← σ SSN=ESSN(E_DP)
R ← π FNAME, LNAME, DEPENDENT_NAME(A_DP)
P
A_D
R
JOIN(BINARY OPERATION)
R <join condition>
S
21
EXAMPLE OF JOIN OPERATION
Retrieve the name of the manager of each department.
22
COMPLETE SET OF RELATIONAL OPERATIONS
The set of operations including
⚫ SELECT σ,
⚫ PROJECT π ,
⚫ UNION ∪,
⚫ DIFFERENCE − ,
⚫ RENAME ρ, and
⚫ CARTESIAN PRODUCT X
is called a complete set because any relational
algebra expression can be expressed using these.
For example:
⚫ R ∩ S = (R ∪ S ) – ((R − S) ∪ (S − R))
⚫ R <join condition>
S = σ <join condition> (R X S)
23
SOME PROPERTIES OF JOIN
Consider the following JOIN operation:
⚫ R(A1, A2, . . ., An) S(B1, B2, . . ., Bm)
R.Ai=S.Bj
⚫ Result is a relation Q with degree n + m attributes:
Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
25
EQUI-JOIN
EQUIJOIN is a join condition that involves only equality operator
=.
Example:
⚫ DEPT_MGR ← DEPARTMENT MGRSSN=SSN EMPLOYEE
⚫ Retrieve a list of each female employee’s dependents
F ← σ SEX=’F’(EMPLOYEE)
EN ← π FNAME, LNAME, SSN (F)
E_DP ← EN DEPENDENT
SSN=ESSN
26
ISSUE WITH EQUIJOIN OPERATION
Superfluous column
28
NATURAL JOIN OPERATION
Example: To apply a natural join on the DNUMBER attributes of
DEPARTMENT and DEPT_LOCATIONS, it is sufficient to write:
29
EXAMPLE: NATURAL JOIN
30
EXAMPLE OF NATURAL JOIN OPERATION
31
DIVISION (BINARY OPERATION)
⚫ The division operation is applied to two relations
R(Z) ÷ S(X), where X ⊂ Z.
⚫ Let Y = Z – X
We have Z = X ∪ Y and Y is a set of attributes of R that are not the
attributes of S.
32
EXAMPLE OF DIVISION
Retrieve all employees who work on all the project that
John Smith works on
33
RECAP OF RELATIONAL ALGEBRA OPERATIONS
34
AGGREGATE FUNCTIONS
Now we specify mathematical aggregate functions on
collections of values from the database.
Examples:
⚫ Retrieve the average or total salary of all employees
⚫ Retrieve total number of employee tuples
35
AGGREGATE FUNCTION OPERATION
Use of the Aggregate Functional operation ℱ
⚫ ℱMAX Salary (EMPLOYEE)
⚫ ℱMIN Salary (EMPLOYEE)
⚫ ℱSUM Salary (EMPLOYEE)
⚫ ℱCOUNT SSN, AVERAGE Salary (EMPLOYEE)
computes no of employees and their average salary
Note: count just counts the number of rows, without removing
duplicates
36
USING GROUPING WITH AGGREGATION
Grouping can be combined with Aggregate Functions
Example:
⚫ For each department, retrieve the DNO, COUNT SSN, and
AVERAGE SALARY
⚫ DNO ℱCOUNT SSN, AVERAGE Salary (EMPLOYEE)
37
EXAMPLE: AGGREGATE FUNCTIONS AND GROUPING
38
EXAMPLES OF QUERIES IN RELATIONAL
ALGEBRA
■ Q1: Retrieve the name and address of all employees who work for the
‘Research’ department.
RESEARCH_DEPT ← σ DNAME=’Research’ (DEPARTMENT)
RESEARCH_EMPS ← (RESEARCH_DEPT DNUMBER= DNO
EMPLOYEE)
RESULT ← π FNAME, LNAME, ADDRESS (RESEARCH_EMPS)
39
EXAMPLES OF QUERIES IN RELATIONAL
ALGEBRA
■ Q6: 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 ← π LNAME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)
40
EXAMPLES OF QUERIES IN RELATIONAL
ALGEBRA
■ Q5: Retrieve the names of all employees with two or more dependents.
T1(Ssn, No_of_dependents) ← Essn ℱ COUNT Dependent_name (DEPENDENT)
T2 ← σ No_of_dependents >1(T1)
RESULT ← π LNAME, FNAME (T2 * EMPLOYEE)
41
ASSIGNMENT 2
Section B: Due date 13-Feb 2013
44
OUTER JOIN OPERATION
Left outer join: keeps every tuple in R, denoted as R
S
⚫ if no matching tuple is found in S, then the attributes of S in
the join result are filled with null values.
Full outer join: keeps all tuples in both the left and the
right relations. It is denoted by
45
FULL OUTER JOIN VS CARTESIAN PRODUCT
??
OUTER UNION OPERATIONS
47
OUTER JOIN EXAMPLE
An outer union can be applied to two relations STUDENT(Name, SSN,
Department, Advisor) and INSTRUCTOR(Name, SSN, Department,
Rank).
48
RECURSIVE CLOSURE OPERATION
This can’t be specified in general using Relational Algebra
Example: Retrieve all SUPERVISEES of an EMPLOYEE e at all levels —
that is,
⚫ all employees e` directly supervised by e;
⚫ all employees e`` directly supervised by each employee e`;
⚫ all employees e```directly supervised by each employee e``;
⚫ and so on.
We can retrieve employees at each level and then take their union, however,
we cannot specify a query such as
⚫ “retrieve the supervisees of ‘James Borg’ at all levels” without utilizing a looping
mechanism.
The SQL3 standard includes syntax for recursive closure.
49
RECURSIVE CLOSURE OPERATION
50
Example of Query Tree
Query: For every project located in ‘Stafford’, list the project number, the controlling
department number, and the department manager’s last name, address, and birth date.
51
QUERY TREE
An internal data structure to represent a query
Standard technique to estimate the work done in executing the
query, and the optimization of execution
A tree gives a good visual feel of the complexity of the query and
the operations involved
Algebraic Query Optimization consists of rewriting the query or
modifying the query tree into an equivalent tree.
52
RELATIONAL ALGEBRA OPERATORS
Relational Algebra consists of several groups of operations
⚫ Unary Relational Operations
SELECT (symbol: σ (sigma))
PROJECT (symbol: π (pi))
RENAME (symbol: ρ (rho))
⚫ Relational Algebra Operations From Set Theory
UNION ( ∪ ), INTERSECTION ( ∩ ), DIFFERENCE (–)
CARTESIAN PRODUCT ( x )
⚫ Binary Relational Operations
JOIN (several variations of JOIN exist)
DIVISION
⚫ Additional Relational Operations
OUTER JOINS, OUTER UNION
AGGREGATE FUNCTIONS (These compute summary of
information: for example, SUM, COUNT, AVG, MIN, MAX)
CHAPTER SUMMARY
Relational Algebra
⚫ Unary Relational Operations
⚫ Relational Algebra Operations From Set Theory
⚫ Binary Relational Operations
⚫ Additional Relational Operations
⚫ Examples of Queries in Relational Algebra
54