09 Relational Algebra
09 Relational Algebra
Algebra
Outline (1)
Introduction
Query Tree
It filters the tuples and keeps only tuples that satisfy the
selection condition.
{=,<,≤,>,≥,≠}
The <constant value> is a constant value from the attribute
domain.
σDno=4(EMPLOYEE) ……..(ex.01)
In ex.01, we want to select (σ) from a EMPLOYEE relation
with a selection condition of “Dno=4”.
σ<cond1>(σ<cond2>(R)) = σ<cond2>(σ<cond1>(R))
Unary Relational Operation
[SELECT Operation] (8)
π<attribute list>(R)
The pi (π) symbol is to denote the PROJECT Operation.
Π (EMPLOYEE)…(ex.03)
Lname, Fname, Salary
ρS(R) …(ro.2)
OR
R S S R and R S S R
The nature of MINUS Operation is not commutative:
RS SR
INTERSECTION can
be expressed in the form of UNION
and MINUS:
R
S R S R S S R
Relational Algebra Operations from Set
Theory
[The CARTESIAN PRODUCT] (1)
Denoted by ×
R(A1,A2,…,An) x S(B1,B2,…,Bm)
Q(A1,A1,…,An,B1,B2,…,Bm)
Relational Algebra Operations from Set
Theory
[The CARTESIAN PRODUCT] (2)
><
DEPT_MGR DEPARTEMENTMgr_ssn=SsnEMPLOYEE
RESULT πDname, Lname, Fname(DEPT_MGR)
Binary Relational Operations
[JOIN] (2)
The general form of JOIN operation on two relations
R(A1,A2,…,An) and S(B1,B2,…,Bm) is
><
R<join condition>S
The result is a relation Q with n+m attributes Q(A1,A2,…An,
B1,B2,…,Bm) in that order. Q has one tuple for each combination
of tuples (one from R and one from S) – whenever the
satisfies the join condition.
combination
<condition>AND<condition>AND…AND<condition>
This general join condition is called THETA JOIN. Tuples with join
attributes equal NULL do not appear in the result (does not
necessarily preserve all of the information in the participating
relations).
Binary Relational Operations
[VARIATION of JOIN: The EQUIJOIN] (1)
A JOIN operation that only use an equal / {=} comparison
operator.
Denoted by *.
PROJ_DEPT PROJECT *
ρ(Dname,Dnum,Mgr_ssn,Mgr_start_date)(DEPARTEMENT)
DEPT ρ(Dname,Dnum,Mgr_ssn,Mgr_start_date)(DEPARTEMENT)
There can be a list of join attributes from each relation, and each
corresponding pair must have the same name.
Q R * (<list1>),(<list2>)S
If no join condition and all combinations of tuples qualify then
the JOIN operation degenerates into a CARTESIAN
PRODUCT.
PROJECT >< DEPARTEMENT ><
Dnum Dnumber EMPLOYEE
Mgr _ ssn Ssn
Binary Relational Operations
[A Complete Set of Relational Algebra
Operation] (1)
These are the complete set of relational algebra operation:
{, ,,,}
Denoted by:
Example: retrieve the names of employees who work on all the
projects that „John Smith‟ works on.
First step get list of project numbers that „John Smith‟
works on:
Fname ' John' ANDLname ' Smith' EMPLOYEE
SMITH
SMITH
_ PNOS PnoWORKS _ON >< SMITH
Essn Ssn
Binary Relational Operations
[The DIVISION Operation] (2)
Second step create a relation that includes a tuple <Pno,
Essn> where Ssn = Essn and the project number is Pno:
Binary Relational Operations
[The DIVISION Operation] (3)
Generally, DIVISION is applied to two relations R(Z) ÷ S(X),
where X Z
T1 Y (R)
T2 Y S T1 R
T T1 T2
Notation for Query Trees (1)
Query Tree, or query evaluation tree, or query execution tree is
a notation that is used typically in relational systems to
represent queries internally.
(2
><
P.Dnum
) D.Dnumber EMPLOYE
E E
(1
P.Plocation
) ' Stafford' DEPARTEMENT
D
PROJEC
P T
Notation for Query Trees (3)
In general, a query tree gives a good visual representation and
understanding of the query in terms of the relational operations
it uses.
F , F ,...,F (R)
1 2 n
Additional Relational Operations
[Generalized Projection] (2)
Consider this example, we have a relation:
Additional Relational Operations
[Aggregate Functions and Grouping] (1)
Mathematical aggregate functions on collections of values
from the database cannot be specified by the original relational
algebra.
Additional Relational Operations
[Aggregate Functions and Grouping] (2)
The resulting relation has the grouping attributes plus one
attribute for each element in the function list.
Additional Relational Operations
[Recursive Closure Operations] (1)
Recursive Closure is another operation that can be specified
using the original relational algebra.
Additional Relational Operations
[Recursive Closure Operations] (2)
Example: retrieve all employees supervised by Borg at level 2
(all employee e‟‟ supervised by some employee e‟ who is directly
supervised by Borg). Apply another JOIN to the result of the first
query:
RESULT2(Ssn)
Ssn1(SUPERVISION >< Ssn2 SsnRESULT1)
Example: to get both sets of employees supervised at levels 1
and 2 by „James Borg‟ apply UNION operation:
RESULT RESULT2 RESULT1
Additional Relational Operations
[Recursive Closure Operations] (3)
Additional Relational Operations
[OUTER JOIN Operations] (1)
On previous JOIN operations:
Tuples without a matching (or related) tuples are eliminated.
Tuples with NULL values in the join attributes are also
eliminated.
Additional Relational Operations
[OUTER UNION Operations] (1)
Developed to take the union of tuples from two relations if the
relations are not union compatible.
Two tuples t1 in R and t2 S are said match if t1[X] = t2[X], and are
considered to represent the same entity of relationship instance.
These will be combined into a single tuple in the result T. No
matching tuple in the other relation are padded with NULL.
Additional Relational Operations
[OUTER UNION Operations] (2)
Example: an OUTER JOIN can be applied to two relations
whose schemas are:
STUDENT(Name, Ssn, Departement, Advisor)
INSTRUCTOR(Name, Ssn, Departement, Rank)