Professional Documents
Culture Documents
Chapter 6, The Relational Algebra and Relational Calculus
Chapter 6, The Relational Algebra and Relational Calculus
Relational Calculus
6.1 Unary Relational Operations: SELECT and
PROJECT
6.1.1 The SELECT Operation
– σ(DN O=4 and SALARY >25000) or (DN O=5 and SALARY >30000) (EM P LOY EE)
See Figure 6.1(a) (Fig 7.8(a) on e3) for the result.
• PROJECT some columns (attributes) from the table (relation) and discard the other
columns.
– R2 has only the attributes in < attribute list > with the same order as they
appear in the list.
1
– | R2 |≤| R1 |. If the < attribute list > is a superkey of R1 , then | R2 | = | R1 |
• It may apply several relational algebra operations one after another to get the final
result.
– we can apply one operation at a time and create intermediate result relations.
• For example, the algebra expression πF N AM E, LN AM E, SALARY (σDN O=5 (EM P LOY EE))
is equivalent to
(
DEP 5 EM P S ←− σDN O=5 (EM P LOY EE)
RESU LT ←− πF N AM E, LN AM E, SALARY (DEP 5 EM P S)
– ρS (R) or
– Where S is the renamed relation name of R and Bi ’s are the renamed attribute
names of R.
• The resulting relation has the same attribute names as the first relation R1
– | Q | = | R 1 | × | R2 |
– For example,
A1 A2 B1
a11 a21 b11
B1
A1 A2 a11 a21 b12
b11
B1 a11 a21 × B2 = Q a11 a21 b13
b12
a12 a22 a12 a22 b11
b13
a12 a22 b12
a12 a22 b13
– See Figure 6.5 (Fig 7.12 on e3). This figures shows a possible sequence of steps
to retrieve a list of names of each female employee’s dependents.
• JOIN is used to combine related tuples from two relations into single tuples.
< Ai1 θBj1 > AN D < Ai2 θBj2 > AN D . . . AN D < Aip θBjp >
Each attribute pair in the condition must have the same domain; θ is one of the
comparison operator.
• All JOIN operations with only “=” operator used in the conditions are called EQUI-
JOIN.
• Each tuple in the resulting relation of an EQUIJOIN has the same values for each pair
of attributes listed in the join condition.
• NATUAL JOIN (∗) was created to get rid of the superfluous attributes in an EQUI-
JOIN.
– NATUAL JOIN requires each pair of join attributes have the same name in both
relations, otherwise, a renaming operation should be applied first.
4
DEPT LOCS ←− DEPARTMENT ∗ DEPT LOCATIONS
PROJ DEPT ←− ρ(DN AM E, DN U M, M GRSSN, M GRST ART DAT E) (DEPARTMENT)
∗ PROJECT
• {σ, π, ∪, −, ×} is a complete set; that is, any of the other relational algebra operations
can be expressed as a sequence of operations from this set. For example,
– R ∩ S ≡ (R ∪ S) − ((R − S) ∪ (S − R))
• Example for DIVISION operation: “Retrieve the names of employees who work on all
the projects that ’John Smith’ works on.
JSMITH SSN(ESSN) ←− πSSN (σF N AM E=0 John0 AN D LN AM E=0 Smith0 (EMPLOYEE))
JSMITH PROJ ←− πP N O (JSMITH SSN ∗ WORKS ON)
WORKS ON2 ←− πESSN,P N O (WORKS ON)
DIV HERE(SSN) ←− WORKS ON2 ÷ JSMITH PROJ
RESULT ←− πF N AM E, LN AM E (EMPLOYEE ∗ DIV HERE)
See figure 6.8(a) (Figure7.15(a) on e3).
5
• Apply aggregate functions SUM, AVERAGE, MAXIMUM, MINIMUM and
COUNT of an attribute to different groups of tuples.
– The resulting relation R2 has the grouping attributes + one attribute for
each element in the function list.
– For example,
– This operation keeps every tuple t in left relation R1 in R3 , and fills “NULL” for
attributes B1 , B2 , . . . , Bm if the join condition is not satisfied for t.
– For example,
T EM P ←− (EM P LOY EE =./ SSN =M GRSSN DEP ART M EN T )
RESU LT ←− π F N AM E, M IN IT, DN AM E (T EM P )
The result is in Figure 6.12 (Figure 7.18 on e3)
• RIGHT OUTER JOIN: similar to LEFT OUTER JOIN, but keeps every tuple t
in right relation R2 in the resulting relation R3 .
– Notation: ./<
6
• FULL OUTER JOIN: =./<
• OUTER UNION: make union of two relations that are partially compatible.
– R3 (A1 , A2 , . . . , An , B1 , B2 , . . . , Bm , C1 , C2 , . . . , Cp ) ←− R1 (A1 , A2 , . . . , An , B1 , B2 ,
. . . , Bm ) OUTER UNION R2 (A1 , A2 , . . . , An , C1 , C2 , . . . , Cp )
– Tuples from R1 and R2 with the same values on the set of compitable attributes
are represented only once in R3
7
6.5 Examples of Queries in Relational Algebra
• Retrieve the name and address of all employees who work for the ’Research’ depart-
ment.
RESEARCH DEP T ←− σDN AM E=0 Research0 (DEP ART M EN T )
RESEARCH EM P S ←− (RESEARCH DEP T ./DN U M BER=DN O (EM P LOY EE))
RESU LT ←− πF N AM E,LN AM E,ADDRESS (RESEARCH EM P S)
• For every project located in ’Stafford’, list the project number, the controlling depart-
ment number, and the department manager’s last name, address, and birthdate.
ST AF F ORD P ROJS ←− σP LOCAT ION =0Staf f ord0 (P ROJECT )
CON T R DEP T ←− (ST AF F ORD P ROJS ./DN U M =DN U M BER (DEP ART M EN T ))
P ROJ DEP T M GR ←− (CON T R DEP T ./M GRSSN =SSN (EM P LOY EE))
RESU LT ←− πP N U M BET,DN U M,LN AM E,ADDRESS,BDAT E (P ROJ DEP T M GR)
• Find the names of employees who work on all the projects controlled by department
number 5.
DEP T 5 P ROJS(P N O) ←− πP N U M BER (σDN U M =5 (P ROJECT ))
EM P P ROJ(SSN, P N O) ←− πESSN,P N O (W ORKS ON )
RESU LT EM P SSN S ←− EM P P ROJ ÷ DEP T P ROJS
RESU LT ←− πLN AM E,F N AM E (RESU LT EM P SSN S ∗ EM P LOY EE)
• Make a list of project numbers for projects that involve an employee whose last name
is ’Smith’, either as a worker or as a manager of the department that controls the
project.
SM IT HS(ESSN ) ←− πSSN (σLN AM E=0 Smith0 (EM P LOY EE))
SM IT H W ORKER P ROJ ←− πP N O (W ORKS ON ∗ SM IT HS)
M GRS ←− πLN AM E,DN U M BER (EM P LOY EE ./SSN =M GRSSN DEP ART M EN T )
SM IT H M AN AGED DEP T S(DN U M ) ←− πDN U M BER (σLN AM E=0 Smith0 (M GRS))
SM IT H M GR P ROJS(P N O) ←− πP N U M BER (SM IT H M AN AGED DEP T S ∗
P ROJECT )
RESU LT ←− (SM IT H W ORKER P ROJS ∪ SM IT H M GR P ROJS)
8
• List the names of all employees who have two or more dependents.
T1 (SSN, N O OF DEP T S) ←−ESSN =COU N T DEP EN DEN T N AM E (DEP EN DEN T )
• Retrieve the name of each employee who has a dependent with the same first name
and same sex as the employee.
EM P S DEP S ←−
(EM P LOY EE ./SSN =ESSN AN D SEX=SEX AN D F N AM E=DEP EN DEN T N AM E DEP EN DEN T )
RESU LT ←− πLN AM E,F N AM E (EM P S DEP S)
• Find the sum of salary of all employees, the maximum salary, the minimum salary, and
the average salary for each department.
RESU LT ←−
DN O =SU M SALARY, M AXIM U M SALARY, M IN IM U M SALARY, AV ERAGE SALARY (EM P LOY EE)
9
6.6 The Tuple Relational Calculus
• Most relational query language is relationally complete but have more expressive power
than relational calculus (algebra) because of additional operations such as aggregate
functions, grouping, and ordering.
• A tuple variable usually range over a particular database relation: the variable may
take as its value any individual tuple from that relation.
• Examples:
– Find the first and last names of all employees whose salary is above $50,000.
{t.F N AM E, t.LN AM E | EM P LOY EE(t) and t.SALARY > 50000}
Compare to:
10
– For each tuple variable t, the range relation R of t is specified as R(t). (FROM
clause in SQL)
• Example: Retrieve the birthdate and address of the employee whose name is ’John B.
Smith’.
{t.BDAT E, t.ADDRESS | EM P LOY EE(t) and t.F N AM E =0 John0 and t.M IN IT =0
B 0 and t.LN AM E =0 Smith0 }
• A formula is made up of one or more atoms, which can be one of the following.
– An atom of the form R(ti ) defines the range of the tuple variable ti as the relation
R.
If the tuple variable ti is assigned a tuple that is a member of R, then the atom
is TRUE.
11
• A formula is made up one or more atoms connected via the logical operators and,
or, not and is defined recursively as follows.
– If F1 and F2 are formulas, then so are (F1 and F2 ), (F1 or F2 ), not(F1 ), not(F2 ).
And
12
– If F is a formula, then so is (∀ t)(F ), where t is a tuple variable. The formula
(∀ t)(F ) is TRUE if the formula F evaluates to TRUE for every tuple (in the
universe) assigned to free occurrences of t in F; otherwise (∀ t)(F ) is FALSE.
• Retrieve the name and address of all employees who work for the ’Research’ depart-
ment.
{t.F N AM E, t.LN AM E, t.ADDRESS | EM P LOY EE(t) and (∃ d)
(DEP ART M EN T (d) and d.DN AM E =0 Research0 and d.DN U M BER = t.DN O)}
• For every project located in ’Stafford’, list the project number, the controlling depart-
ment number, and the department manager’s last name, birthdate, and address.
{p.P N U M BER, p.DN U M, m.LN AM E, m.BDAT E, m.ADDRESS | P ROJECT (p)
and EM P LOY EE(m) and p.P LOCAT ION =0 Staf f ord0 and
((∃ d)(DEP ART M EN T (d) and p.DN U M = d.DN U M BER and d.M GRSSN =
m.SSN ))}
• For each employee, retrieve the employee’s first and last name and the first and last
name of his or her immediate supervisor.
{e.F N AM E, e.LN AM E, s.F N AM E, s.LN AM E | EM P LOY EE(e) and
EM P LOY EE(s) and e.SU P ERSSN = s.SSN }
• Find the name of each employee who works on some project controlled by department
number 5.
{e.LN AM E, e.F N AM E | EM P LOY EE(e) and ((∃ x)(∃ w)
(P ROJECT (x) and W ORKS ON (w) and x.DN U M = 5 and w.ESSN = e.SSN and
x.P N U M BER = w.P N O))}
• Make a list of project numbers for projects that involve an employee whose last name
is ’Smith’, either as a worker or as a manager of the controlling department for the
project.
{p.P N U M BER | P ROJECT (p) and
13
(((∃ e)(∃ w)(EM P LOY EE(e) and W ORKS ON (w) and
w.P N O = p.P N U M BER and e.LN AM E =0 Smith0 and e.SSN = w.ESSN ))
or
((∃ m)(∃ d)(EM P LOY EE(m) and DEP ART M EN T (d) and
p.DN U M = d.DN U M BER and d.M GRSSN = m.SSN and m.LN AM E =0 Smith0 )))}
• Find the names of employees who work on all the projects controlled by department
number 5.
{e.LN AM E, e.F N AM E | EM P LOY EE(e) and ((∀ x)(not(P ROJECT (x) or
not(x.DN U M = 5) or ((∃ w)(W ORKS ON (w) and w.ESSN = e.SSN and
x.P N U M BER = w.P N O))))}
BREAK INTO:
{e..LN AM E, e.F N AM E | EM P LOY EE(e) and F 0 }
F 0 = ((∀ x)(not(P ROJECT (x)) or F1 ))
14
F1 = not(x.DN U M = 5) or F2
F2 = ((∃ w)(W ORKS ON (w) and w.ESSN = e.SSN and x.P N U M BER = w.P N O))
IS EQUIVALENT TO:
{e.LN AM E, e.F N AM E | EM P LOY EE(e) and (not(∃ x)(P ROJECT (x)
and (x.DN U M = 5) and
(not(∃ w)(W ORKS ON (w) and w.ESSN = e.SSN and
x.P N U M BER = w.P N O))))}
IS EQUIVALENT TO:
{e.F N AM E, e.LN AM E | EM P LOY EE(e) and ((∀ d)
(not(DEP EN DEN T (d)) or not(e.SSN = d.ESSN )))}
• Domain of a tuple relational calculus expression: The set of all values that
either appear as constant values in the expression or exist in any tuple of the relations
referenced in the expression.
• An expression is safe if all values in its result are from the domain of the expression.
15
6.7 The Domain Relational Calculus
• Rather than having variables range over tuples in relations, the domain variables
range over single values from domains of attributes,
– An atom of the form R(x1 , x2 , . . . , xj ) (or simply R(x1 x2 . . . xj )), where R is the
name of a relation of degree j and each xi , 1 ≤ i ≤ j, is a domain variable.
This atom defines that < x1 , x2 , . . . , xj > must be a tuple in R, where the value
of xi is the value of the ith attribute of the tuple.
If the domain variables x1 , x2 , . . . , xj are assigned values corresponding to a tuple
of R, then the atom is TRUE.
– Retrieve the birthdate and address of the employee whose name is ’John B Smith’.
{uv | (∃ q)(∃ r)(∃ s)(∃ t)(∃ w)(∃ x)(∃ y)(∃ z)
(EM P LOY EE(qrstuvwxyz) and q =0 John0 and r =0 B 0 and s =0 Smith0 )}
16
in a condition (these would be q, r and s in the above example) in the
rest of examples
– Retrieve the name and address of all employees who work for the ’Research’
department.
{qsv | (∃ z)(∃ l)(∃ m)(EM P LOY EE(qrstuvwxyz) and
DEP ART M EN T (lmno) and l =0 Research0 and m = z)}
– For every project located in ’Stafford’, list the project number, the controlling
department number, and the department manager’s last name, birthdate, and
address.
{iksuv | (∃ j)(∃ m)(∃ n)(∃ t)(P ROJECT (hijk) and EM P LOY EE(qrstuvwxyz)
and DEP ART M EN T (lmno) and k = m and n = t and j =0 Staf f ord0 )}
IS EQUIVALENT TO:
{qs | (∃ t)(EM P LOY EE(qrstuvwxyz) and ((∀ l)(not(DEP EN DEN T (lmnop))
or not(t = l))))}
17