Beruflich Dokumente
Kultur Dokumente
Contents
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
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 3
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 4
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 5
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 result of such a query is set of all tuples t that satisfies COND(t).
The Relational Calculus 7
Example:
{ 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 8
Example:
{ t.FNAME, t.LNAME | EMPLOYEE(t) and t.SALARY>50000 } T.FNAME, T.LNAME EMPLOYEE T T.SALARY > 50000
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 10
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 one of the comparison operators in the set {=,>,u,<,e,{}, 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 12
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 {=,>,u,<,e,{}, 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.
13
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.
14
Formula
Atoms connected via and, or and not. Every atom is a formula If F1 and F2 are formulas, so are :
Universal quantifier()
Existential quantifiers()
16
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.
17
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 18
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.
19
For example
20
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 21
The formula (t)(F) is TRUE if the formula F evaluates to TRUE for some (at least one) tuple assigned to free occurrences of t in F; otherwise (t)(F) is FALSE.
22
Existential Quantifiers
called existential quantifier because (t)(F) is TRUE if there exists some tuple t that make F TURE.
23
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.
24
Universal Quantifiers
called universal quantifier because every tuple in the universe of tuples must make F TRUE if (t)(F) is to be TRUE.
25
Query 1 :
Retrieve the name and address of all employees who work for the Research department.
26
Q1:
{ t.FNAME, t.LNAME, t.ADDRESS | EMPLOYEE(t) and ( d)(DEPARTMENT(d) and d.NAME=Research and d.DNUMBER=t.DNO) }
27
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
28
EMPLOYEE(t), DEPARTMENT(d) specify range relations for t. The condition d.NAME=Research is selection condition.
Query 2
For every project located in Stafford, list the project number, the controlling department number, and the department manages last name, address, and birthdate.
30
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 31
Query 8:
For each employee, retrieve the employee's name, and the name of his or her immediate supervisor.
32
Q8:
33
Query 3
Find the names of employees who work on some projects controlled by department number 5.
34
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 35
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.
36
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')))}
The Relational Calculus 37
In general, UNION in relational algebra corresponds to an or connective in relational calculus. INTERSECTION corresponds to an and connective.
38
The not connective can be used to transform universal and existential quantifiers to equivalent formulas.
39
40
( x) (P(x)) ( x) (P(x)) (not x) (P(x)) not ( x) (P(x)) not( x) (P(x)) (not x) (P(x))
41
Query 3
Find the names of employees who work on all the projects controlled by department number 5.
42
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.ESSN and p.PNO=x.PNUMBER) )) ) }
43
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.
44
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.ESSN and p.PNO=x.PNUMBER))
45
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 46
Basic Components of Q3
In F, not(PROJECT(x)) makes x TRUE for all 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 47
Basic Components of Q3
F2 specifies the condition that must hold on all remaining tuples all PROJECT tuples controlled by department 5
48
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 49
Safe Expressions
For example:
{t | not(EMPLOYEE(t))} is unsafe.
50
One must be careful when specifying universal quantification. Judicious to follow rules to ensure expression make sense. Otherwise, unsafe expressions may result.
51
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 52
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)))) }
53
Query 6:
54
Q6:
55
Q6:
56
Query 7:
57
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)) }
58
Quantifiers in SQL
The EXISTS function in SQL is similar to the existential quantifier of the relational calculus.
59
Quantifiers in SQL
SELECT FROM WHERE EXISTS (SELECT FROM WHERE
* R AS X P(X))
60
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.
61