Download Relational Calculus-Databases-Lecture 05 Slides-Computer Science and more Slides Database Management Systems (DBMS) in PDF only on Docsity!
Lecture 5:
Relational calculus
www.cl.cam.ac.uk/Teaching/current/Databases/
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
Example
Find all sailors with a rating above 7
{<I,N,R,A> | <I,N,R,A> Sailors R>7}
- The condition <I,N,R,A> Sailors ensures that the domain variables are bound to the appropriate fields of the Sailors tuple
Example
{<I,N> | R,A.<I,N,R,A> Sailors}
- Simple projection and selection:
{<I,N> | R,A.<I,N,R,A> Sailors N=`Julia‟}
Example
Find the names of sailors rated >7 who’ve reserved boat 103
{ | I,A,R.<I,N,R,A> Sailors
R> SI,BI,D.(<SI,BI,D> Reserves I=SI BI=103)}
- Note the use of and = to „simulate‟ join
Example
Find the names of sailors rated >7 who’ve reserved a red boat
{ | I,A,R.<I,N,R,A> Sailors
R> SI,BI,D. (<SI,BI,D> Reserves SI=I B,C. (<B,C> Boats B=BI C=„red‟))}
Example
Find names of sailors who’ve reserved all boats
Example
Find names of sailors who’ve reserved all boats
{ | I,R,A. <I,N,R,A> Sailors B,C. ( (<B,C> Boats) ( <SI,BI,D> Reserves. I=SI BI=B)) } { | I,R,A. <I,N,R,A> Sailors <B,C> Boats. <SI,BI,D> Reserves. I=SI BI=B)) }
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
Example
Find names and ages of sailors with a rating above 7
{P | S Sailors. S.rating> P.sname=S.sname P.age=S.age}
Recall P ranges over tuple values
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.
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] = {<x 1 ,…,xn> | <x 1 ,…,xn> R}
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…
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