# 1 relational calculus chapter 4 – part ii. 2 formal relational query languages two mathematical...

Embed Size (px)

TRANSCRIPT

1

Relational Calculus

Chapter 4 – Part II

2

Formal Relational Query Languages

Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation:

Relational Algebra: • how to compute it , operational,

Representing execution plans (inside)

Relational Calculus: • what you want, declarative.

Influent SQL design (outside)

3

Relational Calculus

Calculus has• variables, constants, • comparison ops (<,>,=,,.), • logical connectives ( ,) • Quantifiers (,)

Comes in two flavours: • Tuple relational calculus (TRC) : Variables range

over tuples. (SQL)Domain relational calculus (DRC). Variables range over domain elements. (QBE)

• Both formula in TRC and DRC are simple subsets of first-order logic.

4

Tuple Relational Calculus (TRC)

Tuple variable: variable that takes on tuples of a relation schema as values.

Query form: T = tuple variablep(T) = formula that describes T

Result = set of all tuples t for which p(t) with T=t evaluates to be true

Formula P(T) specified in First Order Logic

TpT |

5

Find all sailors with a rating above 7

Variable S bound to a tuple in relation Sailors

S |SSailors S.rating7

6

TRC Formulas Formula:

an atomic formulaR Rel, R.a op S.b, R.a op constant, costant op R.a

Or recursively defined p, pq, pq, pq , where p, q are

formulasR(p(R)), where R is a tuple variableR(p(R)), where R is a tuple variable

Terms. bind --- the quantifier and are bind to variable R. free --- a variable is free if no quantifier binds to it in a

formula

7

Semantics of TRC Queries F is an atomic formula R Rel, and R is assigned a tuple in the

instance of relation Rel {S|SSailors}

F is a comparison R.a op S.b, R.a op constant, or constant op R.a, and the tuples assigned to R and S have field names R.a and S.b that make the comparison true.

F is of the form p, and p is not true;the form p q, and both p and q are true; the form p q, and one of them is true; the form p q, and q is true whenever p is true.

F is of the form R(p( R )), and there is some assignment of tuples to the free variables in p ( R ), including the variable R that make the formula p( R ) true;

F is of the form R (p ( R )), and there is some assignment of tuples to the free variables in p( R ) that makes the formula p( R ) true no matter what tuple is assigned to R.

8

sid sname rating age

22 dustin 7 45.0

31 lubber 8 55.558 rusty 10 35.0

sid bid day

22 101 10/10/9658 103 11/12/96

SailorsReserves

TRC Example

9

Find the names and ages of sailors with rating > 7

• P is a Tuple variable with two fields: name and age. - the only fields mentioned in P - P does not range over any relation in the query.

{ P | S Sailors (S.rating > 7 P.name = S.sname P.age = S.age)}

name age

Lubber 55.0

rusty 35.0

sid sname rating age

22 dustin 7 45.0

31 lubber 8 55.558 rusty 10 35.0

Sailors

10

Find the names of the sailors who reserved boat 103{ P | S Sailors R Reserves (R.sid = S.sid R.bid = 103 P.sname = S.sname)}

Retrieve all sailor tuples for which there exists a tuple in Reserves having the same value in the sid field and with bid =103

Question: How is the answer tuple looks like? (columns? Rows?)What is difference?

{ S | S Sailors R Reserves (R.sid = S.sid R.bid = 103)}

11

Find the names of the sailors who have reserved a red boat

{ P | S Sailors R Reserves (R.sid = S.sid P.sname = S.sname B Boats(B.bid = R.bid B.color ='red'))}

Retrieve all sailor tuples S for which there exists tuples R in Reserves and B in Boats such that R.sid = S.sid B.bid = R.bid B.color ='red'

{ P | S Sailors R Reserves B Boats(R.sid = S.sid B.bid = R.bid B.color ='red' P.sname = S.sname)}

12

Find the names of the sailors who have reserved all boat

Find sailors S such that for all boats B there is a Reserves tuple showing that sailor S has reserved boat B.

