06 calculus

Upload: upcursor

Post on 29-May-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 06 Calculus

    1/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

    Relational Calculus

    Chapter 4, Part B

  • 8/9/2019 06 Calculus

    2/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

    Relational Calculus

    Comes in two flavors: Tuple relational calculus (TRC)and Domain relational calculus (DRC).Calculus has variables, constants, comparison ops,

    logical connectivesand quantifiers.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.

    Expressions in the calculus are called formulas. Ananswer tuple is essentially an assignment ofconstants to variables that make the formulaevaluate to true.

  • 8/9/2019 06 Calculus

    3/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3

    Tuple Relational Calculus

    A tuple variable takes on tuples of a particular relationschema as values.A TRC query has the form { T | p(T) }, where T is a

    tuple variable and p(T) denotes a formula thatdescribes T .the result of this query is the set of all tuples for which theformula evaluates to true .

    Example: Find all sailors with a rating above 7.

    { S | S Sailors S.rating > 7 }

  • 8/9/2019 06 Calculus

    4/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

    Tuple Relational Calculus

    Let Rel be a relation name, R and S be tuplevariables, a an attribute of R, and b an attribute of S.An atomic formula is one of the ff:

    R

    RelR.a op R.bR.a op constant , or constant op R.a

    op denotes an operator in the set { , =, , , }an atomic formula R Rel gives R the type of tuplesin Rel, and comparisons such as R.a op S.b and R.a opconstant induce type restrictions on the field R.a

  • 8/9/2019 06 Calculus

    5/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5

    Tuple Relational Calculus

    A formula is recursively defined to be one of thefollowing, where p and q are themselves formulas,and p(R) denotes a formula in which the variable R appears.

    any atomic formulap, p q, pq, or p qR(p(R)), where R is a tuple variable

    is true if p(R) evaluates to true for at least one tuple in R (for some) we will use R Rel(p(R)) for R(RRel p(R)) R(p(R)), where R is a tuple variable

    is true if p(R) evaluates to true for all tuples in R (for all) we will use R Rel(p(R)) for R(RRel p(R))

  • 8/9/2019 06 Calculus

    6/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6

    Tuple Relational Calculus

    { S| S Student (T Transcript(S.Id= T.StudId T.CrsCode= CS305)) }

    A variable is said to be free in a formula if the the

    formula does not contain an occurrence of aquantifier that binds it.Free variables designate the tuples to be returned by thequery (used in targets)

    A tuple variable t is bound if it is quantified - itappears in a t or a t.

    quantifiers and are said to bind variablesBound variables are used to make assertions about tuples

  • 8/9/2019 06 Calculus

    7/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7

    Tuple Relational Calculus

    Examples:Find the names and ages of sailors with a ratingabove 7.

    { P |

    S

    Sailors(S.rating > 7

    P.name = S.name P.age = S.age) }Find the sailor name, boat id, and reservation datefor each reservation.

    { P | R Reserves S Sailors(R.sid = S.sid P.bid = R.bid P.day = R.day

    P.sname = S.sname) }

  • 8/9/2019 06 Calculus

    8/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

    Tuple Relational Calculus

    Examples:Find the names of sailors who have reserved boat103.

    { P |

    S

    Sailors

    R

    Reserves(R.sid = S.sid

    R.bid = 103 P.name = S.name) }Find the names of sailors who have reserved at leasttwo boats.

    { P | S Sailors R1 Reserves R2 Reserves

    (S.sid = R1.sid R1.sid = R2.sid R1.bid R2.bid P.sname = S.sname) }

  • 8/9/2019 06 Calculus

    9/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9

    Tuple Relational Calculus

    Examples:Find the names of sailors who have reserved a redboat.

    { P |

    S

    Sailors

    R

    Reserves

    B

    Boats(R.sid = S.sid B.bid = R.bid B.color = red P.sname = S.sname) }

    Find the names of sailors who have reserved a redboat.

    { P | S Sailors R Reserves(R.sid = S.sid P.sname = S.sname

    B Boats(B.bid = R.bid B.color = red)) }

  • 8/9/2019 06 Calculus

    10/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

    Tuple Relational Calculus

    Examples:Find the names of sailors who reserved all boats.

    { P | S Sailors B Boats

    (

    R

    Reserves(S.sid = R.sid

    B.bid = R.bid P.sname = S.sname)) }Find sailors who reserved all red boats.

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

  • 8/9/2019 06 Calculus

    11/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11

    Domain Relational Calculus

    Query has the form:

    Answer includes all tuples thatmake the formula be true.

    Formula is recursively defined, starting withsimple atomic formulas (getting tuples fromrelations or making comparisons of values),and building bigger and better formulas usingthe logical connectives.

  • 8/9/2019 06 Calculus

    12/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12

    DRC Formulas Atomic formula:

    , or X op Y, or X op constantop 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)The use of quantifiers and is said to bind X.

    A variable that is not bound is free.

  • 8/9/2019 06 Calculus

    13/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13

    Free and Bound Variables

    The use of quantifiers and in a formula issaid to bind X.

    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 bethe only free variables in the formula p(...).

  • 8/9/2019 06 Calculus

    14/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

    Find all sailors with a rating above 7

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

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

  • 8/9/2019 06 Calculus

    15/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15

    Find sailors rated > 7 who have reservedboat #103

    We have used as a shorthandfor

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

  • 8/9/2019 06 Calculus

    16/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16

    Find sailors rated > 7 whove reserved ared boat

    Observe how the parentheses control the scope ofeach quantifiers binding.This may look cumbersome, but with a good userinterface, it is very intuitive. (QBE later)

  • 8/9/2019 06 Calculus

    17/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17

    Find sailors whove reserved all boats

    Find all sailors I such that for each 3-tuple

    either it is not a tuple in Boats or there is a tuple inReserves showing that sailor I has reserved it.

  • 8/9/2019 06 Calculus

    18/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18

    Find sailors whove reserved allboats (again!)

    Simpler notation, same query. (Much clearer!)

    To find sailors whove reserved all red boats:.....

  • 8/9/2019 06 Calculus

    19/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19

    Unsafe Queries, Expressive Power

    It is possible to write syntactically correct calculusqueries that have an infinite number of answers!Such queries are called unsafe.

    e.g.,

    It is known that every query that can be expressedin relational algebra can be expressed as a safequery in DRC / TRC; the converse is also true.Relational Completeness: Query language (e.g.,SQL) can express every query that is expressiblein relational algebra/calculus.

  • 8/9/2019 06 Calculus

    20/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

    QBE

    Query-by-Example (QBE) is another languagefor querying (and, like SQL, for creating andmodifying) relational data.It is different from SQL, and from most otherdatabase query languages, in having agraphical user interface that allows users towrite queries by creating example tables onthe screen.

    20

  • 8/9/2019 06 Calculus

    21/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

    QBEQBE, like SQL, was developed at IBM and QBE isan IBM trademark, but a number of othercompanies sell QBE-like interfaces, includingParadox.

    Some systems, such as Microsoft Access, offerpartial support for form-based queries and reflectthe influence of QBE.Often a QBE-like interface is offered in additionto SQL, with QBE serving as a more intuitiveuser-interface for simpler queries and the fullpower of SQL available for more complexqueries.

    21

  • 8/9/2019 06 Calculus

    22/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

    QBE

    The following examples are based on IBMsQuery Management Facility (QMF) and theQBE version that it supports (Version 2,

    Release 4).A tabular interface can provide the expressivepower of relational calculus (and more) in auser-friendly form.

    The reader should concentrate on theconnection between QBE and domainrelational calculus (DRC).

    22

  • 8/9/2019 06 Calculus

    23/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

    QBE

    Find sailors who have reserved a boat for8/24/96 and who are older than 25:

    23

  • 8/9/2019 06 Calculus

    24/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

    QBE

    find the colors of Interlake boats reserved bysailors who have reserved a boat for 8/24/96and who are older than 25:

    24

  • 8/9/2019 06 Calculus

    25/25

    Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 25

    Summary

    Relational calculus is non-operational, andusers define queries in terms of what theywant, not in terms of how to compute it.(Declarativeness.)Algebra and safe calculus have sameexpressive power, leading to the notion ofrelational completeness.