lt19_dmcls_

download lt19_dmcls_

of 13

Transcript of lt19_dmcls_

  • 7/29/2019 lt19_dmcls_

    1/13

    Database Systems and

    Applications

    Lecture 19

    Domain Calculus

  • 7/29/2019 lt19_dmcls_

    2/13

    RelationalCalculus

  • 7/29/2019 lt19_dmcls_

    3/13

    Domain Relational Calculus

    Queryhas the form:

    {x1 , x2 , . . . , xnp(x1 , x2 , . . . , xn)}Answerincludes all tuples that

    make the formula be true.x1 , x2 , . . . , xn

    p (x1, x2 , . . . , xn )Formula is recursively defined, starting with

    simple atomic formulas (getting tuples fromrelations or making comparisons of values),and building bigger and better formulas usingthe logical connectives.

  • 7/29/2019 lt19_dmcls_

    4/13

    DRC Formulas Atomic formula:

    , or X op Y, or X opconstant

    op is one of

    Formula:

    an atomic formula, or

    , where p and q are formulas, or

    , where variable X is freein p(X), or , where variable X is free in p(X)

    x1 , x2 , . . . , xn Rname ,>,=,, ,

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

  • 7/29/2019 lt19_dmcls_

    5/13

    Free and Bound Variables

    The use ofquantifiers and in aformula is said to bind X. A variable that is not bound is free.

    Let us revisit the definition of a query:

    $ XX}} {}

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

    There is an important restriction: thevariables x1, ..., xn that appear to the leftof `| must be the onlyfree variables inthe formula p(...).

  • 7/29/2019 lt19_dmcls_

    6/13

    Find all sailors with a ratingabove 7

    The condition ensuresthat the domain variables I, N, TandA are boundto fields of the same Sailors tuple.

    The term to the left of `| (whichshould be read as such that) says that every tuple

    that satisfies T>7 is in the answer.

    Modify this query 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 SailorsT>7}I , N ,T , A Sailors

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

  • 7/29/2019 lt19_dmcls_

    7/13

    Find sailors rated > 7 whove reservedboat #103

    We have used as ashorthand for

    Note the use of to find a tuple inReserves that `joins with the Sailors tupleunder consideration.

    {I , N ,T , A I , N ,T , A SailorsT>7

    $Ir ,Br ,D (Ir , Br ,DReserves

    Ir=I Br=103

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

    $

  • 7/29/2019 lt19_dmcls_

    8/13

    Find sailors rated > 7 whove reserveda red boat

    Observe how the parentheses control the scope ofeach quantifiers binding.

    {I , N ,T , A I , N ,T , A SailorsT>7

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

  • 7/29/2019 lt19_dmcls_

    9/13

    Find sailors whove reservedall boats

    Find all sailors I such that for each 3-tuple either it is not a tuple in Boats or there is a tuple

    in Reserves showing that sailor I has reserved it.

    Find sailors whove reserved all redboats ?

    {I , N , T , A I , N ,T , A SailorsB, ital BN,C\( left ( langle B, ital BN,C rangle ital Boats right )}} {}

    (

    Ir ,Br ,

    D

    (Ir , Br ,D

    Re serves

    I=Ir

    Br=B )

    ))

    }

  • 7/29/2019 lt19_dmcls_

    10/13

    { | I,T,A ( Sailors

    Br1,Br2,D1,D2( Reserves

    reserves Br1 Br2)) }

    Find the sailors who reserved at least twoboats.

  • 7/29/2019 lt19_dmcls_

    11/13

  • 7/29/2019 lt19_dmcls_

    12/13

    Unsafe Queries, ExpressivePower

    syntactically correct calculusqueries that have an infinite number

    of answers! Unsafe queries. e.g., TRC

    Solution???? Dont do that!

    {S( S Sailors ) }

  • 7/29/2019 lt19_dmcls_

    13/13

    Summary The relational model has rigorously defined query

    languages simple and powerful. Relational algebra is more operational

    useful as internal representation for query evaluationplans.

    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 queryoptimizershould choose the most efficientversion.

    Algebra and safe calculus have same expressivepower

    leads to the notion ofrelational completeness.