{ P | S Sailors B Boats(R Reserves (S.sid = R.sid R.bid = B.bid P.sname = S.sname))}

( , (,

Re ) / ( ))Tempsidssid bid

servesbid

Boats

sname Tempsids Sailors( )

13

Find the sailors who have reserved all red boat

{ P | S Sailors B Boats(B.color = 'red' (R Reserves (S.sid = R.sid R.bid = B.bid))}

( , (,

Re ) / ( ))Tempsidssid bid

servesbid

Boats

sname Tempsids Sailors( )

Question: How to change the algebraic representation?

14

Find the sailors who have reserved all red boat

{ P | S Sailors B Boats(B.color = 'red' (R Reserves (S.sid = R.sid R.bid = B.bid))}

Logically p q is equivalent to pq (Why?)

{ P | S Sailors B Boats(B.color 'red' (R Reserves (S.sid = R.sid R.bid = B.bid))}

is rewritten as:

to restrict attention to those red boat.

15

Domain Relational Calculus

Query has the form:x x xn p x x xn1 2 1 2, ,..., | , ,...,

Answer includes all tuples that make the formula be true.

x x xn1 2, ,...,

p x x xn1 2, ,...,

16

DRC Formulas Atomic formula:

<x1,x2,…,xn> Rel , where Rel is a relation with n variables

X op Y X op constant op is one of comparison ops (<,>,=,,.),

Or recursively defined p, pq, pq, pq , where p, q are

formulasX(p(X)), where X is a domain variableX(p(X)), where X is a domain variable

17

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

18

Find all sailors with a rating above 7

the domain variables I, N, T and A are bound to fields of the same Sailors tuple.

that every tuple that satisfies T>7 is in the answer.

Question: Modify 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, , ,

sid sname rating age

22 dustin 7 45.0

31 lubber 8 55.558 rusty 10 35.0

Sailors

19

Find sailors rating > 7 who’ve 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 . . .

sid bid day

22 101 10/10/9658 103 11/12/96

Reservessid sname rating age

22 dustin 7 45.0

31 lubber 8 55.558 rusty 10 35.0

Sailors

bid bname color

101 interlake

red

103 marine green

Boats

{<I,N,T,A>|<I,N,T,A> Sailors T>7 Ir,Br,D (<Ir,103,D> Reserves Ir =I )}

20

Find sailors rated > 7 who’ve reserved a red boat

Observe how the parentheses control the scope of each quantifier’s binding.

Rewrite using 'red' as constant• BN = red

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, , , , ' '

sid bid day

22 101 10/10/9658 103 11/12/96

Reservessid sname rating age

22 dustin 7 45.0

31 lubber 8 55.558 rusty 10 35.0

Sailors

bid bname color

101 interlake

red

103 marine green

Boats

21

Find sailors who’ve 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 Sailors, , , | , , ,

B BN C B BN C Boats, , , ,

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

sid bid day

22 101 10/10/9658 103 11/12/96

Reservessid sname rating age

22 dustin 7 45.0

31 lubber 8 55.558 rusty 10 35.0

Sailors

bid bname color

101 interlake

red

103 marine green

Boats

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

B BN C Boats, ,

Ir Br D serves I Ir Br B, , Re

22

Algebra vs. Calculus

Two formal query languages are equal in the power of expressivness?

Algebra Calculus?YES! Calculus Algebra?

• Unsafe query{S | (S Sailors)} Correct???

• Safe TRC --- Dom(Q , I)- answers for Q contains only values that are in

Dom(Q, I) R(p(R)), tuple r contains only constant in Dom(Q,I) R(p(R)), tuple r contains a constant that is not in

Dom(Q,I), then p(r) is true.

23

Summary Algebra and safe calculus have same expressive

power

Relationally complete ---if a query language can express all the

queries that can be expressed in relational algebra.

Relational calculus is non-operational, and users define queries in terms of what they want, not in terms of how to compute it. (Declarativeness.)