EECE 433 Chapter 4 Part 2, Spring 2012
American University of Beirut · Database Systems · 18 pages
Showing the first 10 of 18 pages. Download the PDF to see all of it.
More EECE 433 files
- Problems Chapter 3 · Spring 2012ChapterView
- Chapter 7 · Spring 2012ChapterView
- Chapter 8 · Spring 2012ChapterView
- Problems Chapter 1 & 2 · Spring 2012ChapterView
- Problems Chapter 5 · Spring 2012ChapterView
- Problems Chapter 6 Handout · Spring 2012ChapterView
- Chapter 19 · Spring 2012ChapterView
- Problems Chapter 4 · Spring 2012ChapterView
Show the text of this chapter
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
Relational Calculus
Chapter 4, Part B
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
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
Tuple Relational Calculus
Query has the form: { T | p(T) }
Answer includes all tuples T that make the
formula p(T) 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
TRC Formulas
Atomic formula:
R.a op R.b
R.a op constant ; op is one of
Formula:
an atomic formula, or
, where p and q are formulas
, 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.
R Rel
, , , , ,
p p
q p q
,
,
X p X
( ( ))
X p X
( ( ))
X
X
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
TRC Formulas
Note that if a variable R does not appear in an
atomic formula of the form
, we follow
the convention that the type of R is a tuple
whose fields include only fields of R that
appear in the formula.
Example:
Print names of Sailors: {T |
(T.name = S.name)}
What would this give: {T |
(T.name = S.name)}
R Rel
SSailors
SSailors
TSailors
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
Examples of TRC Queries
Assume Sailors (sid, sname, rating, age); Reservations(sid, bid,
day); Boats(bid, bname, color)
Q12: Find the names and ages of Sailors with a rating above 7.
(i.e. print the name and age of sailor each time you find a sailor
with rating above 7).
Q2: Find the names of Sailors who have reserved a red boat
Q7: Find the names of Sailors who have reserved at least two
boats.
Q10: Find the names of Sailors who have reserved all boats.
Q14: Find the sailors who have reserved all red boats
(alternatively: For each sailor such that no matter what boat you
pick, if that boat is red then the sailor has a reservation for it).
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
Find the names of Sailors who
have reserved exactly two boats.
))}
.1
.3
.2
.3
.
.3
(
)
.
.
.2
.1
.2
.
.1
(( .
Re
3
Re
2
Re
1
|
{
R bid
bid
R
R bid
R bid
S sid
sid
R
S sname
P sname
R bid
bid
R
sid
R
S sid
R sid
S sid
serves
R
serves
R
serves
R
Sailors
S
P
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
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 3ed, R. Ramakrishnan and J. Gehrke
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.
x x
xn
Rname
1 2
,
,...,
, , , , ,
p p
q p q
,
,
X p X
( ( ))
X p X
( ( ))
X
X
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
1
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 3ed, R. Ramakrishnan and J. Gehrke
1
Q11: 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
, , ,
|
, , ,
7
I N T A
Sailors
, , ,
I N T A
, , ,
I N T A
, , ,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
1
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.
I N T A
I N T A
Sailors
T
, , ,
|
, , ,
7
Ir Br D
Ir Br D
serves
Ir
I
Br
,
,
,
,
Re
103
Ir Br D
,
,
...
Ir
Br
D ...
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
1
Q1: Find the names of Sailors
who have reserved boat 103
In-class example…
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
1
Find sailors rated > 7 who’ve reserved
a red boat
Observe how the parentheses control the scope
of each quantifier’s binding.
I N T A
I N T A
Sailors
T
, , ,
|
, , ,
7
Ir Br D Ir Br D
serves
Ir
I
,
,
,
,
Re
B BN C B BN C
Boats
B
Br
C
red
,
,
,
,
'
'
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
1
Q9: Find sailors who’ve reserved
all boats
To find sailors who’ve reserved all red boats:
I N T A
I N T A
Sailors
, , ,
|
, , ,
B BN C
Boats
,
,
Ir Br D
serves I
Ir
Br
B
,
,
Re
C
red
Ir Br D
serves I
Ir
Br
B
'
'
,
,
Re
.....
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
1
Q14: Find names of sailors
who’ve reserved all red boats
In-class example…
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
1
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
|
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
1
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.