1 / 61

The Relational Calculus

The Relational Calculus. (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3). Contents. Introduction to Relational Calculus Tuple Relational Calculus Tuple Variables and Range Relations Formal Specification of Tuple Relational Calculus

verda
Download Presentation

The Relational Calculus

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Relational Calculus (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

  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.LNAME FROM EMPLOYEE T WHERE 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. Expressions of Tuple Relational Calculus • 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.Anare tuple variables, • each Ai is an attribute of the relation on which ti ranges, and • COND is a condition or formula 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 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. 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 formula 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. 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) } 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. 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) } 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. 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} 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. 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)) } 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. 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')))} 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))) 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 allthe 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.ESSN and 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. 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.ESSN and 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 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

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

More Related