You are on page 1of 25

Relational Calculus

Chapter 4, Part B

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1
Relational Calculus
 Comes in two flavors: Tuple relational calculus (TRC)
and Domain relational calculus (DRC).
 Calculus has variables, constants, comparison ops,
logical connectives and quantifiers.
 TRC: Variables range over (i.e., get bound to) tuples.
 DRC: Variables range over domain elements (= field values).
 Both TRC and DRC are simple subsets of first-order logic.
 Expressions in the calculus are called formulas. An
answer tuple is essentially an assignment of
constants to variables that make the formula
evaluate to true.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2
Tuple Relational Calculus
 A tuple variable takes on tuples of a particular relation
schema as values.
 A TRC query has the form { T | p(T) }, where T is a
tuple variable and p(T) denotes a formula that
describes T.
 the result of this query is the set of all tuples for which the
formula evaluates to true.
 Example: Find all sailors with a rating above 7.
 { S | S ∈ Sailors ∧ S.rating > 7 }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3
Tuple Relational Calculus
 Let Rel be a relation name, R and S be tuple
variables, a an attribute of R, and b an attribute of S.
 An atomic formula is one of the ff:
 R ∈ Rel
 R.a op R.b
 R.a op constant, or constant op R.a
 op denotes an operator in the set { <, >, =, ≤, ≥, ≠ }
 an atomic formula R ∈ Rel gives R the type of tuples
in Rel, and comparisons such as R.a op S.b and R.a op
constant induce type restrictions on the field R.a

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4
Tuple Relational Calculus
 A formula is recursively defined to be one of the
following, where p and q are themselves formulas,
and p(R) denotes a formula in which the variable R
appears.
 any atomic formula
 ¬p, p∧q, p∨q, or p⇒q
 ∃R(p(R)), where R is a tuple variable
• is true if p(R) evaluates to true for at least one tuple in R (for some)
• we will use ∃R ∈ Rel(p(R)) for ∃R(R∈Rel ∧ p(R))
 ∀R(p(R)), where R is a tuple variable
• is true if p(R) evaluates to true for all tuples in R (for all)
• we will use ∀R ∈ Rel(p(R)) for ∀R(R∈Rel ⇒ p(R))
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5
Tuple Relational Calculus
{ S| S ∈ Student ∧ (∃T ∈ Transcript
(S.Id= T.StudId ∧ T.CrsCode= ‘CS305’)) }
 A variable is said to be free in a formula if the the
formula does not contain an occurrence of a
quantifier that binds it.
 Free variables designate the tuples to be returned by the
query (used in targets)
 A tuple variable t is bound if it is quantified - it
appears in a ∃ t or a ∀ t.
 quantifiers ∃ and ∀ are said to bind variables
 Bound variables are used to make assertions about tuples
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6
Tuple Relational Calculus
 Examples:
 Find the names and ages of sailors with a rating
above 7.
{ P | ∃S ∈ Sailors(S.rating > 7 ∧ P.name = S.name
∧ P.age = S.age) }
 Find the sailor name, boat id, and reservation date
for each reservation.
{ P | ∃R ∈ Reserves ∃S ∈ Sailors
(R.sid = S.sid ∧ P.bid = R.bid ∧ P.day = R.day
∧ P.sname = S.sname) }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7
Tuple Relational Calculus
 Examples:
 Find the names of sailors who have reserved boat
103.
{ P | ∃S ∈ Sailors ∃R ∈ Reserves(R.sid = S.sid ∧ R.bid = 103
∧ P.name = S.name) }
 Find the names of sailors who have reserved at least
two boats.
{ P | ∃S ∈ Sailors ∃R1 ∈ Reserves ∃R2 ∈ Reserves
(S.sid = R1.sid ∧ R1.sid = R2.sid ∧ R1.bid ≠ R2.bid
∧ P.sname = S.sname) }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8
Tuple Relational Calculus
 Examples:
 Find the names of sailors who have reserved a red
boat.
{ P | ∃S ∈ Sailors ∃R ∈ Reserves ∃B ∈ Boats
(R.sid = S.sid ∧ B.bid = R.bid ∧ B.color = ‘red’
∧ P.sname = S.sname) }
 Find the names of sailors who have reserved a red
