Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Chapter 6, The Relational Algebra and

Relational Calculus
6.1 Unary Relational Operations: SELECT and
PROJECT
6.1.1 The SELECT Operation

• SELECT a subset of tuples from R that satisfy a selection condition.

– σ<selection condition> (R1 )

– σ(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.

– The resulting relation R2 after applying selection on R1 , we have

degree(R1 ) = degree(R2 ) and

| R2 |≤| R1 | f or any selection condition

– Commutative: σC1 (σC2 (R)) = σC2 (σC1 (R))

– σC1 (σC2 (. . . (σCn (R)) . . .)) = σC1 and C2 and......and Cn (R)

6.1.2 The PROJECT Operation

• PROJECT some columns (attributes) from the table (relation) and discard the other
columns.

– R2 ← π<attribute list> (R1 )

– R2 ← πLN AM E, SALARY (EM P LOY EE)

– R2 has only the attributes in < attribute list > with the same order as they
appear in the list.

– degree(R2 ) = |< attribute list >|

– PROJECT operation will remove any duplicate tuples (duplication elimina-


tion). This happens when attribute list contains only non-key attributes of R1 .

1
– | R2 |≤| R1 |. If the < attribute list > is a superkey of R1 , then | R2 | = | R1 |

– π<list1> (π<list2> (R)) = π<list1> (R), where


< list2 > must be a subset of < list1 >.

6.1.3 Sequences of Operations and the RENAME Operation

• It may apply several relational algebra operations one after another to get the final
result.

– 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.

• 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)

• We could rename the above intermediate (or final) relations by


(
T EM P ←− σDN O=5 (EM P LOY EE)
R(F N, LN, SALARY ) ←− πF N AM E, LN AM E, SALARY (T EM P )

or we can define a RENAME operation and rewrite the query as follows.

– ρS(B1 ,B2 ,...,Bn ) (R) or

– ρS (R) or

– ρ(B1 ,B2 ,...,Bn ) (R)

– Where S is the renamed relation name of R and Bi ’s are the renamed attribute
names of R.

– The query becomes


(
ρT EM P (σDN O=5 (EM P LOY EE))
ρR(F N, LN, SALARY ) (πF N AM E, LN AM E, SALARY (T EM P ))

6.2 Relational Algebra Operations from Set Theory


2
6.2.1 The UNION, INTERSECTION, and MINUS
Operations
• R1 ∪ R2 (UNION), R1 ∩ R2 (INTERSECTION), or R1 − R2 (SET DIFFERENCE) are
valid operations iff R1 and R2 are union compatible.

• Two relations R1 (A1 , A2 , . . . , An ) and R2 (B1 , B2 , . . . , Bn ) are union compatible if


degree(R1 ) = degree(R2 ) and dom(Ai ) = dom(Bi ) for 1 ≤ i ≤ n.

• The resulting relation has the same attribute names as the first relation R1

• Commutative: UNION, INTERSECTION

• Associative: UNION, INTERSECTION

• See Figure 6.4 (or Fig 7.11 on 3e)

6.2.2 The CARTESIAN PRODUCT (or CROSS


PRODUCT) Operation
• R1 × R2 (R1 and R2 do not need to be union compatible)

• R1 (A1 , A2 , . . . , An ) × R2 (B1 , B2 , . . . , Bm ) = Q(A1 , A2 , . . . , An , B1 , B2 , . . . , Bm ), where

– | 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.

6.3 Binary Relational Operations: JOIN and


DIVISION
3
6.3.1 The JOIN Operation

• JOIN is used to combine related tuples from two relations into single tuples.

• R1 (A1 , A2 , . . . , An ) ./<join condition> R2 (B1 , B2 , . . . , Bm ) = Q(A1 , A2 , . . . , An , B1 , B2 , . . . , Bm );


where each tuple in Q is the combination of tuples – one from R1 and one from R2 –
whenever the combination satisfies the join condition.

• R1 ./<condition> R2 ≡ σ<condition> (R1 × R2 )

– For example, see Figure 6.6 (Figure 7.13 on e3),

ACTUAL DEPENDENT ←− EMPLOYEE ./SSN =ESSN DEPENDENT


≡ACTUAL DEPENDENT ←− σSSN =ESSN (EMPLOYEE × DEPENDENT)

• Usually the join condition is of the form:

< 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.

6.3.2 The EQUIJOIN and NATURAL JOIN Variations

• 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.

– For example, see 6.7 (Figure 7.14 on e3),

4
DEPT LOCS ←− DEPARTMENT ∗ DEPT LOCATIONS
PROJ DEPT ←− ρ(DN AM E, DN U M, M GRSSN, M GRST ART DAT E) (DEPARTMENT)
∗ PROJECT

• 0 ≤ | R1 (A1 , A2 , . . . , An ) ./<CON D> R2 (B1 , B2 , . . . , Bm ) | ≤ n × m

6.3.3 A Complete Set of Relational Algebra Operations

• {σ, π, ∪, −, ×} 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))

