Sei sulla pagina 1di 17

Database Management Systems, R.

Ramakrishnan 1
Relational Calculus
Chapter 4
Database Management Systems, R. Ramakrishnan 2
Relational Calculus
Comes in two flavours: 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, R. Ramakrishnan 3
First-Order Predicate Logic
Predicate: is a feature of language which you can use
to make a statement about something, e.g., to
attribute a property to that thing.
Peter is tall. We predicated tallness of peter or attributed
tallness to peter.
A predicate may be thought of as a kind of function which
applies to individuals and yields a proposition.
Proposition logic is concerned only with sentential
connectives such as and, or, not.
Predicate Logic, where a logic is concerned not only
with the sentential connectives but also with the
internal structure of atomic propositions.
Database Management Systems, R. Ramakrishnan 4
First order predicate logic
First-order predicate logic, first-order says we
consider predicates on the one hand, and individuals
on the other; that atomic sentences are constricted by
applying the former to the latter; and that
quantification is permitted only over the individuals
First-order logic permits reasoning about
propositional connectives and also about
quantification.
All men are motal
Peter is a man
Peter is mortal


Database Management Systems, R. Ramakrishnan 5
Tuple Relational Calculus
Query: {T|P(T)}
T is tuple variable
P(T) is a formula that describes T
Result, the set of all tuples t for which P(t) evaluates
True.
Find all sailors with a rating above 7.

Atomic formula

R.a op S.b , op is one of
R.a op constant




} 7 . | { > . e rating S Sailors S S
l R Re e
< > = s > = , , , , ,
Database Management Systems, R. Ramakrishnan 6
TRC
Formula
Any atomic formula



Example
Find the names and ages of sailors with a rating above 7





q p q p q p p v . , , ,
)) ( ( R p R -
)) ( ( R p R
)} . . . . 7 . ( | { age S age P name S name P rating S Sailors S P = . = . > e -
Database Management Systems, R. Ramakrishnan 7
TRC
Find the names of sailors who have reserved
all boats


Find sailors S such that for all boats B there is
a Reserves tuple showing that sailor S has
reserved boat B
Boats B Sailors S P e e - | {
)) . . . . . . ( Re ( sname S sname P bid B bid R sid R sid S serves R = . = . = e -
Database Management Systems, R. Ramakrishnan 8
Domain Relational Calculus
Query has the form:
x x xn p x x xn 1 2 1 2 , ,..., | , ,...,
|
\

|
.
|
|
|

Answer includes all tuples that


make the formula be true.
x x xn 1 2 , ,...,
p x x xn 1 2 , ,...,
|
\

|
.
|
|
|
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, R. Ramakrishnan 9
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)
x x xn Rname 1 2 , ,..., e
< > = s > = , , , , ,
. v p p q p q , ,
-X p X ( ( ))
X p X ( ( ))
Database Management Systems, R. Ramakrishnan 10
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:
- X X
x x xn p x x xn 1 2 1 2 , ,..., | , ,...,
|
\

|
.
|
|
|

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, R. Ramakrishnan 11
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.
I N T A I N T A Sailors T , , , | , , , e . >

7
I N T A Sailors , , , e
I N T A , , ,
I N T A , , ,
Database Management Systems, R. Ramakrishnan 12
Find sailors rated > 7 whove 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.
I N T A I N T A Sailors T , , , | , , , e . > .

7
- e . = . =
|
\

|
.
|
|

`

Ir Br D Ir Br D serves Ir I Br , , , , Re 103
( ) - Ir Br D , , . . .
( )
( )
( )
- - - Ir Br D . . .
-
Database Management Systems, R. Ramakrishnan 13
Find sailors rated > 7 whove reserved a red boat
Observe how the parentheses control the scope of
each quantifiers binding.
This may look cumbersome, but with a good user
interface, it is very intuitive. (Wait for QBE!)

I N T A I N T A Sailors T , , , | , , , e . > .

7
- e . = .
|
\

Ir Br D Ir Br D serves Ir I , , , , Re
- e . = . =
|
\

|
.
|
|
|
|
.
|
|
|

B BN C B BN C Boats B Br C red , , , , ' '


Database Management Systems, R. Ramakrishnan 14
Find sailors whove 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.

I N T A I N T A Sailors , , , | , , , e .

e v
|
\

|
.
|
|
|
|
\

B BN C B BN C Boats , , , ,
- e . = . =
|
\

|
\

|
.
|
|
|
.
|
|
|
|
.
|
|
|

Ir Br D Ir Br D serves I Ir Br B , , , , Re
B BN C , ,
Database Management Systems, R. Ramakrishnan 15
Find sailors whove reserved all boats (again!)
Simpler notation, same query. (Much clearer!)
To find sailors whove reserved all red boats:
I N T A I N T A Sailors , , , | , , , e .

e B BN C Boats , ,
- e = . =
|
\

|
.
|
|
|
\

|
.
|
|

Ir Br D serves I Ir Br B , , Re

|
|
|
.
|

\
|
|
|
.
|

\
|
= . = e - v = B Br Ir I serves D Br Ir red C Re , , ' '
.....
Database Management Systems, R. Ramakrishnan 16
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.
S S Sailors | e
|
\

|
.
|
|

Database Management Systems, R. Ramakrishnan 17


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.

Potrebbero piacerti anche