boat.
{ P | ∃S ∈ Sailors ∃R ∈ Reserves(R.sid = S.sid
∧ P.sname = S.sname
∧ ∃B ∈ Boats(B.bid = R.bid ∧ B.color = ‘red’)) }
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9
Tuple Relational Calculus
 Examples:
 Find the names of sailors who reserved all boats.
{ P | ∃S ∈ Sailors ∀B ∈ Boats
(∃R ∈ Reserves(S.sid = R.sid ∧ B.bid = R.bid
∧ P.sname = S.sname)) }
 Find sailors who reserved all red boats.
{ S | S ∈ Sailors ∧ ∀B ∈ Boats
(B.color = ‘red’ ⇒ (∃R ∈ Reserves
(S.sid = R.sid ∧ B.bid = R.bid))) }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10
Domain Relational Calculus
 Query has the form:

 Answer includes all tuples that
make the formula be true.
 Formula is recursively defined, starting with
simple atomic formulas (getting tuples from
relations or making comparisons of values),
and building bigger and better formulas using
the logical connectives.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11
DRC Formulas
 Atomic formula:
 , or X op Y, or X op constant
 op is one of
 Formula:
 an atomic formula, or
 , where p and q are formulas, or
 , where variable X is free in p(X), or
 , where variable X is free in p(X)
 The use of quantifiers and is said to bind X.
 A variable that is not bound is free.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12
Free and Bound Variables
 The use of quantifiers and in a formula is
said to bind X.
 A variable that is not bound is free.
 Let us revisit the definition of a query:

 There is an important restriction: the variables
x1, ..., xn that appear to the left of `|’ must be
the only free variables in the formula p(...).

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13
Find all sailors with a rating above 7

 The condition ensures that
the domain variables I, N, T and A are bound to
fields of the same Sailors tuple.
 The term to the left of `|’ (which should
be read as such that) says that every tuple
that satisfies T>7 is in the answer.
 Modify this query to answer:
 Find sailors who are older than 18 or have a rating under
9, and are called ‘Joe’.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14
Find sailors rated > 7 who have reserved
boat #103

 We have used as a shorthand
for

 Note the use of to find a tuple in Reserves that
`joins with’ the Sailors tuple under consideration.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15
Find sailors rated > 7 who’ve reserved a
red boat

 Observe how the parentheses control the scope of
each quantifier’s binding.
 This may look cumbersome, but with a good user
interface, it is very intuitive. (QBE later)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16
Find sailors who’ve reserved all boats

 Find all sailors I such that for each 3-tuple
either it is not a tuple in Boats or there is a tuple in
Reserves showing that sailor I has reserved it.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17
Find sailors who’ve reserved all
boats (again!)

 Simpler notation, same query. (Much clearer!)
 To find sailors who’ve reserved all red boats:

.....

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18
Unsafe Queries, Expressive Power
 It is possible to write syntactically correct calculus
queries that have an infinite number of answers!
Such queries are called unsafe.
 e.g.,

 It is known that every query that can be expressed
in relational algebra can be expressed as a safe
query in DRC / TRC; the converse is also true.
 Relational Completeness: Query language (e.g.,
SQL) can express every query that is expressible
in relational algebra/calculus.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19
QBE

 Query-by-Example (QBE) is another language
for querying (and, like SQL, for creating and
modifying) relational data.
 It is different from SQL, and from most other
database query languages, in having a
graphical user interface that allows users to
write queries by creating example tables on
the screen.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20
QBE
 QBE, like SQL, was developed at IBM and QBE is
an IBM trademark, but a number of other
companies sell QBE-like interfaces, including
Paradox.
 Some systems, such as Microsoft Access, offer
partial support for form-based queries and reflect
the influence of QBE.
 Often a QBE-like interface is offered in addition
to SQL, with QBE serving as a more intuitive
user-interface for simpler queries and the full
power of SQL available for more complex
queries.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21
QBE
 The following examples are based on IBM’s
Query Management Facility (QMF) and the
QBE version that it supports (Version 2,
Release 4).
 A tabular interface can provide the expressive
power of relational calculus (and more) in a
user-friendly form.
 The reader should concentrate on the
connection between QBE and domain
relational calculus (DRC).
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 22
QBE
 Find sailors who have reserved a boat for
8/24/96 and who are older than 25:

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 23
QBE
 find the colors of Interlake boats reserved by
sailors who have reserved a boat for 8/24/96
and who are older than 25:

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 24
Summary

 Relational calculus is non-operational, and
users define queries in terms of what they
want, not in terms of how to compute it.
(Declarativeness.)
 Algebra and safe calculus have same
expressive power, leading to the notion of
relational completeness.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 25