4 - relational calculus-2

Upload: subrat

Post on 21-Feb-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/24/2019 4 - Relational Calculus-2

    1/18

    CS 222

    Spring 2010-11

    Lecture 2

    orra a ya a uDepartment of Computer Science

    1/22/2011 Lecture 2 1

    our e a

  • 7/24/2019 4 - Relational Calculus-2

    2/18

    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, logicalconnectives and quantifiers. TRC: Variables range over (i.e., get bound to) tuples

    DRC: Variables range over domain elements (= field values)

    o an are s mp e su se s o rs r er re ca e og c

    Both TRC and DRC are simple subsets of First Order Predicate Logic

    In first-order logic (predicate calculus), predicate is a truth-valued function withar uments

    When we substitute values for the arguments, function yields an expression, called aproposition, which can be either true or false

    Expressions in the calculus are called formulas. An answer tuple isessentially an assignment of constants to variables that make the

    formula evaluate to true1/22/2011 Lecture 2 2

  • 7/24/2019 4 - Relational Calculus-2

    3/18

    ExampleSno Sname Rating Age

    22 Dustine 7 45.0

    Sid bid day

    22 101 10/10/08

    bid bname color

    101 Interlake Blue

    29 Brutus 1 33.0

    31 Lubber 8 55.5

    32 And 8 25.5

    22 102 10/10/08

    22 103 10/08/08

    22 104 10/07/08

    102 Interlake Red103 Clipper Green

    58 Rusty 10 35.0

    64 Horatio 7 35.0

    31 102 10/10/08

    31 103 11/06/08

    Instance of Boats

    .

    74 Horatio 9 35.0

    85 Art 3 25.5

    64 101 09/05/08

    64 102 09/08/08

    o .

    Instance of Sailors Instance of Reserves

    1/22/2011 Lecture 2 3

  • 7/24/2019 4 - Relational Calculus-2

    4/18

    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 aretu les of the relation

    Specify range of a tuple variable T as the employeerelation as: Employee (T)

    u , u r w v uTrue. Find all sailors with a rating above 7.

    .

    1/22/2011 4Lecture 2

  • 7/24/2019 4 - Relational Calculus-2

    5/18

    Al ebra 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) }

    RX 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 5

  • 7/24/2019 4 - Relational Calculus-2

    6/18

    TRC Examples

    Find all sailors with a rating above 7.

    }7.|{ >

    orratingSSailorsSS

    }7.)(|{ > ratingSSSailorsS

    r u r r u u r . wras:

    ..

    1/22/2011 6Lecture 2

  • 7/24/2019 4 - Relational Calculus-2

    7/18

    Tuple Relational Calculus

    Can use two quantifiers to tell how many instancesthe predicate applies to: x s en a uan er ere ex s s

    Universal Quantifier (for all)

    Tuple variables qualified by or are called bound

    variables, otherwise called freevariables

    1/22/2011 7Lecture 2

  • 7/24/2019 4 - Relational Calculus-2

    8/18

    Tuple Relational Calculus

    Existential quantifier used in formulae that must betrue for at least one instance, such as:

    {T/Staff(T) (B) (Branch(B) (B.branchNO=S.branchNo) B.city = Rourkela)}

    branchNo as the branchNo of the current Staff tuple,T, and is located in Rourkela.

    1/22/2011 8Lecture 2

  • 7/24/2019 4 - Relational Calculus-2

    9/18

    Tuple Relational Calculus

    Universal quantifier used in statements about every,

    (B) (B.city Delhi) Means For all Branch tuples, the address is not in

    .

    One can also use ~ B B.cit = Delhi whichmeans there are no branches with an address inDelhi

    1/22/2011 9Lecture 2

  • 7/24/2019 4 - Relational Calculus-2

    10/18

    Tuple Relational Calculus

    Formulae should be unambiguous and make sense

    A (well-formed) formula is made out of atoms R(S

    i

    ), 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: 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 10Lecture 2

  • 7/24/2019 4 - Relational Calculus-2

    11/18

    Example Tuple Relational Calculus

    Find the names and ages of sailors with a rating above 7

    )}....7.(|{ ageSagePnameSnamePratingSSailorsSP ==>

    BoatsBai orsP

    ......Re snameSsnamePbidBbidRsidRsidSservesR ===

    1/22/2011 11Lecture 2

  • 7/24/2019 4 - Relational Calculus-2

    12/18

    uple Relational Calculus Expressions can generate an infinite set.It is possible

    have an infinite number of answers! Such queriesare called unsafe.

    { S/~Staff(S) }

    ,must be values in the domain of the expression.

    1/22/2011 12Lecture 2

  • 7/24/2019 4 - Relational Calculus-2

    13/18

    Uses variables that make values from domains

    .

    If F d d d stands for a formula com osed of , ,atoms and d1,d2,,dn represent domain variables,

    then:1, 2,, n 1, 2,, n r

    relational calculus expression

    1/22/2011 Lecture 2 13

  • 7/24/2019 4 - Relational Calculus-2

    14/18

    Domain Relational Calculus Find all sailors with a rating above 7

    {/ < I,N,T,A > Sailors T > 7 } he condition < I,N,T,A > Sailors ensures that the

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

    Modify this query to answer:

    rating under 9, and are called Joe.

    1/22/2011 Lecture 2 14

  • 7/24/2019 4 - Relational Calculus-2

    15/18

    Domain Relational Calculus Find sailors rated > 7 whove reserved boat #103

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

    7

    == 103Re,,,, BrIIrservesDBrIrDBrIr

    1/22/2011 Lecture 2 15

  • 7/24/2019 4 - Relational Calculus-2

    16/18

    Find sailors rated > 7 whove reserved a redboat

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

    > 7

    =

    = =

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

    Observe how the parentheses control the

    scope o eac quan er s n ng.

    1/22/2011 16Lecture 2

  • 7/24/2019 4 - Relational Calculus-2

    17/18

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

    B BN C Boats, ,

    = =

    Ir Br D serves I Ir Br B, , Re

    1/22/2011 17Lecture 2

  • 7/24/2019 4 - Relational Calculus-2

    18/18

    Summary

    Relational calculus is non-operational,and users define ueries in terms ofwhat they want, not in terms of how to

    com ute it. Declarativeness. Algebra and safe calculus have same

    ,of relational completeness.

    1/22/2011 18Lecture 2