You are on page 1of 15

Rela%onal

 Calculus  

INFS  614  
Tautologies  to  Know  
1.  P  ⇒  Q        ⇔  ¬  P  ∨  Q  
2.  P  ∧  Q        ⇔  ¬  (¬  P  ∨  ¬  Q)  
3.  P  ∨  Q        ⇔  ¬  (¬  P  ∧  ¬  Q)  
4.  ∀x  P(x)    ⇔  ¬  ∃x  (¬  P(x))  
5.  ∃x  P(x)      ⇔  ¬  ∀x  (¬  P(x))    
 
Example  Rela%ons  
•  Sailors  (sid:integer,  sname:string,  ra%ng:integer,  age:real)  
•  Boats  (bid:integer,  bname:string,  color:string)  
•  Reserves  (sid:integer,  bid:integer,  day:date)  
 
SID   SNAME   RATING   AGE   SID   SNAME   RATING   AGE  
22   Dus%n   7   45.0   28   Yuppy   9   35.0  
31   Lubber   8   55.5   31   Lubber   8   55.5  
58   Rusty   10   35.0   44   Guppy   5   35.0  

Instance  S1  of  Sailors   58   Rusty   10   35.0  


Instance  S2  of  Sailors  

SID   BID   DAY  


22   101   10/10/96  
58   103   11/12/96  

Instance  R1  of  Reserves  


Rela%onal  Calculus  
•  A  major  influence  on  SQL  
•  Declara%ve,  not  procedural  
•  Formulas,  not  operators  (e.g.  σ  condi%on)  
•  None  the  less  equivalent  to  rela%onal  algebra  
–  All  rela%onal  algebra  queries  can  be  rewri]en  as  
equivalent  rela%onal  calculus  queries  
–  And  vice  versa*    (more  later…)  
•  Two  forms:  Tuple  Rela%onal  Calculus  (TRC)    
 Domain  Rela%onal  Calculus  (DRC)  
•  We  will  only  learn  TRC  

*  nearly  
A  Rela%onal  Calculus  Query  
Tuple  variable,  its  domain  is  the  set  of  tuples  of  a  par%cular  
rela%on  scheme.  
(this  tuple  variable  is  “free”.    All  
others  are  somehow  “bound”)  

{S  |  S  ∈  Sailors  ∧  S.ra%ng  >  7}  

formula  

“return  the  values  of  S  which  make  the  formula  true”  

(the  key  is  to  write  formulas  correctly).  


Formulas  
(The  key  to  TRC)  
•  Atomic  Formulas:  
–  S  ∈  Rela%on  
–  S.A  op  constant       “op”  =  {<,  >,  =,  ≤,  ≥,  ≠}  
–  S.A  op  R.b  
•  Formulas:  
–  An  Atomic  formula  
–  ¬  P,  P  ∨  Q,  P  ∧  Q,  P  ⇒  Q    
“p”,  “q”  are  formulas  
–  ∃  R  (p(R))   R  is  a  tuple  variable.  
–  ∀R  (p(R))  
•  Example:   (R  is  “bound”  by  its  quan%fier)  

 {S  |  S  ∈  Sailors  ∧  S.ra%ng  >  7  


 ∧  ∃R  (R∈  Reserves  ∧  R.sid=S.sid  ∧  R.bid=103)}  
 ∧  ∃R∈  Reserves  (R.sid=S.sid  ∧  R.bid=103)}  ←  abbrevia%on  
“Free”  and  “Bound”  Tuple  Variables  
S  is  “free”  over  the  en%re  query;  no  quan%fier  binds  it.    In  
tRC,  only  the  1st  tuple  variable  (return  value)  in  the  query  
can  be  free.  

{S  |  S  ∈  Sailors  ∧  ∀B  ∈  Boats(∃R∈Reserves    


       (  S.sid=R.sid  ∧  R.bid=B.bid))}  

B  is  “bound”  by  it’s  universal  


quan%fier.    It  ranges  over  (has  scope   Likewise  for  R.    (It  is  bound  by  the  
of)  the  underlined  parenthe%cal   existen%al  quan%fier).  
expression,  within  which  its  
meaning  is  defined  by  the  quan%fier  
binding  it.  
TRC  Formulas  and  their  Rela%onal  
Algebra  Equivalents  
1.  σname=‘Bob’ (Sailors)    
   S  ∈  Sailors  ∧  S.sname  =  ‘Bob’  
2.  πname,sid  Sailors  
   {P  |  ∃S  ∈  Sailors  (P.name  =  S.name  ∧    
  Define    a  new  tuple  
  variable!  
   P.sid=S.sid)}  
