Learning Resource
On
Database Management Systems
Chapter-7
Relational Calculus
Prepared By:
Kunal Anand, Asst. Professor
SCE, KIIT, DU, Bhubaneswar-24
Chapter Outcome
• After completion of this chapter, the students will
be able to:
– Define Relational Calculus
– Distinguish between Relational Algebra and Relational
Calculus.
– Explain Tuple Relational Calculus and write queries.
– Explain Domain Relational Calculus and write queries.
16 March 2021 2
Organization of this Chapter
• Introduction
• Difference between Relational Algebra and
Relational Calculus
• Tuple Relational Calculus
• Queries on TRC
• Domain Relational Calculus
• Queries on DRC
16 March 2021 3
Introduction
• Relational Calculus is a non-procedural query language. A
calculus expression specifies “what to retrieve” rather than
“how to retrieve”. This makes relational calculus different
from relational algebra.
• A calculus expression may be written in different ways, but the
way it is written has no bearing on how a query should be
evaluated.
• In relational calculus, a query is solved by defining a solution
relation in a single step.
• Relational calculus is mainly based on the well-known
propositional calculus, which is a method of calculating with
sentences or declarations. That's why it is also known as
declarative language
• Various types of relational calculus are:
• Tuple Relational Calculus (TRC)
• Domain Relational Calculus (DRC)
16 March 2021 4
Relational Algebra Vs Relational Calculus
• Relational Algebra, RA, is a
Procedural language.
• RA means “how to obtain
the result”.
• In RA, The order is
specified in which the
operations have to be
performed.
• RA is independent on
domain.
• RA is nearer to a
programming language.
• Relational Calculus, RC, is
non-procedural language.
• RC means “What to obtain”
as result.
• In RC, The order is not
specified.
• RC can be a domain
dependent.
• RC is not nearer to
programming language.
16 March 2021 5
Tuple Relational Calculus (TRC)
• The Tuple Relational Calculus i.e. TRC is based on specifying
a number of tuple variables.
• Each tuple variable usually ranges over a particular database
relation i.e. the variable may take as its value any individual
tuple from that relation.
• A TRC query is of the form: {t | P(t)}
– Where,
• t is a tuple variable
• P(t) is a condition (Boolean) expression involving t
that evaluates to either TRUE or FALSE for different
assignments of tuples to the variable t.
– The result of such a query is the set of all tuples t that
evaluate P(t) to TRUE i.e. these tuples t satisfy the
condition.
16 March 2021 6
An Example
• Sailor (sid, sname, rating, age)
– Query 1: Find all the sailors with a rating above 4
{s | SAILOR(s) AND s.rating > 4}
– Here, the condition SAILOR(s) specifies that the range
relation of tuple variable s is SAILOR.
– Each SAILOR tuple s that satisfies the condition
s.rating>4 will be retrieved.
– This query will retrieve all attribute value for each
selected SAILOR tuple s. To retrive only some of the
attributes, we need to write the specific attribute name in
the format “tuple.attribute”
16 March 2021 7
More Examples
• Query 2: Retrieve the details of the employees whose
salary is greater than 50000.
– {e | EMPLOYEE(e) AND e.salary > 50000}
– Here, if we need only some attributes like Emp_ID and
Fname then we can write:
• {e.Emp_ID, e.Fname | EMPLOYEE(e) AND e.salary >
50000}
• Query 3: Retrieve the birth date and address of the
employee whose name is John B. Smith.
– {e.dob, e.address | EMPLOYEE(e) AND e.Fname='John'
AND e.Minit='B' AND e.Lname='Smith'}
16 March 2021 8
Expression and Formula in TRC
• Let Rel → be a relation name;
R, S → be the tuple variables;
a → an attribute of R, b → an attribute of S;
op → operator in the set {<, ≤,>, ≥,=, != }.
• Rule 1: An Atomic formula is one of the following:
•R ∈Rel
•R.a op S.b
•R.a op Constant or Constant op R.a
• Rule 2: A formula or boolean condition is made up of one or more
atoms connected via the logical operators AND, OR, and NOT i.e.
If F1 and F2 are formulas, then so are F1 AND F2, F1 OR F2,
NOT(F1) or NOT(F2).
16 March 2021 9
The Existential and Universal Quantifiers
• To represent the join and division operation of relational
algebra by relational calculus, we need quantifiers such like
Existential quantifier (∃) for join and Universal quantifier(∀) for
division.
• A tuple variable t is bound if it is quantified i.e. the tuple t
appears in an (∃t)or(∀t) clause; otherwise it is free.
• Rule 3: If F is a formula, then so is (∃t)(F), where t is a tuple
variable. The formula (∃t)(F) is TRUE if F evaluates to TRUE
for at least one tuple assigned to free occurences of t in F;
otherise the formula (∃t)(F)is FALSE.
• Rule 4: If F is a formula then so is (∀t)(F), where t is a tuple
variable. This formula (∀t)(F) is TRUE if F evaluates to TRUE
for ALL the tuples assigned to free occurences of t in F; otherise
the formula (∀t)(F)is FALSE.
16 March 2021 10
An Example
• Consider the following relational schema
Sailor(sid, sname, rating, age)
Boat(bid, bname, color)
Reserves(sid, bid, day)
• Query 1: Find the names & ages of sailors with a
rating above 4
{ t | ∃s ∈Sailor(t.sname = s.sname ∧t.age = s.age) ∧ s.rating > 4 }
How to read: The set of all tuples t such that there exists a tuple
s in relation Sailor for which rating is greater than 4.
16 March 2021 11
Sample Queries in TRC
• Query 2: Find the sailor name, boat id & reservation date
for each reservation.
– { t | ∃ s ∈ Sailor(t.sname = s.sname) ∧ ∃ r ∈ Reserves
(t.bid = r.bid ∧t.day = r.day) ∧ r.sid = s.sid}
• Query 3: Find the names of sailors who have reserved boat
111.
– { t | ∃s∈ Sailor(t.sname = s.sname) ∧ ∃ r ∈ Reserves( r.sid
= s.sid ∧ r.bid=111)}
• Query 4: Find the names of sailors who have reserved a
green boat
– { t |∃ s ∈ Sailor( t.sname = s.sname) ∧ ∃r∈ Reserves( r.sid
= s.sid) ∧∃b∈ Boat( b.bid = r.bid ∧b.color = ’green’))}
16 March 2021 12
contd..
• Query 5: Find the names of sailors who have reserved at
least 2 boats.
– { t | ∃ s ∈ Sailor( t.sname = s.sname) ∧ ∃r1∈Reserves ∧ ∃r2
∈ Reserves ∧r1.sid = r2.sid ∧r1.bid != r2.bid)}
• Query 6: Find the names of sailors who have reserved all
boats.
– {t | ∃s ∈ Sailor( t.sname = s.sname) ∧ ∀b ∈ Boat( ∃r ∈
Reserves (r.sid = s.sid ∧ r.bid = b.bid))}
• Query 7: Find sailors who have reserved all green boats.
– { t | ∃s ∈ Sailor ∧ ∀b ∈ Boat( b.color=’green’ ∧ ∃r ∈
Reserves( r.sid = s.sid ∧ r.bid = b.bid))}
16 March 2021 13
Safe Expression
• Whenever we use universal quantifiers or existential
quantifiers, or negation of predicates in a calculus expression,
we must make sure that the resulting expression makes sense.
• A safe expression in relational calculus is one that is
guaranteed to yield a finite number of tuples as its result;
otherwise, the expression is called unsafe.
• That means, an expression is said to be safe if all values in its
result are from the domain of the expression
16 March 2021 14
Domain Relational Calculus (DRC)
• In tuple relational calculus, the variables range over the tuples
whereas in domain relational calculus, the variables range over
the domains.
• The domain variables are the ones which range over the
underlying domains instead of over the relations
• The domain relational calculus query has the form:
{<x1, x2, ... xn > | P(x1, x2, ... xn )}
– where xi is either a domain variable or a constant and
P(x1, x2,xn ) is the domain relational calculus formula
whose only free variables are the variables among the xi,
1 ≤ i ≤ n
• The result of this query is the set of all domains <x1, x2, ... xn
> for which the formula evaluates to TRUE
16 March 2021 15
Expression and Formula in DRC
• Let Rel → be a relation name;
X, Y → be the domain variables;
op → operator in the set {<, ≤,>, ≥,=, != }.
• Rule 1: An Atomic formula is one of the following:
•<x1, x2, ... xn > ∈Rel
•X op Y
•X op Constant or Constant op X
• Rule 2: A formula is recursively defined by using the
following rules:
•Any atomic formula
16 March 2021 16
contd..
• If p(x) is a formula that contains x as a free domain
variable, then∃x(p(x)) and ∀x(p(x)) are also formulae.
• If p and q are formulae, then ¬p, p ∧q, p ∨q are
also formulae.
• Rule 3: The quantifiers ∃& ∀are said to bind the
domain variable X. Whereas a variable is said to be
free in a formula if the formula does not contain an
occurrence of a quantifier that binds it.
16 March 2021 17
Queries on DRC
Consider the following relational schema:
Sailor Database
Sailors(sid, sname, rating, age)
Boats(bid, bname, color)
Reserves(sid, bid, day)
• Query 1: Find all sailors with a rating above 7
– {<Is, N, R,A> | <Is, N, R, A> ∈Sailors ∧R >7}
• Query 2: Find the names of sailors who reserved
boat 111
– {<N> | ∃<Is,R , A> ( <Is, N, R,A> ∈Sailors ∧∃ <Ir, Br, D>
∈Reserves ( Ir=Is ∧Br=111 ))}
16 March 2021 18
Queries
• Query 3: Find the names of sailors who have reserved a
green boat
– {<N> | ∃ <Is, R,A> ( <Is, N, R,A> ∈Sailors ∧∃ <Ir, Br, D>
∈Reserves( Ir=Is ∧ (∃<Ib, Bn, C> ∈Boats( Ib=Br ∧
C='Green'))}
• Query 4: Find the names of sailors who have reserved at
least 2 boats.
{<N>| ∃ <Is, R,A> (<Is, N, R,A> ∈Sailors ∧ (∃Br1(<Ir,
Br1, D1> ∈ Reserves( Ir=Is ) ∧ ∃Br2( <Ir, Br2, D2> ∈
Reserves( Ir=Is )) ∧Br1 != Br2)}
16 March 2021 19
Queries
• Query 5: Find the names of sailors who have
reserved all boats.
{<N>| ∃ <Is, R,A> ( <Is, N, R,A> ∈Sailors ∧∀<Ib,Bn,
C> ∈Boats(∃ <Ir, Br, D> ∈Reserves ( Ir=Is ∧ Br=Ib)))}
• Query 6: Find sailors who have reserved all green boats.
{<Is,N,R,A> | <Is,N,R,A> ∈Sailors ∧(∀<Ib,Bn, C>
∈Boats (C=’green’ ∧∃<Ir, Br, D> ∈Reserves ( Ir=Is
∧Br=Ib)))}
16 March 2021 20

Chapter-7 Relational Calculus

  • 1.
    Learning Resource On Database ManagementSystems Chapter-7 Relational Calculus Prepared By: Kunal Anand, Asst. Professor SCE, KIIT, DU, Bhubaneswar-24
  • 2.
    Chapter Outcome • Aftercompletion of this chapter, the students will be able to: – Define Relational Calculus – Distinguish between Relational Algebra and Relational Calculus. – Explain Tuple Relational Calculus and write queries. – Explain Domain Relational Calculus and write queries. 16 March 2021 2
  • 3.
    Organization of thisChapter • Introduction • Difference between Relational Algebra and Relational Calculus • Tuple Relational Calculus • Queries on TRC • Domain Relational Calculus • Queries on DRC 16 March 2021 3
  • 4.
    Introduction • Relational Calculusis a non-procedural query language. A calculus expression specifies “what to retrieve” rather than “how to retrieve”. This makes relational calculus different from relational algebra. • A calculus expression may be written in different ways, but the way it is written has no bearing on how a query should be evaluated. • In relational calculus, a query is solved by defining a solution relation in a single step. • Relational calculus is mainly based on the well-known propositional calculus, which is a method of calculating with sentences or declarations. That's why it is also known as declarative language • Various types of relational calculus are: • Tuple Relational Calculus (TRC) • Domain Relational Calculus (DRC) 16 March 2021 4
  • 5.
    Relational Algebra VsRelational Calculus • Relational Algebra, RA, is a Procedural language. • RA means “how to obtain the result”. • In RA, The order is specified in which the operations have to be performed. • RA is independent on domain. • RA is nearer to a programming language. • Relational Calculus, RC, is non-procedural language. • RC means “What to obtain” as result. • In RC, The order is not specified. • RC can be a domain dependent. • RC is not nearer to programming language. 16 March 2021 5
  • 6.
    Tuple Relational Calculus(TRC) • The Tuple Relational Calculus i.e. TRC is based on specifying a number of tuple variables. • Each tuple variable usually ranges over a particular database relation i.e. the variable may take as its value any individual tuple from that relation. • A TRC query is of the form: {t | P(t)} – Where, • t is a tuple variable • P(t) is a condition (Boolean) expression involving t that evaluates to either TRUE or FALSE for different assignments of tuples to the variable t. – The result of such a query is the set of all tuples t that evaluate P(t) to TRUE i.e. these tuples t satisfy the condition. 16 March 2021 6
  • 7.
    An Example • Sailor(sid, sname, rating, age) – Query 1: Find all the sailors with a rating above 4 {s | SAILOR(s) AND s.rating > 4} – Here, the condition SAILOR(s) specifies that the range relation of tuple variable s is SAILOR. – Each SAILOR tuple s that satisfies the condition s.rating>4 will be retrieved. – This query will retrieve all attribute value for each selected SAILOR tuple s. To retrive only some of the attributes, we need to write the specific attribute name in the format “tuple.attribute” 16 March 2021 7
  • 8.
    More Examples • Query2: Retrieve the details of the employees whose salary is greater than 50000. – {e | EMPLOYEE(e) AND e.salary > 50000} – Here, if we need only some attributes like Emp_ID and Fname then we can write: • {e.Emp_ID, e.Fname | EMPLOYEE(e) AND e.salary > 50000} • Query 3: Retrieve the birth date and address of the employee whose name is John B. Smith. – {e.dob, e.address | EMPLOYEE(e) AND e.Fname='John' AND e.Minit='B' AND e.Lname='Smith'} 16 March 2021 8
  • 9.
    Expression and Formulain TRC • Let Rel → be a relation name; R, S → be the tuple variables; a → an attribute of R, b → an attribute of S; op → operator in the set {<, ≤,>, ≥,=, != }. • Rule 1: An Atomic formula is one of the following: •R ∈Rel •R.a op S.b •R.a op Constant or Constant op R.a • Rule 2: A formula or boolean condition is made up of one or more atoms connected via the logical operators AND, OR, and NOT i.e. If F1 and F2 are formulas, then so are F1 AND F2, F1 OR F2, NOT(F1) or NOT(F2). 16 March 2021 9
  • 10.
    The Existential andUniversal Quantifiers • To represent the join and division operation of relational algebra by relational calculus, we need quantifiers such like Existential quantifier (∃) for join and Universal quantifier(∀) for division. • A tuple variable t is bound if it is quantified i.e. the tuple t appears in an (∃t)or(∀t) clause; otherwise it is free. • Rule 3: If F is a formula, then so is (∃t)(F), where t is a tuple variable. The formula (∃t)(F) is TRUE if F evaluates to TRUE for at least one tuple assigned to free occurences of t in F; otherise the formula (∃t)(F)is FALSE. • Rule 4: If F is a formula then so is (∀t)(F), where t is a tuple variable. This formula (∀t)(F) is TRUE if F evaluates to TRUE for ALL the tuples assigned to free occurences of t in F; otherise the formula (∀t)(F)is FALSE. 16 March 2021 10
  • 11.
    An Example • Considerthe following relational schema Sailor(sid, sname, rating, age) Boat(bid, bname, color) Reserves(sid, bid, day) • Query 1: Find the names & ages of sailors with a rating above 4 { t | ∃s ∈Sailor(t.sname = s.sname ∧t.age = s.age) ∧ s.rating > 4 } How to read: The set of all tuples t such that there exists a tuple s in relation Sailor for which rating is greater than 4. 16 March 2021 11
  • 12.
    Sample Queries inTRC • Query 2: Find the sailor name, boat id & reservation date for each reservation. – { t | ∃ s ∈ Sailor(t.sname = s.sname) ∧ ∃ r ∈ Reserves (t.bid = r.bid ∧t.day = r.day) ∧ r.sid = s.sid} • Query 3: Find the names of sailors who have reserved boat 111. – { t | ∃s∈ Sailor(t.sname = s.sname) ∧ ∃ r ∈ Reserves( r.sid = s.sid ∧ r.bid=111)} • Query 4: Find the names of sailors who have reserved a green boat – { t |∃ s ∈ Sailor( t.sname = s.sname) ∧ ∃r∈ Reserves( r.sid = s.sid) ∧∃b∈ Boat( b.bid = r.bid ∧b.color = ’green’))} 16 March 2021 12
  • 13.
    contd.. • Query 5:Find the names of sailors who have reserved at least 2 boats. – { t | ∃ s ∈ Sailor( t.sname = s.sname) ∧ ∃r1∈Reserves ∧ ∃r2 ∈ Reserves ∧r1.sid = r2.sid ∧r1.bid != r2.bid)} • Query 6: Find the names of sailors who have reserved all boats. – {t | ∃s ∈ Sailor( t.sname = s.sname) ∧ ∀b ∈ Boat( ∃r ∈ Reserves (r.sid = s.sid ∧ r.bid = b.bid))} • Query 7: Find sailors who have reserved all green boats. – { t | ∃s ∈ Sailor ∧ ∀b ∈ Boat( b.color=’green’ ∧ ∃r ∈ Reserves( r.sid = s.sid ∧ r.bid = b.bid))} 16 March 2021 13
  • 14.
    Safe Expression • Wheneverwe use universal quantifiers or existential quantifiers, or negation of predicates in a calculus expression, we must make sure that the resulting expression makes sense. • A safe expression in relational calculus is one that is guaranteed to yield a finite number of tuples as its result; otherwise, the expression is called unsafe. • That means, an expression is said to be safe if all values in its result are from the domain of the expression 16 March 2021 14
  • 15.
    Domain Relational Calculus(DRC) • In tuple relational calculus, the variables range over the tuples whereas in domain relational calculus, the variables range over the domains. • The domain variables are the ones which range over the underlying domains instead of over the relations • The domain relational calculus query has the form: {<x1, x2, ... xn > | P(x1, x2, ... xn )} – where xi is either a domain variable or a constant and P(x1, x2,xn ) is the domain relational calculus formula whose only free variables are the variables among the xi, 1 ≤ i ≤ n • The result of this query is the set of all domains <x1, x2, ... xn > for which the formula evaluates to TRUE 16 March 2021 15
  • 16.
    Expression and Formulain DRC • Let Rel → be a relation name; X, Y → be the domain variables; op → operator in the set {<, ≤,>, ≥,=, != }. • Rule 1: An Atomic formula is one of the following: •<x1, x2, ... xn > ∈Rel •X op Y •X op Constant or Constant op X • Rule 2: A formula is recursively defined by using the following rules: •Any atomic formula 16 March 2021 16
  • 17.
    contd.. • If p(x)is a formula that contains x as a free domain variable, then∃x(p(x)) and ∀x(p(x)) are also formulae. • If p and q are formulae, then ¬p, p ∧q, p ∨q are also formulae. • Rule 3: The quantifiers ∃& ∀are said to bind the domain variable X. Whereas a variable is said to be free in a formula if the formula does not contain an occurrence of a quantifier that binds it. 16 March 2021 17
  • 18.
    Queries on DRC Considerthe following relational schema: Sailor Database Sailors(sid, sname, rating, age) Boats(bid, bname, color) Reserves(sid, bid, day) • Query 1: Find all sailors with a rating above 7 – {<Is, N, R,A> | <Is, N, R, A> ∈Sailors ∧R >7} • Query 2: Find the names of sailors who reserved boat 111 – {<N> | ∃<Is,R , A> ( <Is, N, R,A> ∈Sailors ∧∃ <Ir, Br, D> ∈Reserves ( Ir=Is ∧Br=111 ))} 16 March 2021 18
  • 19.
    Queries • Query 3:Find the names of sailors who have reserved a green boat – {<N> | ∃ <Is, R,A> ( <Is, N, R,A> ∈Sailors ∧∃ <Ir, Br, D> ∈Reserves( Ir=Is ∧ (∃<Ib, Bn, C> ∈Boats( Ib=Br ∧ C='Green'))} • Query 4: Find the names of sailors who have reserved at least 2 boats. {<N>| ∃ <Is, R,A> (<Is, N, R,A> ∈Sailors ∧ (∃Br1(<Ir, Br1, D1> ∈ Reserves( Ir=Is ) ∧ ∃Br2( <Ir, Br2, D2> ∈ Reserves( Ir=Is )) ∧Br1 != Br2)} 16 March 2021 19
  • 20.
    Queries • Query 5:Find the names of sailors who have reserved all boats. {<N>| ∃ <Is, R,A> ( <Is, N, R,A> ∈Sailors ∧∀<Ib,Bn, C> ∈Boats(∃ <Ir, Br, D> ∈Reserves ( Ir=Is ∧ Br=Ib)))} • Query 6: Find sailors who have reserved all green boats. {<Is,N,R,A> | <Is,N,R,A> ∈Sailors ∧(∀<Ib,Bn, C> ∈Boats (C=’green’ ∧∃<Ir, Br, D> ∈Reserves ( Ir=Is ∧Br=Ib)))} 16 March 2021 20