5 the relational calculus
TRANSCRIPT
The Relational Calculus
(Based on Chapter 9 in Fundamentals of Database Systems
by Elmasri and Navathe, Ed. 3)
The Relational Calculus 2
Contents Introduction to Relational Calculus Tuple Relational Calculus
Tuple Variables and Range Relations Formal Specification of Tuple Relational Calculus Queries Using the Existential Quantifier Transforming Universal and Existential Quantifiers Universal Quantifiers and Safe Expressions
Domain Relational Calculus
The Relational Calculus 3
Introduction to Relational Calculus A formal language based on first-
order predicate calculus. Many commercial relational
languages based on some aspects of relational calculus, including SQL.
QUEL, QBE(Chapter 9) closer to relational calculus than SQL
The Relational Calculus 4
Difference from relational algebra: One declarative calculus expression
specifies a retrieval query. A sequence of operations is used in
relational algebra. Relational algebra more procedural. Relational calculus more declarative
(less procedural). Expressive power of the two languages
is identical.
The Relational Calculus 5
Relational Completeness: A relational query language L is
relationally complete if we can express in L any query that can be expressed in the relational calculus (or algebra)
Most relational query languages are relationally complete.
More expressive power is provided by operations such as aggregate functions, grouping, and ordering.
The Relational Calculus 6
Tuple Variable and Range Relations The tuple relational calculus is
based on specifying a number of tuple variables.
A tuple variable ranges over the tuples of a particular relation.
Such relation is called a range relation.
The Relational Calculus 7
A Form of Tuple Relational Calculus Query A simple tuple relational calculus
query is of the from{t | COND(t)} t is a tuple variable COND(t) is a conditional expression
involving t. The result of such a query is set of
all tuples t that satisfies COND(t).
The Relational Calculus 8
Example: Find all employees whose salary is
above $50000: { t | EMPLOYEE(t) and t.SALARY > 50000
} EMPLOYEE(t) specifies the range relation
EMPLOYEE for the tuple variable t Each tuple t satisfying t.SALARY > 50000
is retrieved Retrieves the whole tuple t
The Relational Calculus 9
Example: To retrieve only some attributes of
t: { t.FNAME, t.LNAME | EMPLOYEE(t)
and t.SALARY>50000 } Similar to the SQL query:
SELECT T.FNAME, T.LNAMEFROM EMPLOYEE TWHERE T.SALARY > 50000
The Relational Calculus 10
A Tuple Calculus Expression Need to specific the following
information For each tuple variable t, the range
relation R of t. This value is specified by a condition of the form R(t).
A condition to select particular combinations of tuples.
A set of attributes to be retrieved, the requested attribute.
The Relational Calculus 11
A general expression of tuple relational calculus is of the form{(t1.A1, t2.A2,…tn.An | COND(t1,t2,…,tn,tn+1,tn+2,
…,tn+m)} Where t1.A1, t2.A2,…tn.An are tuple variable
s, each Ai is an attribute of the relation on w
hich ti ranges, and COND is a condition or formula
Expressions of Tuple Relational Calculus
The Relational Calculus 12
Formulas of Tuple Relational Calculus A formula is made up of predicate calculus
atoms, which can be one of the following: An atom of the form R(ti), where R is a relation
name and ti is a tuple variable. An atom of the form ti.A op tj.B, where op is on
e of the comparison operators in the set {=,>,,<,,}, ti and tj are tuple variables, A is an attribute of the relation on which ti ranges, and B is an attribute of the relation on which tj ranges.
The Relational Calculus 13
Formulas of Tuple Relational Calculus A formula is made up of predicate calculus
atoms, which can be one of the following: An atom of the form ti.A op c or c op tj.B, where op is one of the comparison operators in the set {=,>,,<,,}, ti and tj are tuple variables, A is an attribute of the relation on which ti ranges, and B is an attribute of the relation on which tj ranges, and c is a constant value.
The Relational Calculus 14
Truth Value Each of the preceding atoms
evaluates to ether TRUE or FALSE for a specific combination of tuples.
This is called the truth value of an atom.
In general, a tuple variable ranges over all possible tuples “in the universe.”
The Relational Calculus 15
Formula Atoms connected via and, or and not.
Every atom is a formula If F1 and F2 are formulas, so are :
(F1 and F2) (F1 or F2) not(F1) not(F2)
The Relational Calculus 16
The Existential and Universal Quantifiers Universal quantifier()
Read for all Existential quantifiers()
Read their exists
The Relational Calculus 17
Free and Bound Tuple Variables
Informally, A tuple variable t is bound if it is quantified, mean that it appears in an (t) or (t) clause; otherwise, it is free.
The Relational Calculus 18
Free and Bound Tuple Variables
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 a formula made up of logical connectives – (F1 and F2), (F1 or F2), not(F1), and not(F2) – depending on whether it is free or bound in F1 or F2 (if it occur in either).
The Relational Calculus 19
Free and Bound Tuple Variables
All free occurrences of a tuple variable t in F are bound in a formula F’ of the form F’ = (t)(F) or F’ = (t)(F) .
The tuple variable is bound to the quantifier specified in F’.
The Relational Calculus 20
For example F1 : d.DNAME=‘Research’ F2 : (t)(d.DNUMBER=t.DNO) F3 : (t)(d.MGRSSN=‘333445555’)
d is free in F1, F2 and F3 t is bound to the quantifier in F2 t is bound to the quantifier in F3
The Relational Calculus 21
Formulas Every atom is a formula. If F1 and F2 are formulas, then so
are (F1 and F2), (F1 or F2), not(F1), and not(F2).
If F is a formula, then so is (t)(F), where t is a tuple variable.
If F is a formula, then so is (t)(F), where t is a tuple variable.
The Relational Calculus 22
Truth Value for Existential Quantifiers The formula (t)(F) is TRUE if the form
ula F evaluates to TRUE for some (at least one) tuple assigned to free occurrences of t in F; otherwise (t)(F) is FALSE.
The Relational Calculus 23
Existential Quantifiers called existential quantifier
because (t)(F) is TRUE if “there exists” some tuple t that make F TURE.
The Relational Calculus 24
Truth Value for Universal Quantifiers 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.
The Relational Calculus 25
Universal Quantifiers called universal quantifier
because every tuple in “the universe of” tuples must make F TRUE if (t)(F) is to be TRUE.
The Relational Calculus 26
Example Queries Using the
Existential Quantifiers (1)
Query 1 : Retrieve the name and address of all
employees who work for the ‘Research’ department.
Query 1 : Retrieve the name and address of all
employees who work for the ‘Research’ department.
The Relational Calculus 27
Example Queries Using the
Existential Quantifiers (1)
Q1: { t.FNAME, t.LNAME, t.ADDRESS |
EMPLOYEE(t) and( d)(DEPARTMENT(d) and d.NAME=‘Research’ and d.DNUMBER=t.DNO) }
Q1: { t.FNAME, t.LNAME, t.ADDRESS |
EMPLOYEE(t) and( d)(DEPARTMENT(d) and d.NAME=‘Research’ and d.DNUMBER=t.DNO) }
The Relational Calculus 28
Example Queries Using the
Existential Quantifiers (1) The only free tuple variables in a
relational calculus expression should be those that appear to the left of the bar (|).
Each free variable is bound successively to each tuple that satisfies the condition to the right of the bar (|).
The bar (|) read as “such that”
The Relational Calculus 29
Example Queries Using the
Existential Quantifiers (1) EMPLOYEE(t), DEPARTMENT(d) specify
range relations for t. The condition d.NAME=“Research” is
selection condition. (corresponds to SELECT in relational algebra)
The condition d.DNUMBER=t.DNO is a join condition. (serves a similar purpose to EQUIJOIN in
relational algebra)
The Relational Calculus 30
Example Queries Using the
Existential Quantifiers (2)
Query 2 For every project located in ‘Stafford’,
list the project number, the controlling department number, and the department manage’s last name, address, and birthdate.
Query 2 For every project located in ‘Stafford’,
list the project number, the controlling department number, and the department manage’s last name, address, and birthdate.
The Relational Calculus 31
Example Queries Using the
Existential Quantifiers (2)
Q2: {p.PNUMBER, p. DNUM, m.LNAME,
m.BDATE, m.ADDRESS |PROJECT(p) and EMPLOYEE(m)
and PLOCATION='Stafford‘ and (d)
(DEPARTMENT(d) and p.DNUM=d.DNUMBER
and d.MGRSSN=m.SSN) }
Q2: {p.PNUMBER, p. DNUM, m.LNAME,
m.BDATE, m.ADDRESS |PROJECT(p) and EMPLOYEE(m)
and PLOCATION='Stafford‘ and (d)
(DEPARTMENT(d) and p.DNUM=d.DNUMBER
and d.MGRSSN=m.SSN) }
The Relational Calculus 32
Example Queries Using the
Existential Quantifiers (8)
Query 8: For each employee, retrieve the
employee's name, and the name of his or her immediate supervisor.
Query 8: For each employee, retrieve the
employee's name, and the name of his or her immediate supervisor.
The Relational Calculus 33
Example Queries Using the
Existential Quantifiers (8)
Q8: {e.FNAME, e.LNAME, s.FNAME,s.LNAME |
EMPLOYEE(e) and EMPLOYEE(s) and e.SUPERSSN=s.SSN}
Q8: {e.FNAME, e.LNAME, s.FNAME,s.LNAME |
EMPLOYEE(e) and EMPLOYEE(s) and e.SUPERSSN=s.SSN}
The Relational Calculus 34
Example Queries Using the
Existential Quantifiers (3’)
Query 3’ Find the names of employees who
work on some projects controlled by department number 5.
Query 3’ Find the names of employees who
work on some projects controlled by department number 5.
The Relational Calculus 35
Example Queries Using the
Existential Quantifiers (3’)
Q3’: {e.FNAME, e.LNAME |
EMPLOYEE(e) and((x)(w)(PROJECT(x)
and WORKS_ON(w) and x.DNUM=5 and e.SSN=w.ESSN
and p.PNO=x.PNUMBER))}
Q3’: {e.FNAME, e.LNAME |
EMPLOYEE(e) and((x)(w)(PROJECT(x)
and WORKS_ON(w) and x.DNUM=5 and e.SSN=w.ESSN
and p.PNO=x.PNUMBER))}
The Relational Calculus 36
Example Queries Using the
Existential Quantifiers (4)
Query 4 Make a list of project numbers for
projects that involve an employee whose last name is ‘Smith’, either as a worker or a manager of the department that controls the project.
Query 4 Make a list of project numbers for
projects that involve an employee whose last name is ‘Smith’, either as a worker or a manager of the department that controls the project.
The Relational Calculus 37
Example Queries Using the
Existential Quantifiers (4) Q4:
{p.PNUMBER | PROJECT(p) and (((e)( w)(EMPLOYEE(e) and WORKS_ON(w)
and p.PNUMBER=w.PNO and e.LNAME='Smith‘
and w.ESSN=e.SSN ))or
(( m)( d)(EMPLOYEE(m) and DEPARTMENT(d) and p.DNUM=d.DNUMBER and d.MGRSSN=m.SSN and m.LNAME='Smith')))}
Q4: {p.PNUMBER | PRO
JECT(p) and (((e)( w)(EMPLOYEE(e) and WORKS_ON(w)
and p.PNUMBER=w.PNO and e.LNAME='Smith‘
and w.ESSN=e.SSN ))or
(( m)( d)(EMPLOYEE(m) and DEPARTMENT(d) and p.DNUM=d.DNUMBER and d.MGRSSN=m.SSN and m.LNAME='Smith')))}
The Relational Calculus 38
Example Queries Using the
Existential Quantifiers (4) In general, UNION in relational
algebra corresponds to an or connective in relational calculus.
INTERSECTION corresponds to an and connective.
The Relational Calculus 39
Transforming Universal and Existential Quantifiers The not connective can be used to
transform universal and existential quantifiers to equivalent formulas.
The Relational Calculus 40
Well-known transformations from mathematical logic.
( x) (P(x)) (not x) (not(P(x))) ( x) (P(x)) not ( x) (not(P(x)))
( x) (P(x)) (not x) (not(P(x))) ( x) (P(x)) not ( x) (not(P(x)))
The Relational Calculus 41
Well-known transformations from mathematical logic. The following is also true, where
stands for implies: ( x) (P(x)) ( x) (P(x)) (not x) (P(x)) not ( x) (P(x))
The following is not true: not( x) (P(x)) (not x) (P(x))
The Relational Calculus 42
Example Queries Using Universal Quantifiers (3)
Query 3 Find the names of employees who
work on all the projects controlled by department number 5.
Query 3 Find the names of employees who
work on all the projects controlled by department number 5.
The Relational Calculus 43
Example Queries Using Universal Quantifiers (3) Q3:
{e.FNAME, e.LNAME | EMPLOYEE(e) and((x)(not(PROJECT(x)) or
(not(x.DNUM=5) or ((w)(WORKS_ON(w)
and e.SSN=w.ESSNand p.PNO=x.PNUMBER)
)) )
}
Q3: {e.FNAME, e.LNAME | EMPLOYEE(e) and
((x)(not(PROJECT(x)) or (not(x.DNUM=5) or ((w)(WORKS_ON(w)
and e.SSN=w.ESSNand p.PNO=x.PNUMBER)
)) )
}
The Relational Calculus 44
Example Queries Using Universal Quantifiers (3)
Q3 : For every tuple x in the project relation
with x.DUM = 5, there must exist a tuple w in WORK_ON such that w.ESSN=e.SSN and w.PNO=x.PNUMBER.
Q3 : For every tuple x in the project relation
with x.DUM = 5, there must exist a tuple w in WORK_ON such that w.ESSN=e.SSN and w.PNO=x.PNUMBER.
The Relational Calculus 45
Basic Components of Q3
Q3: {e.FNAME, e.LNAME | EMPLOYEE(e) and
F’} F’= ((x)(not(PROJECT(x)) or F1)) F1 = not(x.DNUM=5) or F2
F2 = ((w)(WORKS_ON(w) and e.SSN=w.ESSNand p.PNO=x.PNUMBER))
Q3: {e.FNAME, e.LNAME | EMPLOYEE(e) and
F’} F’= ((x)(not(PROJECT(x)) or F1)) F1 = not(x.DNUM=5) or F2
F2 = ((w)(WORKS_ON(w) and e.SSN=w.ESSNand p.PNO=x.PNUMBER))
The Relational Calculus 46
Basic Components of Q3 Must exclude all tuples not of
interest from the universal quantification by making the condition TRUE for all such tuples.
Universally quantified variable x must evaluate to TRUE for every possible tuple in the universe.
The Relational Calculus 47
Basic Components of Q3 In F’, not(PROJECT(x)) makes x TRUE for al
l tuples not in the relation of interest “PROJECT”. F’= ((x)(not(PROJECT(x)) or F1))
In F1, not(x.DNUM=5) makes x TRUE for those PROJECT tuples we are not interested in “whose DNUM is not 5” F1 = not(x.DNUM=5) or F2
The Relational Calculus 48
Basic Components of Q3 F2 specifies the condition that must
hold on all remaining tuples “ all PROJECT tuples controlled by department 5” F2 = ((w)(WORKS_ON(w)
and e.SSN=w.ESSNand p.PNO=x.PNUMBER))
The Relational Calculus 49
Safe Expressions A safe expression in relational
calculus is one that is guaranteed to yield a finite number of tuples as its result; otherwise, the expression is called unsafe.
Unsafe expression may yield infinite number of tuples, and the tuples may be different types.
The Relational Calculus 50
Safe Expressions For example:
{t | not(EMPLOYEE(t))} is unsafe.
Yields all non-EMPLOYEE tuples in the universe.
The Relational Calculus 51
Universal Quantifiers and Safe Expressions One must be careful when
specifying universal quantification. Judicious to follow rules to ensure
expression make sense. Otherwise, unsafe expressions
may result.
The Relational Calculus 52
Safe Expressions We discuss rules for safe expressions
using the universal quantifier by looking at query Q3.
Following the rules for Q3 discussed above guarantees safe expressions when using universal quantifiers.
Using transformations from universal to existential quantifiers, can rephrase Q3 as Q3A
The Relational Calculus 53
Example of Transforming Universal and Existential Quantifiers
Q3A: {e.FNAME, e.LNAME |
EMPLOYEE(e) and (not ( x)(PROJECT(x) and (x.DNUM=5)
and (not ( w)(WORKS_ON(w) and e.SSN=w.ESSN
and p.PNO=x.PNUMBER)))) }
Q3A: {e.FNAME, e.LNAME |
EMPLOYEE(e) and (not ( x)(PROJECT(x) and (x.DNUM=5)
and (not ( w)(WORKS_ON(w) and e.SSN=w.ESSN
and p.PNO=x.PNUMBER)))) }
The Relational Calculus 54
Additional Examples (6)
Query 6: Retrieve the names of employees who
have no dependents.
Query 6: Retrieve the names of employees who
have no dependents.
The Relational Calculus 55
Example Queries Using the
Existential Quantifiers (6) Q6:
{e.FNAME, e.LNAME |EMPLOYEE(e) and(not(d)(DEPENDENT(d)
and e.SSN=d.ESSN))}
The Relational Calculus 56
Example Queries Using the
Universal Quantifiers (6)
Q6: {e.FNAME, e.LNAME |
EMPLOYEE(e) and((d)(not (DEPENDENT(d))
or not (e.SSN=d.ESSN))) }
Q6: {e.FNAME, e.LNAME |
EMPLOYEE(e) and((d)(not (DEPENDENT(d))
or not (e.SSN=d.ESSN))) }
The Relational Calculus 57
Additional Examples (7)
Query 7: List the names of managers who have
at least one dependent.
Query 7: List the names of managers who have
at least one dependent.
The Relational Calculus 58
Example Queries Using the
Existential Quantifiers (7)
Q7: {e.FNAME, e.LNAME |
EMPLOYEE(e) and((d)(p)
(DEPARTMENT(d) and (DEPENDENT(p)
and e.SSN=d.MGRSSN and p.ESSN=e.SSN)) }
Q7: {e.FNAME, e.LNAME |
EMPLOYEE(e) and((d)(p)
(DEPARTMENT(d) and (DEPENDENT(p)
and e.SSN=d.MGRSSN and p.ESSN=e.SSN)) }
The Relational Calculus 59
Quantifiers in SQL The EXISTS function in SQL is
similar to the existential quantifier of the relational calculus.
The Relational Calculus 60
Quantifiers in SQL
SELECT …FROM …WHERE EXISTS (SELECT *
FROM R AS XWHERE P(X))
SELECT …FROM …WHERE EXISTS (SELECT *
FROM R AS XWHERE P(X))
The Relational Calculus 61
Quantifiers in SQL SQL does not include a universal
quantifier. Use of a negated existential
quantifier not (x) by writing NOT EXISTS is how SQL supports universal quantification.