Copyright © 2004 Ramez Elmasri and Shamkant Navathe The Relational Calculus The main reference of this presentation is the textbook and PPT from : Elmasri.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Advertisements

The Relational Calculus
Copyright © 2004 Ramez Elmasri and Shamkant Navathe Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 15-1 Query Processing and.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1.
Slide Chapter 6 The Relational Algebra and Calculus.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Revision of Midterm 2 Prof. Sin-Min Lee Department of Computer Science.
Chapter 4 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Lecture 3 [Self Study] Relational Calculus
1 CS 430 Database Theory Winter 2005 Lecture 6: Relational Calculus.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Appendix C Overview of the QBE (Query-By-Example) Language.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
CS 380 Introduction to Database Systems Chapter 7: The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 3 Relational Model.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 14 A First Course in Database Systems.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Relational Algebra. 2 Outline  Relational Algebra Unary Relational Operations Relational Algebra Operations from Set Theory Binary Relational Operations.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1 CIS 611.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
The Relational Algebra and Calculus. Slide 6- 2 Chapter Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set.
The Relational Calculus (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 11 A First Course in Database Systems.
Copyright © 2004 Pearson Education, Inc.. Chapter 6 The Relational Algebra and Relational Calculus.
The Relational Algebra and The Relational Calculus
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 16 A First Course in Database Systems.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Jun-Ki Min.  Based on predicate calculus ◦ Predicate  a function whose value is true or false  non-procedural ◦ A relational calculus expression creates.
Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Chapter 4 The Relational Algebra and Calculus
Introduction to Relational Calculus Tuple Relational Calculus
The Relational Algebra and The Relational Calculus
CSE202 Database Management Systems
A First Course in Database Systems
The Relational Algebra and Relational Calculus
Database Management systems Subject Code: 10CS54 Prepared By:
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
11/22/2018.
12/5/2018.
12/31/2018.
Relational Calculus and QBE
Structured Query Language (3)
The Relational Algebra and The Relational Calculus
Relational Calculus and QBE
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
The Relational Algebra and The Relational Calculus
Presentation transcript:

Copyright © 2004 Ramez Elmasri and Shamkant Navathe The Relational Calculus The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems, 4 th edition, 2004, Chapter 6

Slide 6b-2 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Chapter Outline Relational Calculus  Tuple Relational Calculus  Domain Relational Calculus

Slide 6b-3 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Relational Calculus A relational calculus expression:  creates a new relation,  which is specified in terms of variables that range over rows of the stored database relations (in tuple calculus) or over columns of the stored relations (in domain calculus).

Slide 6b-4 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Relational Calculus In a calculus expression, there is no order of operations to specify how to retrieve the query result  a calculus expression specifies only what information the result should contain.  This is the main distinguishing feature between relational algebra and relational calculus. Relational calculus is considered to be a nonprocedural language.  This differs from relational algebra, where we must write a sequence of operations to specify a retrieval request; hence relational algebra can be considered as a procedural way of stating a query.

Slide 6b-5 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Tuple Relational Calculus The tuple relational calculus is based on specifying a number of tuple variables. Each tuple variable usually ranges over a particular database relation, meaning that the variable may take as its value any individual tuple from that relation. A simple tuple relational calculus query is of the form {t | COND(t)} where t is a tuple variable and COND (t) is a conditional expression involving t. The result of such a query is the set of all tuples t that satisfy COND (t).

Slide 6b-6 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Tuple Relational Calculus Example: To find the first and last names of all employees whose salary is above $50,000, we can write the following tuple calculus expression: {t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SALARY>50000} The condition EMPLOYEE(t) specifies that the range relation of tuple variable t is EMPLOYEE. The first and last name (PROJECTION  FNAME, LNAME ) of each EMPLOYEE tuple t that satisfies the condition t.SALARY>50000 (SELECTION  SALARY >50000 ) will be retrieved.

Slide 6b-7 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 The Existential and Universal Quantifiers Two special symbols called quantifiers can appear in formulas; these are the universal quantifier   ) and the existential quantifier   ). Informally, a tuple variable t is bound if it is quantified, meaning that it appears in an (  t) or (  t) clause; otherwise, it is free. 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 (at least one) tuple assigned to free occurrences of t in F; otherwise (  t)(F) is false.

Slide 6b-8 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 The Existential and Universal Quantifiers 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. It is called the universal or “for all” quantifier because every tuple in “the universe of” tuples must make F true to make the quantified formula true.

Slide 6b-9 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Example Query Using Existential Quantifier Retrieve the name and address of all employees who work for the ‘Research’ department. Query : {t.FNAME, t.LNAME, t.ADDRESS | EMPLOYEE(t) and   d) (DEPARTMENT(d) and d.DNAME=‘Research’ and d.DNUMBER=t.DNO) } The only free tuple variables in a relational calculus expression should be those that appear to the left of the bar ( | ). In above query, t is the only free variable; it is then bound successively to each tuple. If a tuple satisfies the conditions specified in the query, the attributes FNAME, LNAME, and ADDRESS are retrieved for each such tuple. The conditions EMPLOYEE (t) and DEPARTMENT(d) specify the range relations for t and d. The condition d.DNAME = ‘Research’ is a selection condition and corresponds to a SELECT operation in the relational algebra, whereas the condition d.DNUMBER = t.DNO is a JOIN condition.

Slide 6b-10 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Example Query Using Universal Quantifier Query 3: Find the names of employees who works on all projects controlled by department number 5. Q3: {e.LNAME, e.FNAME|EMPLOYEE(e) and ((  x)(not(PROJECT(x)) or (not(x.DNUM = 5) or ((  w)(WORKS_ON(w) and w.ESSN = e.SSN and x.PNUMBER = w.PNO)))))}

