Você está na página 1de 18

CS 222

Database Management System


Spring 2010-11

Lecture 2
K
Korra
Sathya
S th B
Babu
b
Department of Computer Science
NIT R
Rourkela
k l
1/22/2011

Lecture 2

Relational Calculus
RA and RC are formal Languages of Relational model
RA is Procedural and RC is Declarative (non procedural)

RC expression specifies what data to be retrieved rather than how it is to be retrieved

RC comes in two flavors


Tuple Relation Calculus (TRC), Domain Relation Calculus (DRC)

Calculus has variables, constants, comparison operators, logical


connectives and quantifiers.
TRC: Variables range over (i.e., get bound to) tuples
DRC: Variables range over domain elements (= field values)
Both
B th TRC and
d DRC are simple
i l subsets
b t off First
Fi t Order
O d Predicate
P di t Logic
L i
Both TRC and DRC are simple subsets of First Order Predicate Logic
In first-order logic (predicate calculus), predicate is a truth-valued function with
arguments
g
When we substitute values for the arguments, function yields an expression, called a
proposition, which can be either true or false

Expressions in the calculus are called formulas. An answer tuple is


essentially an assignment of constants to variables that make the
formula
1/22/2011evaluate to true
Lecture 2

Example
Sno

Sname

Rating

Age

Sid

bid

day

bid

bname

color

22

Dustine

45.0

22

101

10/10/08

101

Interlake

Blue

29

Brutus

33.0

22

102

10/10/08

102

Interlake

Red

31

Lubber

55.5

22

103

10/08/08

103

Clipper

Green

32

Andyy

25.5

22

104

10/07/08

104

Marine

red

58

Rusty

10

35.0

31

102

10/10/08

64

Horatio

35.0

31

103

11/06/08

71

Zorba

10

16 0
16.0

31

104

11/12/08

74

Horatio

35.0

64

101

09/05/08

85

Art

25.5

64

102

09/08/08

95

B b
Bob

63 5
63.5

74

103

09/08/08

Instance of Sailors

1/22/2011

Instance of Boats

Instance of Reserves

Lecture 2

Tuple Relational Calculus (TRC)


Interested in finding tuples for which a predicate is true.
Based on the use of tuple variables
Query: {T|P(T)}
T is tuple variable
P(T) is a formula (predicate) that describes T

Tuple variable is a variable that ranges over a named


relation: i.e, variable whose only permitted values are
tuples of the relation
Specify range of a tuple variable T as the employee
relation as: Employee (T)
Result,
Result the set of all tuples t for which P(t) evaluates
True.
Find all sailors with a rating above 7.

{S | S Sailors S .rating > 7}

1/22/2011

Lecture 2

Algebra
g
Vs Calculus
condition (formula)(R) = { T/ R (T) formula }
1,2,..,k (R) = { tk/ (u) (R(u) t[1] = u[i1] .. t[k] = u[ik] }
R S = { t/ R(t) S(t) }
R S = { t/ R(t) S(t) }
R X S = {tr+s/ (ur) (vs) (R(u) S(v) t[1] = u[i1] .. t[r] = u[r]
t[r+1] = v[1] .. t[r+s] = v[s] }

1/22/2011

Lecture 2

TRC Examples
Find all sailors with a rating above 7.

{ S | S Sailors S .rating > 7}


or
{ S | Sailors ( S ) S .rating > 7}
To find particular attribute such as rating.
rating We write
as:

{ S .rating | Sailors ( S ) S .rating > 7}

1/22/2011

Lecture 2

Tuple Relational Calculus


Can use two quantifiers to tell how many instances
the predicate applies to:
Existential
E i t ti l Quantifier
Q
tifi ( there
(th
exists)
i t )
Universal Quantifier ( for all)

Tuple variables qualified by or are called bound


variables, otherwise called free variables

1/22/2011

Lecture 2

Tuple Relational Calculus


Existential quantifier used in formulae that must be
true for at least one instance, such as:
{T/Staff(T) ( B) (Branch(B) ( B.branchNO=S.branchNo)
B.city = Rourkela)}

Means There
There exists a Branch Tuple with same
branchNo as the branchNo of the current Staff tuple,
T, and is located in Rourkela.

1/22/2011

Lecture 2

Tuple Relational Calculus


Universal quantifier used in statements about every
instance such as:
instance,
(B) (B.city Delhi)

Means For all Branch tuples, the address is not in


Delhi .
Delhi
One can also use ~(B)
( ) (B.city
(
y = Delhi)) which
means there are no branches with an address in
Delhi

1/22/2011

Lecture 2

Tuple Relational Calculus


Formulae should be unambiguous and make sense
A (well-formed) formula is made out of atoms
R(Si), where Si is a tuple variable and R is a relation
R.a op S.b , op is one of <, >, = , , ,
R.a op constant

Can recursively build up formulae from atoms:


An atom is a formula
If F1 and F2 are formulae, so are their conjunction, F1 F2;
disjunction, F1 F2 ; negation, ~ F1; and implication F1 F2
If F is a formula with free variable X, then (X)(F) and (X)
(F) are also formulae

1/22/2011

Lecture 2

10

Example Tuple Relational Calculus


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 names of sailors who have reserved all boats

{ P | S Sailors
S l
B Boats
B
(R Re serves( S .sid = R.sid R.bid = B.bid P.sname = S .sname))

1/22/2011

Lecture 2

11

Tuple
p Relational Calculus
Expressions can generate an infinite set.It is possible
to write syntactically correct calculus queries that
have an infinite number of answers! Such queries
are called unsafe.
For example
{ S/~Staff(S) }

S | S Sailors

To avoid this,
this add restriction that all values in result
must be values in the domain of the expression.

1/22/2011

Lecture 2

12

Domain Relational Calculus (DRC)


Uses variables that make values from domains
instead of tuples of relations.
relations
If F(d
( 1,
, n) stands for a formula composed
p
of
1 d2,
2 ,d
atoms and d1,d2,,dn represent domain variables,
then:
{<d1,d2,,d
dn>/ F(d1,d2,,d
dn)} is a general
gene al domain
relational calculus expression

1/22/2011

Lecture 2

13

Domain Relational Calculus


Find all sailors with a rating above 7
{<I,N,T,A>/ < I,N,T,A > Sailors T > 7 }
The condition < I,N,T,A > Sailors ensures that the
domain variables I, N, T and A are bound to fields of
the same Sailors tuple.
Modify this query to answer:
Find sailors who are older than 18 or have a
rating under 9, and are called Joe.

1/22/2011

Lecture 2

14

Domain Relational Calculus


Find sailors rated > 7 whove reserved boat #103

I, N , T , A | I, N , T , A Sailors T > 7

Ir,Br,D Ir, Br, D Re servesIr = I Br =103

1/22/2011

Lecture 2

15

Find sailors rated > 7 whove reserved a red


boat

I, N,T, A | I, N,T, A Sailors T > 7

Ir, Br, D Ir, Br, D Re serves Ir = I

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

Observe how the parentheses control the


scope off each
h quantifiers
tifi binding.
bi di

1/22/2011

Lecture 2

16

Find sailors who


whove
ve reserved all boats

I, N,T, A | I, N,T, A Sailors


l

B, BN, C Boats

1/22/2011

Ir, Br, D Re serves I = Ir Br = B

Lecture 2

17

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

Lecture 2

18

Você também pode gostar