– R ./<CON D> S ≡ σ<CON D> (R × S)

– A NATUAL JOIN can be specified as a CARTESIAN PRODUCT preceded by


RENAME and followed by SELECT and PROJECT operations.

6.3.4 The DIVISION Operation

• T (Y ) ←− R(Z)÷S(X), where X, Y, Z are sets of attributes and X ⊆ Z and Y = Z −X

• A tuple t ∈ T if tuples t1 ∈ R with t1 [Y ] = t and with t1 [X] = t2 for every tuple t2 in


S. See Figure 6.8(b) (Figure 7.15(b) on e3).

• 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).

6.4 Additional Relational Operations


6.4.1 Aggregate Functions and Grouping

5
• Apply aggregate functions SUM, AVERAGE, MAXIMUM, MINIMUM and
COUNT of an attribute to different groups of tuples.

• R2 ←− <grouping attribs> = <<f unc attrib>,<f unc attrib>,......> (R1 )

– The resulting relation R2 has the grouping attributes + one attribute for
each element in the function list.

– Each group results in a tuple in R2 .

– For example,

∗ DN O = COU N T SSN, AV ERAGE SALARY (EM P LOY EE)


DNO COUNT SSN AVERAGE SALARY
5 4 33250
4 3 31000
1 1 55000

∗ = COU N T SSN, AV ERAGE SALARY (EM P LOY EE)


COUNT SSN AVERAGE SALARY
8 35125

6.4.3 OUTER JOIN

• LEFT OUTER JOIN: R3 (A1 , A2 , . . . , An , B1 , B2 , . . . , Bm ) ←− R1 (A1 , A2 , . . . , An )


=./ <JOIN CON D.> R2 (B1 , B2 , . . . , Bm )

– 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: =./<

6.4.4 The OUTER UNION Operation

• 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 )

– The list of compatible attributes are represented only once in R3 .

– Tuples from R1 and R2 with the same values on the set of compitable attributes
are represented only once in R3

– In R3 , fill “NULL” if necessary

– For example, STUDENT(NAME, SSN, DEPT, ADVISOR) and FACULTY(NAME,


SSN, DEPT, RANK)
The resulting relation schema after OUTER UNION will be R 3(NAME, SSN,
DEPT, ADVISOR, RANK)

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 )

T2 ←− σN O OF DEP T S≥2 (T1 )

RESU LT ←− πLN AM E,F N AM E (T2 ∗ EM P LOY EE)

• Retrieve the names of employees who have no dependents.


ALL EM P S ←− πSSN (EM P LOY EE)
EM P S W IT H DEP S(SSN ) ←− πESSN (DEP EN DEN T )
EM P S W IT HOU T DEP S ←− (ALL EM P S − EM P S W IT H DEP S)
RESU LT ←− πLN AM E,F N AM E (EM P S W IT HOU T DEP S ∗ EM P LOY EE)

