1 Lecture 5: Relational calculus www.cl.cam.ac.uk/Teaching/current/Databases/

Slides:



Advertisements
Similar presentations
Relational Algebra Chapter 4, Part A
Advertisements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Part B.
Relational Calculus   Logic, like whiskey, loses its beneficial effect when taken in too large quantities. --Lord Dunsany.
1 TRC vs DRC 한욱신. 2 Queries in TRC and DRC  TRC Q = {t| f(t)} where t is a (free) tuple variable and f(t) is a well-formed formula  DRC.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
1 Relational Calculus Chapter 4 – Part II. 2 Formal Relational Query Languages  Two mathematical Query Languages form the basis for “real” languages.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
Relational Algebra Content based on Chapter 4 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
R ELATIONAL A LGEBRA M ORE POINTERS FROM T UESDAY.
Relational Calculus. Another Theoretical QL-Relational Calculus n Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus.
Lecture 4: Relational algebra
Relational Calculus CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth   We will occasionally use this arrow notation unless there is danger of.
Relational Algebra & Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 21, 2004 Some slide content.
Introduction to Database Systems 1 Relational Algebra Relational Model: Topic 3.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Relational Calculus R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion. Ronald Graham Elements of Ramsey.
Introduction to Database Systems 1 Relational Calculus Relational Model : Topic 2.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Calculus CS 186, Fall 2003, Lecture 6 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion.
Relational Algebra Wrap-up and Relational Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 11, 2003.
Rutgers University Relational Calculus 198:541 Rutgers University.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational Algebra.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
The Relational Model: Relational Calculus
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Section 4.3.
1 CS 430 Database Theory Winter 2005 Lecture 6: Relational Calculus.
Relational Algebra & Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 12, 2007 Some slide content.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4.
1 Relational Algebra. 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports.
Database Management Systems,1 Relational Calculus.
Relational Calculus. Non Procedural or Declarative Calculus has variables, constants, comparison ops, logical connectives and quantifiers. There are TWO.
Relational Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 17, 2007 Some slide content courtesy.
Relational Calculus R&G, Chapter 4. Relational Calculus Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Calculus.
Relational Calculus CS 186, Spring 2005, Lecture 9 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
Relational Algebra.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
1 Copyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B.
Copyright © Curt Hill The Relational Calculus Another way to do queries.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B.
Relational Calculus Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
1 Relational Algebra. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational model supports.
Relational Calculus Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Relational Calculus Chapter 4, Section 4.3.
Relational Algebra & Calculus
Relational Calculus Chapter 4, Part B
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Calculus Zachary G. Ives November 15, 2018
Relational Algebra & Calculus
Relational Calculus.
CS 186, Fall 2002, Lecture 8 R&G, Chapter 4
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Algebra & Calculus
Relational Calculus Chapter 4, Part B 7/1/2019.
Relational Calculus Chapter 4 – Part II.
Relational Calculus Chapter 4, Part B
Presentation transcript:

1 Lecture 5: Relational calculus

2 Relational calculus There are two versions of the relational calculus: –Tuple relational calculus (TRC) –Domain relational calculus (DRC) Both TRC and DRC are simple subsets of first- order logic The difference is the level at which variables are used: for fields (domains) or for tuples The calculus is non-procedural (‘declarative’) compared to the relational algebra

3 Domain relational calculus Queries have the form { | F(x 1,…,x n )} where x 1,…,x n are domain variables and F is a formula with free variables {x 1,…,x n } Answer: all tuples that make F(v 1,…,v n ) true

4 Example Find all sailors with a rating above 7 { |  Sailors  R>7} The condition  Sailors ensures that the domain variables are bound to the appropriate fields of the Sailors tuple

5 Example Simple projection: { |  R,A.  Sailors} Simple projection and selection: { |  R,A.  Sailors  N=`Julia’}

6 DRC formulae Atomic formulae: a ::= –  R –x i binop x j, x i binop c, c binop x j, unop c, unop x i DRC Formulae: P, Q ::= –a –  P, P  Q, P  Q –  x.P –  x.P Recall that  x and  x are binders for x

7 Example Find the names of sailors rated >7 who’ve reserved boat 103 { |  I,A,R.  Sailors  R>7   SI,BI,D.(  Reserves  I=SI  BI=103)} Note the use of  and = to ‘simulate’ join

8 Example Find the names of sailors rated >7 who’ve reserved a red boat { |  I,A,R.  Sailors  R>7   SI,BI,D. (  Reserves  SI=I   B,C. (  Boats  B=BI  C=‘red’))}

9 Example Find the names of sailors who have reserved at least two boats { | I,R,A. Sailors  BI 1,BI 2,D 1,D 2. Reserves  Reserves  BI 1 BI 2 }

10 Example Find names of sailors who’ve reserved all boats

11 Example Find names of sailors who’ve reserved all boats { | I,R,A. Sailors  B,C. (( Boats)  ( Reserves. I=SI  BI=B)) } { | I,R,A. Sailors   Boats.  Reserves. I=SI  BI=B)) }

12 Tuple relational calculus Similar to DRC except that variables range over tuples rather than field values For example, the query “Find all sailors with rating above 7” is represented in TRC as follows: {S | S  Sailors  S.rating>7}

13 Semantics of TRC queries In general a TRC query is of the form {t | P} where FV(P)={t} The answer to such a query is the set of all tuples T for which P[T/t] is true

14 Example Find names and ages of sailors with a rating above 7 {P |  S  Sailors. S.rating>7  P.sname=S.sname  P.age=S.age} Recall P ranges over tuple values

15 Example Find the names of sailors who have reserved at least two boats { P | SSailors. R 1 Reserves. R 2 Reserves. S.sid=R 1.sid  R 1.sid=R 2.sid  R 1.bid  R 2.bid  P.sname=S.sname}

16 Example Find the name of sailors who have reserved all the boats

17 Equivalence with relational algebra This equivalence was first considered by Codd in 1972 Codd introduced the notion of relational completeness –A language is relationally complete if it can express all the queries expressible in the relational algebra.

18 Encoding relational algebra Let’s consider the first direction of the equivalence: can the relational algebra be coded up in the (domain) relational calculus? This translation can be done systematically, we define a translation function [-] Simple case: [R] = { |  R}

19 Encoding selection Assume [e] = { | F } Then [  c (e)] = { | F  C’} where C’ is obtained from C by replacing each attribute with the corresponding variable

20 Encoding relational calculus Can we code up the relational calculus in the relational algebra? At the moment, NO! Given our syntax we can define ‘problematic’ queries such as {S |  (S  Sailors)} This (presumably) means the set of all tuples that are not sailors, which is an infinite set… 

21 Safe queries A query is said to be safe if no matter how we instantiate the relations, it always produces a finite answer Unfortunately, safety (a semantic condition) is undecidable  –That is, given a arbitrary query, no program can decide if it is safe Fortunately, we can define a restricted syntactic class of queries which are guaranteed to be safe Safe queries can be encoded in the relational algebra

22 Summary You should now understand The relational calculus –Tuple relational calculus –Domain relational calculus Translation from relational algebra to relational calculus Safe queries and relational completeness Next lecture: Basic SQL