22
Relational Calculus Logic, like whiskey, loses its beneficial effect when taken in too large quantities. --Lord Dunsany

Relational Calculus

  • Upload
    abia

  • View
    42

  • Download
    1

Embed Size (px)

DESCRIPTION

. Relational Calculus. . Logic, like whiskey, loses its beneficial effect when taken in too large quantities. --Lord Dunsany. Relational Calculus. Query has the form: { T | p ( T )} p(T) is a formula containing T Answer = tuples T for which p ( T ) = true . Formulae. - PowerPoint PPT Presentation

Citation preview

Page 1: Relational Calculus

Relational Calculus

Logic, like whiskey, loses its beneficial effect when taken in too large quantities.

--Lord Dunsany

Page 2: Relational Calculus

Relational Calculus

• Query has the form: {T | p(T)}– p(T) is a formula containing T

• Answer = tuples T for which p(T) = true.

Page 3: Relational Calculus

Formulae• Atomic formulae:

T Relation T.a op T.bT.a op constant

… op is one of • A formula can be:

– an atomic formula– – –

< >=≤≥≠, , , , ,

¬p, p∧q, p∨q,p⇒ q))(( RpR∃))(( RpR∀

Page 4: Relational Calculus

Free and Bound Variables• Quantifiers: and • Use of or binds X.

– A variable that is not bound is free. • Recall our definition of a query:

– {T | p(T)}

∃X ∀X

• Important restriction:— T must be the only free variable in p(T).— all other variables must be bound using a

quantifier.

Page 5: Relational Calculus

Simple Queries• Find all sailors with rating above 7

• Find names and ages of sailors with rating

above 7.

– Note: S is a variable of 2 fields (i.e. S is a projection of Sailors)

{S |S Sailors S.rating > 7}

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

Page 6: Relational Calculus

Find sailors rated > 7 who’ve reserved boat #103

{ S | SSailors S.rating > 7 R(RReserves R.sid = S.sid R.bid = 103) }

Joins

Page 7: Relational Calculus

Joins (continued)

• This may look cumbersome, but it’s not so different from SQL!

{ S | SSailors S.rating > 7 R(RReserves R.sid = S.sid B(BBoats B.bid = R.bid B.color = ‘red’)) }

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

Page 8: Relational Calculus

Universal QuantificationFind sailors who’ve reserved all boats

{ S | SSailors BBoats (RReserves (S.sid = R.sid B.bid = R.bid)) }

Page 9: Relational Calculus

A trickier example…

{ S | SSailors B Boats ( B.color = ‘red’ R(RReserves S.sid = R.sid B.bid = R.bid)) }

Find sailors who’ve reserved all Red boats in existence…

{ S | SSailors B Boats ( B.color ‘red’ R(RReserves S.sid = R.sid B.bid = R.bid)) }

Alternatively…

Page 10: Relational Calculus

a b is the same as a b

aT

F

T Fb

T

T T

F

Page 11: Relational Calculus

A Remark: Unsafe Queries

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

– Solution???? Don’t do that!

S S Sailors| ¬ ∈⎛

⎝⎜⎜

⎠⎟⎟

⎧⎨⎪

⎩⎪

⎫⎬⎪

⎭⎪

Page 12: Relational Calculus

Your turn …• Schema:

Movie(title, year, studioName)ActsIn(movieTitle, starName)Star(name, gender, birthdate, salary)

• Queries to write in Relational Calculus:– Find all movies by Paramount studio– … movies whose stars are all women– … movies starring Kevin Bacon– Find stars who have been in a film w/Kevin Bacon– Stars within six degrees of Kevin Bacon*– Stars connected to K. Bacon via any number of

films*** Try two degrees for starters ** Good luck with this one!

Page 13: Relational Calculus

Answers …

1. Find all movies by Paramount studio

{M | MMovie M.studioName = ‘Paramount’}

Page 14: Relational Calculus

Answers …

2. Movies whose stars are all women

{M | MMovie AActsIn((A.movieTitle = M.title) SStar(S.name = A.starName S.gender = ‘F’))}

Page 15: Relational Calculus

Answers …

3. Movies starring Kevin Bacon

{M | MMovie AActsIn(A.movieTitle = M.title

A.starName = ‘Bacon’))}

Page 16: Relational Calculus

Answers …4. Stars who have been in a film w/Kevin

Bacon{S | SStar AActsIn(A.starName = S.name A2ActsIn(A2.movieTitle = A.movieTitle A2.starName = ‘Bacon’))}

movie starA2:

S: name …

‘Bacon’

movie starA:

Page 17: Relational Calculus

Answers …

5. Stars within six degrees of Kevin Bacon

{S | SStar AActsIn(A.starName = S.name A2ActsIn(A2.movieTitle = A.movieTitle A3ActsIn(A3.starName = A2.starName A4ActsIn(A4.movieTitle = A3.movieTitle A4.starName = ‘Bacon’))}

two

Page 18: Relational Calculus

Two degrees:

S: name …

‘Bacon’

movie starA2:

movie starA:

movie starA4:

movie starA3:

Page 19: Relational Calculus

Answers …

6. Stars connected to K. Bacon via any number of films

• Sorry … that was a trick question– Not expressible in relational calculus!!

• What about in relational algebra?– We will be able to answer this question shortly

Page 20: Relational Calculus

Expressive Power

• Expressive Power (Theorem due to Codd):– Every query that can be expressed in relational

algebra can be expressed as a safe query in relational calculus; the converse is also true.

• Relational Completeness: Query language (e.g., SQL) can express every

query that is expressible in relational algebra/calculus.

(actually, SQL is more powerful, as we will see…)

Page 21: Relational Calculus

Question:• Can we express query #6 in relational

algebra?

• A: If we could, then by Codd’s theorem we could also express it in relational calculus. However, we know the latter is not possible, so the answer is no.

Page 22: Relational Calculus

Summary• Formal query languages — simple and powerful.

– Relational algebra is operational•used as internal representation for query

evaluation plans.– Relational calculus is “declarative”

•query = “what you want”, not “how to compute it” – Same expressive power

--> relational completeness.

• Several ways of expressing a given query– a query optimizer should choose the most efficient

version.