• List the names of managers who have at least one dependent.


M GRS(SSN ) ←− πM GRSSN (DEP ART M EN T )
EM P S W IT H DEP S(SSN ) ←− πESSN (DEP EN DEN T )
M GRS W IT H DEP S ←− (M GRS ∩ EM P S W IT H DEP S)
RESU LT ←− πLN AM E,F N AM E (M GRS W IT H DEP S ∗ EM P LOY EE)

• 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)

• Retrieve the names of all employees who do not have supervisors.


RESU LT ←− πLN AM E,F N AM E (σSU P ERSSN =N U LL (EM P LOY EE))

• 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

• Nonprocedural Language: Specify what to do; Tuple (Relational) Calculus, Domain


(Relational) Calculus.

• Procedural Language: Specify how to do; Relational Algebra.

• The expressive power of Relational Calculus and Relational Algebra is identical.

• A relational query language L is considered relationally complete if we can express


in L any query that can be expressed in 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.

6.6.1 Tuple Variables and Range Relations

• A tuple variable usually range over a particular database relation: the variable may
take as its value any individual tuple from that relation.

• General Form: {t | CON D(t)}

• Examples:

– Find all employees whose salary is above $50,000.


{t | EM P LOY EE(t) and t.SALARY > 50000}

– 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:

SELECT T.FNAME, T.LNAME


FROM EMPLOYEE AS T
WHERE T.SALARY > 50000;

• Three information should be specified in a tuple calculus expression.

10
– For each tuple variable t, the range relation R of t is specified as R(t). (FROM
clause in SQL)

– A condition to select particular combinations of tuples. (WHERE clause in SQL)

– A set of attributes to be retrieved, the requested attributes. (SELECT 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 }

6.6.2 Expressions and Formulas in Tuple Relation Calculus

• A general expression form:


{t1 .A1 , t2 .A2 , . . . , tn .An | CON D(t1 , t2 , . . . , tn , tn+1 , tn+2 , . . . , tn+m )}
Where t1 , t2 , . . . , tn , tn+1 , tn+2 , . . . , tn+m are tuple variables, each Ai is an attribute of
the relation on which ti ranges, and COND is a condition or formula

• 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.

– An atom of the form ti .A op tj .B, where op is one of the comparison operators


{=, >, ≥, <, ≤, 6=}.
If the tuple variables ti and tj are assigned to tuples such that the values of the
attributes ti .A and tj .B satisfy the condition, then the atom is TRUE.

– An atom of the form ti .A op c or c op tj .B.


If the tuple variables ti (or tj ) is assigned to a tuple such that the value of the
attribute ti .A (or tj .B) satisfies the condition, 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.

– Every atom is a formula.

– If F1 and F2 are formulas, then so are (F1 and F2 ), (F1 or F2 ), not(F1 ), not(F2 ).
And

∗ (F1 and F2 ) is TRUE if both F1 and F2 are TRUE; otherwise, it is FALSE.


∗ (F1 or F2 ) is FALSE if both F1 and F2 are FALSE; otherwise, it is TRUE.
∗ not(F1 ) is TRUE if F1 is FALSE; it is FALSE if F1 is TRUE.
∗ not(F2 ) is TRUE if F2 is FALSE; it is FALSE if F2 is TRUE.

6.6.3 The Existential and Universal Quantifiers


• There are two quantifiers can appear in formula, universal quantifier ∀ and exis-
tential quantifier ∃.

• free and bound for tuple variables in formula.

– An occurrence of a tuple variable in a formula F that is an atom is free in F .

– An occurrence of a tuple variable t is free or bound in (F1 and F2 ), (F1 or F2 ),


not(F1 ), not(F2 ), depending on whether it is free or bound in F1 or F2 . Notice
that a tuple variable may be free in F1 and bound in F2 .

– All free occurrences of a tuple variable t in F are bound in formulas F 0 = (∃ t)(F )


