Database Management System 14: Relational Calculus
Database Management System 14: Relational Calculus
Database Management System 14: Relational Calculus
Chittaranjan Pradhan
Tuple Relational
Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
14.1
Relational Calculus
Relational Calculus
Chittaranjan Pradhan
Relational Calculus
Tuple Relational
Relational Calculus Calculus (TRC)
Safe Expressions
Relational calculus is non-procedural Queries
Domain Relational
Calculus (DRC)
In relational calculus, a query is solved by defining a solution Queries
14.2
Relational Calculus
Tuple Relational Calculus (TRC)
Chittaranjan Pradhan
Relational Calculus
Tuple Relational
Calculus (TRC)
Tuple Relational Calculus (TRC) Safe Expressions
Queries
A tuple variable is a variable that takes on tuples of a particular Domain Relational
relation schema as values Calculus (DRC)
Queries
14.3
Relational Calculus
Tuple Relational Calculus (TRC)...
Chittaranjan Pradhan
Domain Relational
Calculus (DRC)
• R ∈ Rel Queries
• R.a op S.b
• R.a op Constant or Constant op R.a
To represent the join and division of relational algebra by
relational calculus, we need quantifiers such as: existential for
join and universal for division
Relational Calculus
Tuple Relational Calculus (TRC) Tuple Relational
Calculus (TRC)
A formula is recursively defined by using the following rules: Safe Expressions
Queries
14.5
Relational Calculus
Safe Expressions
Chittaranjan Pradhan
Relational Calculus
Tuple Relational
Calculus (TRC)
Safe Expressions Safe Expressions
Queries
14.6
Relational Calculus
Queries
Chittaranjan Pradhan
Sailor Database
Relational Calculus
Sailors(sid, sname, rating, age)
Tuple Relational
Boats(bid, bname, color) Calculus (TRC)
Safe Expressions
Reserves(sid, bid, day) Queries
Domain Relational
Calculus (DRC)
Queries
Query: Find the names & ages of sailors with a rating above 4
{T/∃S ∈ Sailors (S.rating >4 ∧ T.sname=S.sname ∧ T.age=
S.age)}
Query: Find the sailor name, boat id & reservation date for each
reservation
{T/∃R ∈ Reserves ∃S ∈ Sailors (R.sid = S.sid ∧
T.sname=S.sname ∧ T.bid=R.bid ∧ T.day=R.day)}
Query: Find the names of sailors who have reserved boat 111
{T/∃R ∈ Reserves ∃S ∈ Sailors (R.sid = S.sid ∧ R.bid=111 ∧
T.sname=S.sname)}
14.7
Relational Calculus
Queries...
Chittaranjan Pradhan
Relational Calculus
Tuple Relational
Query: Find the names of sailors who have reserved a green boat Calculus (TRC)
Safe Expressions
14.8
Relational Calculus
Queries...
Chittaranjan Pradhan
Relational Calculus
Tuple Relational
Calculus (TRC)
Safe Expressions
Queries
Query: Find the names of sailors who have reserved all boats Domain Relational
Calculus (DRC)
Queries
{T/∃S ∈ Sailors ∀B ∈ Boats(∃R ∈ Reserves (S.sid=R.sid ∧
R.bid= B.bid ∧ T.sname= S.sname))}
14.9
Relational Calculus
Domain Relational Calculus (DRC)
Chittaranjan Pradhan
Relational Calculus
Domain Relational Calculus (DRC)
Tuple Relational
In tuple relational calculus, the variables range over the tuples Calculus (TRC)
Safe Expressions
The domain variables are the ones which range over the
underlying domains instead of over the relations
The result of this query is the set of all tuples <x1 , x2 , ... xn > for
which the formula evaluates to TRUE
14.10
Relational Calculus
Domain Relational Calculus (DRC)...
Chittaranjan Pradhan
Domain Relational
• <x1 , x2 , ... xn > ∈ Rel Calculus (DRC)
Queries
• X op Y
• X op Constant or Constant op X
A formula is recursively defined by using the following rules:
• Any atomic formula
• If p and q are formulae, then ¬p, p ∧ q, p ∨ q, or p ⇒ q are
also formulae
• If p is a formula that contains X as a domain variable, then
∃X(p) and ∀ X(p) are also formulae
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
14.11
Relational Calculus
Queries
Chittaranjan Pradhan
Sailor Database
Sailors(sid, sname, rating, age) Relational Calculus
Domain Relational
Query: Find all sailors with a rating above 7 Calculus (DRC)
Queries
Query: Find the names of sailors who have reserved a green boat
Relational Calculus
{<N>/∃I, T, A (<I, N, T, A> ∈ Sailors ∧ <I, Br, D> ∈ Reserves ∧ Tuple Relational
Calculus (TRC)
∃<Br, Bn, ’green’> ∈ Boats)} Safe Expressions
Queries
Domain Relational
Calculus (DRC)
Query: Find the names of sailors who have reserved at least 2 Queries
boats
{<N>/∃I, T, A (<I, N, T, A> ∈ Sailors ∧ ∃Br1, Br2, D1, D2(<I, Br1,
D1> ∈ Reserves ∧ <I, Br2, D2> ∈ Reserves ∧ Br1 6= Br2))}
Query: Find the names of sailors who have reserved all boats
{<N>/∃I, T, A(<I, N, T, A> ∈ Sailors ∧ ∀<B, Bn, C> ∈ Boats
(∃<Ir, Br, D> ∈ Reserves ( I=Ir ∧ Br=B)))}
14.13