3.    Sailors              Reserves  
   ∃S  ∈  Sailors  ∧  ∃R  ∈  Reserves  (S.sid=R.sid)  
 
Equivalents  (cont’d)  
4.  Sailors1  ∪  /  ∩  Sailors2  
   {S  |  ∃  S1  ∈  Sailors  ∃  S2  ∈  Sailors  
   (S=S1  ∨/∧  S=S2)}  
 
5.  Sailors1  -­‐  Sailors2  
 {S1  |  S1  ∈  Sailors1  (∀  S2  ∈  Sailors  
   (S1  ≠  S2)}  
 
   
Equivalents  (cont’d)  
6.  Division  
 “Find  the  boats  reserved  by  all  sailors”  
Rel  alg:  πsid,bid  Reserves  /  πsid  Sailors  
 
Rel  calc:  {B  |  B  ∈  Boats  ∧∀  S  ∈  Sailors  
 (∃R  ∈Reserves  (B.bid=R.bid  ∧  S.sid=R.sid))}  
 “Find  the  boats  such  that  for  all  sailors  there  exists  
a  reserva%on  for  that  boat.”  
Complica%ons!  
1.  Predicates  over  the  subject:  “red  boats”  
2.  Predicates  over  the  object:  “all  old  sailors”    
 
Equivalents  (cont’d)  
6.  Division  with  a  subject  predicate  
 “Find  the  red  boats  reserved  by  all  sailors”  

Answer  Set  

Red  Boats  
Non-­‐  Red  Boats  
Boats  

Boats  reserved   Boats  for  which  ∃  a  Sailor  who  did  


by  all  sailors   not  reserve  it.  

{B  |  B  ∈  Boats  (B.color=red  ∧∀S∈Sailors  


 ∃R  ∈Reserves  (B.bid=R.bid  ∧  S.sid=R.sid)}  
Equivalents  (cont’d)  
6.  Division  with  an  object  predicate  
 “Find  the  boats  reserved  by  all  sailors  older  than  40”  
Rel  Alg:    πsid,bid  (Reserves)  /  πsid  (σage>40  (Sailors))  
 
Rel  Calc:  
 *  describe  the  answer  set  (par%%on  it)  

Boats  reserved  by  all  


Boats  such  that  there  
sailors  older  than  40.  
exists  a  sailor  older  than  
40  who  has  not  reserved  
it.  

Answer   Complement  of  Answer  


*  Use  the  descrip%on  to  write  a  TRC  query  (here  we  
use  the  nega%on  of  the  complement!)  
{B  |  B  ∈  Boats  ∧¬(∃  S  ∈  Sailors  (S.age>40)  ∧  
 ¬  (∃R  ∈Reserves  (B.bid=R.bid  ∧  S.sid=R.sid)))}  
 
 Simplify  the  expression  
 (we  change  the  outer  “not  exists”  to  a  “for  all”)  
 
{B  |  B  ∈  Boats  ∧∀S  ∈  Sailors  (¬(S.age>40)    
 ∨  (∃R  ∈Reserves  (B.bid=R.bid  ∧  S.sid=R.sid)))}  
 
(now  we  change  ¬p∨q  into  p⇒q)  
 
{B  |  B  ∈  Boats  ∧∀S  ∈  Sailors  ((S.age>40)    ⇒  
 (∃R  ∈Reserves  (B.bid=R.bid  ∧  S.sid=R.sid)))}  
 
“Find  the  boats  such  that,  for  all  sailors,  if  they  are  older  than  40,  then  
they’ve  reserved  it.”  
Rela%onal  Completeness  
•  How  powerful  is  a  query  language  L ?  
•  A  language  L is  rela%onally  complete  if  it  can  
express  at  least  the  queries  expressible  using  
σ,  π,  X,  ∪,  -­‐  operators  of  rela%onal  algebra.  
•  TRC  is  rela%onally  complete  
•  SQL  is  rela%onally  complete  
•  …  of  course,  so  is  rela%onal  algebra  
Safety  of  Queries  in  TRC  
•  Some  queries  expressible  in  TRC  are  not  “safe”:                    
     {S  |  ¬(S  ∈  Sailors)}  
•  TRC  (logic)  is  more  powerful  than  rela%onal  
algebra.  
•  You  can  (inadvertently)  express  queries  with  
infinite  answers  which  do  not  correspond  to  the  
rela%on  instances  queried.    These  are  unsafe.  
•  Rela%onal  algebra  and  safe  TRC  are  equivalent  
(can  express  exactly  the  same  queries).  

You might also like