Download Relational Calculus

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Relational Calculus
More “declarative” than relational algebra
– Foundation for query languages (such as SQL)
– Relational algebra used more for physical operators
Relational Calculus
Comes in two flavours: Tuple relational calculus (TRC)
and Domain relational calculus (DRC)
– 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
–
We will study DRC
Database Management Systems, R. Ramakrishnan
1
Database Management Systems, R. Ramakrishnan
Domain Relational Calculus
DRC Formulas
Atomic formula:
Query has the form:
x1, x2,..., xn | p x1, x2,..., xn
x1, x2,..., xn ∈ Rname, or X op Y,
<, >, =, ≤,≥, ≠
–
Formula:
– an atomic formula, or … (more later)
Example of query with atomic formula
Formula is recursively defined
{< I , N , T , A > |< I , N , T , A >∈ Sailors }
Starting with simple atomic formulas
– Logical connectives
– Quantification
–
–
3
DRC Formulas
{< I , N , T , A >|< I , N , T , A >∈ Sailors ∧ T > 7}
–
Equivalent relational algebra query?
Find sailors who are older than 18 or have a rating under 9, and
are called ‘Joe’
Database Management Systems, R. Ramakrishnan
Database Management Systems, R. Ramakrishnan
4
Formula:
– an atomic formula, or
– ¬ p, p ∧ q, p ∨ q, where p and q are formulas, or
– ∃X ( p( X )) , where variable X is free in p(X), or
– ∀ X ( p( X )) , where variable X is free in p(X)
The use of quantifiers ∃ Xand ∀ Xis said to bind X.
Query using logical connectives
–
Equivalent relational algebra query?
DRC Formulas
Formula:
– an atomic formula, or
– ¬ p, p ∧ q, p ∨ q, where p and q are formulas, or
– (more later)
–
or X op constant
op is one of
–
Answer includes all tuples x1, x2,..., xn that
make the formula p x1, x2,..., xn be true.
Database Management Systems, R. Ramakrishnan
2
A variable that is not bound is free
For query:
–
5
x 1, x 2 ,..., x n | p x 1, x 2 ,..., x n
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
6
Find sailors rated > 7 who’ve reserved boat # 103
Find sailors rated > 7 who’ve reserved a red boat
I, N,T, A | I, N,T, A ∈ Sailors ∧ T > 7 ∧
I, N,T , A | I, N,T , A ∈ Sailors ∧ T > 7 ∧
∃ Ir, Br, D Ir, Br, D ∈ Re serves ∧ Ir = I ∧ Br = 103
∃ Ir, Br, D Ir, Br, D ∈ Re serves ∧ Ir = I ∧
We have used ∃ Ir , Br , D ( . . .)
for ∃ Ir ∃ Br ( ∃ D (. . .) )
(
)
∃ B, BN,C B, BN, C ∈ Boats ∧ B = Br ∧ C = ' red '
as a shorthand
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.
Note the use of ∃ to find a tuple in Reserves that
j̀oins with’ the Sailors tuple under consideration.
Database Management Systems, R. Ramakrishnan
7
Database Management Systems, R. Ramakrishnan
Find sailors who’ve reserved all boats
Find sailors who’ve reserved all boats
I, N,T, A | I, N,T, A ∈ Sailors ∧
I, N,T, A | I, N,T, A ∈ Sailors ∧
∀B,BN ,C B, BN ,C ∈Boats
∀B,BN ,C B, BN ,C ∈Boats∧
∃ Ir, Br, D Ir, Br, D ∈ Re serves ∧ I = Ir ∧ Br = B
∃ Ir, Br, D Ir, Br, D ∈ Re serves ∧ I = Ir ∧ Br = B
Find all sailors I such that for each 3-tuple B, BN,C
there is a tuple in Reserves showing that sailor I has
reserved it.
Database Management Systems, R. Ramakrishnan
8
What is wrong with this?
9
Database Management Systems, R. Ramakrishnan
Find sailors who’ve reserved all boats
Find sailors who’ve reserved all boats
I, N,T, A | I, N,T, A ∈ Sailors ∧
I, N,T, A | I, N,T, A ∈ Sailors ∧
∀ B, BN, C ¬ B, BN, C ∈ Boats ∨
10
¬∃B,BN ,C B, BN ,C ∈Boats ∧
∃ Ir, Br, D Ir, Br, D ∈ Re serves ∧ I = Ir ∧ Br = B
¬ ∃Ir,Br,D Ir, Br, D ∈Re serves∧I = Ir∧Br = B
Find all sailors I such that for each 3-tuple B, BN,C
either it is not a tuple in Boats or there is a tuple in
Reserves showing that sailor I has reserved it.
Find all sailors I such that there does not exist a 3tuple B, BN,C in Boats for which there does not exist
a tuple in Reserves showing that sailor I has
reserved it.
Database Management Systems, R. Ramakrishnan
11
Database Management Systems, R. Ramakrishnan
12
Find sailors who’ve reserved all red boats
Unsafe Queries, Expressive Power
Syntactically correct calculus queries that have an
infinite number of answers
I, N,T, A | I, N,T, A ∈ Sailors ∧
– Such queries are called unsafe.
– e.g., I , N ,T , A |¬ I , N ,T , A ∈Sailors
∀B,BN ,C B, BN ,C ∈Boats ∧C ='Red '
∃ Ir, Br, D Ir, Br, D ∈ Re serves ∧ I = Ir ∧ Br = B
Every relational algebra query can be expressed as
a safe query in DRC/TRC
Find all sailors I such that for each 3-tuple B, BN,C
there is a tuple in Reserves showing that sailor I has
reserved it.
Database Management Systems, R. Ramakrishnan
13
Summary
Relational calculus is “declarative” or nonoperational
– Users define queries in terms of what they want,
not in terms of how to compute it
Algebra and safe calculus have same
expressive power
– Relational “completeness”
Database Management Systems, R. Ramakrishnan
15
– And vice versa!
Relational Completeness: Query language (e.g.,
SQL) can express every query that is expressible
in relational algebra/calculus.
Database Management Systems, R. Ramakrishnan
14
Related documents