Vous êtes sur la page 1sur 13

Database Systems and Applications

Lecture 19 Domain Calculus


Relational Calculus

Domain Relational Calculus


Query has the form:
p

{ x1, x2,..., xn ( x1,x2,..., xn )}

Answer includes all tuples make the formula

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.

that x1, x2,...,xntrue. be p ( x1,x2,...,xn )


Atomic formula:

DRC Formulas
, or X op Y, or X op
Rname

constant

x1, x2,..., xn

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)

p, pq , pq
$ X ( p( X ))
\) }} {}

X \( p \( X \)

Free and Bound Variables


The use of quantifiers $ Xand formula is said to bind X.

X}} {}

in a

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(...).

{ x1, x2,..., xn ( x1,x2,..., xn )}


p

Find all sailors with a rating above 7

{ I , N ,T , A I , N ,T , A

SailorsT >7

ensures The condition that the domain variables I, N, Sailors T and A are bound to fields of the same Sailors tuple. to the left of `| (which The term should be read as such that) says that every tuple that satisfies T>7 is in the answer. Modify this query to answer:

I , N ,T , A

I ,N ,T , A

I ,N ,T , A

Find sailors who are older than 18 or have a rating under 9, and are called Joe.

Find sailors rated > 7 whove reserved boat #103

{ I , N ,T , A I , N ,T , A

SailorsT >7

$ Ir , Br , D ( Ir , Br , D Reserves Ir=I Br=103 )}

We have used shorthand for

$ Ir , Br , D ( . .. ) $ Ir ( $ Br ( $ D ( .. . ) ) )

as a

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

Find sailors rated > 7 whove reserved a red boat

{ I , N ,T , A I , N ,T , A

SailorsT >7

$ Ir,Br,D(Ir,Br,D Reserves Ir=I $ B,BN ,C ( B,BN ,C BoatsB=BrC=' red' ) }


)

Observe how the parentheses control the scope of each quantifiers binding.

Find sailors whove reserved all boats


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.

{ I , N ,T , A I , N ,T , A

B, ital BN ,C\( left ( langle B, ital BN ,C rangle ital Boats right )}} {}

Sailors

Ir,Br,D ( Ir,Br,DReservesI=Ir Br=B ) }


))

Find sailors whove reserved all red boats ?

Find the sailors who reserved at least two boats.

{ <N>| I,T,A (<I,N,T,A> Sailors Br1,Br2,D1,D2(<I,Br1,D1> Reserves <I,Br2,D2> reserves Br1 Br2 )) }

Unsafe Queries, Expressive Power


syntactically correct calculus queries that have an infinite number of answers! Unsafe queries.
e.g., TRC

{S ( S Sailors ) }

Solution???? Dont do that!

Summary
The relational model has rigorously defined query languages simple and powerful. Relational algebra is more operational
useful as internal representation for query evaluation plans.

Relational calculus is non-operational


users define queries in terms of what they want, not in terms of how to compute it. (Declarative)

Several ways of expressing a given query


a query optimizer should choose the most efficient version.

Algebra and safe calculus have same expressive power


leads to the notion of relational completeness.

Vous aimerez peut-être aussi