Slide 6b-11 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Basic component of the above query:  Q3:{e.LNAME, e.FNAME|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 w.ESSN = e.SSN and x.PNUMBER = w.PNO) Must exclude all tuples not of interest from the universal quantification by making the condition TRUE for all such tuples

Slide 6b-12 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Universally quantified variable x must evaluate to TRUE for every possible tuple in the universe In F’, not(PROJECT(x)) makes x TRUE for all tuples not in the relation of interest “PROJECT” 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 2 specifies the condition that must hold on all remaining tuples “all PROJECT tuples controlled by department 5”

Slide 6b-13 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Safe Expression Safe expressions in the relational calculus:  Is guaranteed to yield a finite number of tuples as its result  Unsafe expression may yield infinate number of tuples, and the tuples may be of different types. Example: {t|not(EMPLOYEE(t))}  Yields all non-EMPLOYEE tuples in the universe  Following the rules for Q3 discussed above guarantees safe expressions when using universal quantifiers

Slide 6b-14 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Using transformations from universal to existential quantifiers, can rephrase Q3 as Q3’:  Q3’:{e.KNAME, e.FNAME|EMPLOYEE(e) and (not(  x)(PROJECT(x) and (x.DNUM = 5) and (not(  w)(WORKS_ON(w) and w.ESSN = e.SSN and x.PNUMBER = w.PNO))))}

Slide 6b-15 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Transforming Universal and Existential Quantifier 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) 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)))

Slide 6b-16 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Transforming Universal and Existential Quantifier 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))

Slide 6b-17 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Another Example Additional examples:  Query 6: Find the names of employees without any dependents  Q6:{e.FNAME, e.LNAME|EMPLOYEE(e) and (not(  d)(DEPENDENT(d) and e.SSN = d.ESSN))} Transforming Q6 into Q6’ to use universal quantifier  Q6’:{e.FNAME, e.LNAME|EMPLOYEE(e) and ((  d)(not(DEPENDENT(d)) or not (e.SSN = d.ESSN)))}

Slide 6b-18 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Query 7: List the names of managers who have at least one dependent  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))}

Slide 6b-19 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Languages Based on Tuple Relational Calculus The language SQL is based on tuple calculus. It uses the basic SELECT FROM WHERE block structure to express the queries in tuple calculus where the SELECT clause mentions the attributes being projected, the FROM clause mentions the relations needed in the query, and the WHERE clause mentions the selection as well as the join conditions. SQL syntax is expanded further to accommodate other operations. (See Chapter 8).

Slide 6b-20 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Languages Based on Tuple Relational Calculus Another language which is based on tuple calculus is QUEL which actually uses the range variables as in tuple calculus. Its syntax includes: RANGE OF IS Then it uses RETRIEVE WHERE This language was proposed in the relational DBMS INGRES.

Slide 6b-21 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 The Domain Relational Calculus Another variation of relational calculus called the domain relational calculus, or simply, domain calculus is equivalent to tuple calculus and to relational algebra. The language called QBE (Query-By-Example) that is related to domain calculus was developed almost concurrently to SQL at IBM Research, Yorktown Heights, New York.  Domain calculus was thought of as a way to explain what QBE does.

Slide 6b-22 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 The Domain Relational Calculus Domain calculus differs from tuple calculus in the type of variables used in formulas:  rather than having variables range over tuples, the variables range over single values from domains of attributes.  To form a relation of degree n for a query result, we must have n of these domain variables—one for each attribute. An expression of the domain calculus is of the form {x1, x2,..., xn | COND(x1, x2,..., xn, xn+1, xn+2,..., xn+m)} where x1, x2,..., xn, xn+1, xn+2,..., xn+m are domain variables that range over domains (of attributes) and COND is a condition or formula of the domain relational calculus.

Slide 6b-23 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Example Query Using Domain Calculus Retrieve the birthdate and address of the employee whose name is ‘John B. Smith’. Query : {uv | (  q) (  r) (  s) (  t) (  w) (  x) (  y) (  z) (EMPLOYEE(qrstuvwxyz) and q=’John’ and r=’B’ and s=’Smith’)} Ten variables for the employee relation are needed, one to range over the domain of each attribute in order. Of the ten variables q, r, s,..., z, only u and v are free. Specify the requested attributes, BDATE and ADDRESS, by the free domain variables u for BDATE and v for ADDRESS. Specify the condition for selecting a tuple following the bar ( | )— namely, that the sequence of values assigned to the variables qrstuvwxyz be a tuple of the employee relation and that the values for q (FNAME), r (MINIT), and s (LNAME) be ‘John’, ‘B’, and ‘Smith’, respectively.

Slide 6b-24 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Another Examples Retrieve the name and address of all employess who work for the ‘Research’ department Query : {qsv | (  z) (  l) (  m) (EMPLOYEE(qrstuvwxyz) and DEPARTMENT (lmno) and l=‘Research’ and m=z)} At the query, m=z is a join condition, whereas l=‘Research’ is a selection condition. Find the names of employees who have no dependent {qs | (  t) (EMPLOYEE(qrstuvwxyz) and (not(  l) (DEPENDENT (lmnop) and t=l)))}

Slide 6b-25 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 QBE: A Query Language Based on Domain Calculus The language QBE (Query by Example) is a query language based on domain Calculus This language is based on the idea of giving an example of a query using example elements. For details, see Appendix D

Slide 6b-26 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Exercises Specify queries (a), (b), (c), (e), (f), (i), and (j) of the Exercise of previous topic (relational algebra) in both the tuple relational calculus and the domain relational calculus.