Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Lecture 5: Relational calculus www.cl.cam.ac.uk/Teaching/current/Databases/"— Presentation transcript:

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

2 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 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 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 5 Example Simple projection: { |  R,A.  Sailors} Simple projection and selection: { |  R,A.  Sailors  N=`Julia’}

6 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 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 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 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 10 Example Find names of sailors who’ve reserved all boats

11 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 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 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 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 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 16 Example Find the name of sailors who have reserved all the boats

17 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 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 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 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 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 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


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

Similar presentations


Ads by Google