or F 0 = (∀ t)(F ). For example:
F1 : d.DN AM E =0 Research0
F2 : (∃ t)(d.DN U M BER = t.DN O)
F3 : (∀ d)(d.M GRSSN =0 3334455550)
Where variable d is free in F1 and F2 , but bound in F3 . Variable t is bound in F2 .

• A formula with quantifiers is defined as follows.

– 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 some tuple assigned to
free occurrences of t in F; otherwise (∃ t)(F ) is FALSE.

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.

6.6.4 Example Queries Using the Existential Quantifier

• 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 )))}

6.6.5 Transforming the Universal and Existential Quantifiers

• (∀ x)(P (x)) ≡ not(∃ x)(not(P (x)))

• (∃ x)(P (x)) ≡ not(∀ x)(not(P (x)))

• (∀ x)(P (x) and Q(x)) ≡ not(∃ x)(not(P (x)) or not(Q(x)))

• (∀ x)(P (x) or Q(x)) ≡ not(∃ x)(not(P (x)) and not(Q(x)))

• (∃ x)(P (x) or Q(x)) ≡ not(∀ x)(not(P (x)) and not(Q(x)))

• (∃ x)(P (x) and Q(x)) ≡ not(∀ x)(not(P (x)) or not(Q(x)))

• (∀ x)(P (x)) ⇒ (∃ x)(P (x))

• not(∃ x)(P (x)) ⇒ not(∀ x)(P (x))

6.6.6 Using the Universal Quantifier

• 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))))}

• Find the names of employees who have no dependents.


{e.F N AM E, e.LN AM E | EM P LOY EE(e) and (not(∃ d)(DEP EN DEN T (d)
and e.SSN = d.ESSN ))

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 )))}

• List the names of managers who have at least one dependent.


{e.F N AM E, e.LN AM E | EM P LOY EE(e) and ((∃ d)(∃ p)
(DEP ART M EN T (d) and DEP EN DEN T (p) and e.SSN = d.M GRSSN and
p.ESSN = e.SSN ))}

6.6.7 Safe Expressions

• Safe Expression: The result is a finite number of tuples.

• For example, {t | not(EM P LOY EE(t))} is unsafe.

• 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,

• General form: {x1 , x2 , . . . , xn | CON D(x1 , x2 , . . . , xn , xn+1 , xn+2 , . . . , xn+m )}


Domain Variables: x1 , x2 , . . . , xn that range over the domains of attributes.
Formula: CON D is the formula or condition of the domain relational calculus.
A formula is made up of atoms.

– 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.

– An atom of the form xi op xj , where op is one of the comparison operators


{=, >, ≤, <, ≥, 6=}.
If the domain variables xi and xj are assigned values that satisfy the condition,
then the atom is TRUE.

– An atom of the form xi op c or c op xj , where c is a constant value.


If the domain variables xi (or xj ) is assigned a value that satisfies the condition,
then the atom is TRUE.

• Examples: we use lowercase letters l, m, n, . . . , x, y, z for domain variables.

– 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 )}

An alternative notation for this query.


{uv | EM P LOY EE(0 John0 ,0 B 0 ,0 Smith0 , t, u, v, w, x, y, z)}

For convenience, we quantify only those variables actually appearing

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 )}

– Find the names of employees who have no dependents.


{qs | (∃ t)(EM P LOY EE(qrstuvwxyz) and (not(∃ l)(DEP EN DEN T (lmnop)
and t = l)))}

IS EQUIVALENT TO:
{qs | (∃ t)(EM P LOY EE(qrstuvwxyz) and ((∀ l)(not(DEP EN DEN T (lmnop))
or not(t = l))))}

– List the names of managers who have at least one dependent.


{sq | (∃ t)(∃ j)(∃ l)(EM P LOY EE(qrstuvwxyz) and DEP ART M EN T (hijk)
and DEP EN DEN T (lmnop) and t = j and l = t)}

17

You might also like