Download presentation
Presentation is loading. Please wait.
Published byOctavia Martin Modified over 9 years ago
1
The Relational Calculus (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)
2
The Relational Calculus2 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
3
The Relational Calculus3 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
4
The Relational Calculus4 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.
5
The Relational Calculus5 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.
6
The Relational Calculus6 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.
7
The Relational Calculus7 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).
8
The Relational Calculus8 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
9
The Relational Calculus9 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.LNAME FROM EMPLOYEE T WHERE T.SALARY > 50000
10
The Relational Calculus10 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.
11
The Relational Calculus11 A general expression of tuple relational calculus is of the form {(t 1.A 1, t 2.A 2,…t n.A n | COND(t 1,t 2,…,t n,t n+1,t n+2,…,t n+m )} Where t 1.A 1, t 2.A 2, … t n.A n are tuple variables, each A i is an attribute of the relation on which t i ranges, and COND is a condition or formula Expressions of Tuple Relational Calculus
12
The Relational Calculus12 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(t i ), where R is a relation name and t i is a tuple variable. An atom of the form t i.A op t j.B, where op is one of the comparison operators in the set {=,>, ,<, , }, t i and t j are tuple variables, A is an attribute of the relation on which t i ranges, and B is an attribute of the relation on which t j ranges.
13
The Relational Calculus13 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 t i.A op c or c op t j.B, where op is one of the comparison operators in the set {=,>, ,<, , }, t i and t j are tuple variables, A is an attribute of the relation on which t i ranges, and B is an attribute of the relation on which t j ranges, and c is a constant value.
14
The Relational Calculus14 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. ”
15
The Relational Calculus15 Formula Atoms connected via and, or and not. Every atom is a formula If F 1 and F 2 are formulas, so are : (F 1 and F 2 ) (F 1 or F 2 ) not(F 1 ) not(F 2 )
16
The Relational Calculus16 The Existential and Universal Quantifiers Universal quantifier( ) Read for all Existential quantifiers( ) Read their exists
17
The Relational Calculus17 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.
18
The Relational Calculus18 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 – (F 1 and F 2 ), (F 1 or F 2 ), not(F 1 ), and not(F 2 ) – depending on whether it is free or bound in F 1 or F 2 (if it occur in either).
19
The Relational Calculus19 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 ’.
20
The Relational Calculus20 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
21
The Relational Calculus21 Formulas Every atom is a formula. If F1 and F2 are formulas, then so are (F 1 and F 2 ), (F 1 or F 2 ), not(F 1 ), and not(F 2 ). 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.
22
The Relational Calculus22 Truth Value for Existential Quantifiers 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.
23
The Relational Calculus23 Existential Quantifiers called existential quantifier because ( t)(F) is TRUE if “ there exists ” some tuple t that make F TURE.
24
The Relational Calculus24 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.
25
The Relational Calculus25 Universal Quantifiers called universal quantifier because every tuple in “ the universe of ” tuples must make F TRUE if ( t)(F) is to be TRUE.
26
The Relational Calculus26 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.
27
The Relational Calculus27 Example Queries Using the Existential Quantifiers (1) Q1: { t.FNAME, t.LNAME, t.ADDRESS | EMPLOYEE(t) and( d)(DEPARTMENT(d) andd.NAME= ‘ Research ’ and d.DNUMBER=t.DNO) } Q1: { t.FNAME, t.LNAME, t.ADDRESS | EMPLOYEE(t) and( d)(DEPARTMENT(d) andd.NAME= ‘ Research ’ and d.DNUMBER=t.DNO) }
28
The Relational Calculus28 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 ”
29
The Relational Calculus29 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)
30
The Relational Calculus30 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.
31
The Relational Calculus31 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) }
32
The Relational Calculus32 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.
33
The Relational Calculus33 Example Queries Using the Existential Quantifiers (8) Q8: {e.FNAME, e.LNAME, s.FNAME,s.LNAME | EMPLOYEE(e) and EMPLOYEE(s) ande.SUPERSSN=s.SSN} Q8: {e.FNAME, e.LNAME, s.FNAME,s.LNAME | EMPLOYEE(e) and EMPLOYEE(s) ande.SUPERSSN=s.SSN}
34
The Relational Calculus34 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.
35
The Relational Calculus35 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)) }
36
The Relational Calculus36 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.
37
The Relational Calculus37 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 | 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')))}
38
The Relational Calculus38 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.
39
The Relational Calculus39 Transforming Universal and Existential Quantifiers The not connective can be used to transform universal and existential quantifiers to equivalent formulas.
40
The Relational Calculus40 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)))
41
The Relational Calculus41 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))
42
The Relational Calculus42 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.
43
The Relational Calculus43 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.ESSN and 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.ESSN and p.PNO=x.PNUMBER) )) ) }
44
The Relational Calculus44 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.
45
The Relational Calculus45 Basic Components of Q3 Q3: {e.FNAME, e.LNAME | EMPLOYEE(e) and F ’ } F ’ = (( x)(not(PROJECT(x)) or F 1 )) F 1 = not(x.DNUM=5) or F 2 F 2 = (( w)(WORKS_ON(w) and e.SSN=w.ESSN and p.PNO=x.PNUMBER)) Q3: {e.FNAME, e.LNAME | EMPLOYEE(e) and F ’ } F ’ = (( x)(not(PROJECT(x)) or F 1 )) F 1 = not(x.DNUM=5) or F 2 F 2 = (( w)(WORKS_ON(w) and e.SSN=w.ESSN and p.PNO=x.PNUMBER))
46
The Relational Calculus46 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.
47
The Relational Calculus47 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 F 1 )) In F 1, not(x.DNUM=5) makes x TRUE for those PROJECT tuples we are not interested in “ whose DNUM is not 5 ” F 1 = not(x.DNUM=5) or F 2
48
The Relational Calculus48 Basic Components of Q3 F 2 specifies the condition that must hold on all remaining tuples “ all PROJECT tuples controlled by department 5 ” F 2 = (( w)(WORKS_ON(w) and e.SSN=w.ESSN and p.PNO=x.PNUMBER))
49
The Relational Calculus49 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.
50
The Relational Calculus50 Safe Expressions For example: {t | not(EMPLOYEE(t))} is unsafe. Yields all non-EMPLOYEE tuples in the universe.
51
The Relational Calculus51 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.
52
The Relational Calculus52 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
53
The Relational Calculus53 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)))) }
54
The Relational Calculus54 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.
55
The Relational Calculus55 Example Queries Using the Existential Quantifiers (6) Q6: {e.FNAME, e.LNAME | EMPLOYEE(e) and (not( d)(DEPENDENT(d) and e.SSN=d.ESSN)) }
56
The Relational Calculus56 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))) }
57
The Relational Calculus57 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.
58
The Relational Calculus58 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)) }
59
The Relational Calculus59 Quantifiers in SQL The EXISTS function in SQL is similar to the existential quantifier of the relational calculus.
60
The Relational Calculus60 Quantifiers in SQL SELECT … FROM … WHERE EXISTS (SELECT * FROM R AS X WHERE P(X)) SELECT … FROM … WHERE EXISTS (SELECT * FROM R AS X WHERE P(X))
61
The Relational Calculus61 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.