11266 Ch6 the Relational Calculus

Embed Size (px)

Citation preview

  • 8/13/2019 11266 Ch6 the Relational Calculus

    1/13

    The Relational Calculus

  • 8/13/2019 11266 Ch6 the Relational Calculus

    2/13

    The Relational Calculus

    The Relational Calculus

    High-level, non-procedural language

    Relational calculus expression declares the requirementstuples in the resulting relation must satisfy.

    Provides foundation for standard query language (SQL)

    Expressive power

    Any expression in relational algebra can be expressed inrelational calculus.

    The tuple relational calculus

    The domain relational calculus

  • 8/13/2019 11266 Ch6 the Relational Calculus

    3/13

    The Tuple Relational Calculus

    A tuple variable

    Ranges over a database relation, denoted R(t)

    Reference to an attribute of a tuple is denoted using the. notation

    Tuple relational expression

    Form: {t | COND(t)}, where COND(t) is a conditional

    expression involving t

  • 8/13/2019 11266 Ch6 the Relational Calculus

    4/13

    The Tuple Relational Calculus

    Example:

    {t.NAME | EMPLOYEE(t) ANDt.SALARY>50000}

    The range relation of t is EMPLOYEE The select condition that must evaluate to true:

    SALARY>50000

    The requested attribute: NAME

  • 8/13/2019 11266 Ch6 the Relational Calculus

    5/13

    Expressions and Formulas

    A general expression

    {t1.Aj, t2.Ak,, tn.Ap| COND(t1, t2,, tn, tn+1,, tn+m)}

    t1, t2,, tn, tn+1,, tn+m: tuple variables ti.Aj: attribute Ajof the relation on which tiranges

    COND is a condition or formula made of predicate

    calculus atoms

  • 8/13/2019 11266 Ch6 the Relational Calculus

    6/13

    Expressions and Formulae

    Atom

    R(t): R is a relation name and t is a tuple variable

    ti.A opt

    j.B: A and B are attributes of the relations on

    which tiand tjrange, respectively; op{=, , , }

    ti.A opc: c is a constant value in the domain of A

    Formula

    An atom is a formula.

    If F is a formula, then so is NOT(F).

    If F1and F2are formulas, then so are (F1ANDF2), (F1ORF2), and (IF F1THENF2).

  • 8/13/2019 11266 Ch6 the Relational Calculus

    7/13

    Quantifiers

    Existential quantifier If F is a formula, then so is (t)(F), where t is a tuple

    variable.

    (t)(F) is true if there exists some tuple that makes F

    true.

    read as "there exists an x such that...".

    Universal quantifier

    If F is a formula, then so is (

    t)(F), where t is a tuplevariable.

    (t)(F) is true if F is true for every substitution for t.

    read as "for all x...".

  • 8/13/2019 11266 Ch6 the Relational Calculus

    8/13

    Other Notations

    R(t) tR

    NOTF F

    F1ANDF2 F1

    F2F1ORF2 F1F2

    IFF1THENF2 F1F2

  • 8/13/2019 11266 Ch6 the Relational Calculus

    9/13

    Examples

    {t.FNAME,t.LNAME | EMPLOYEE(t) and t.SALARY >50000 }

    is equivalent to

    SELECT T.FNAME, T.LNAME

    FROM EMPLOYEE TWHERE T.SALARY > 50000

  • 8/13/2019 11266 Ch6 the Relational Calculus

    10/13

    Find all the tuples in student relation.

    {t|t student}

    Find those tuples for which age is greater

    than 18.

    {t|t student [age]>18 }

    Find the roll numbers of all the students.

    {t| s student (t[rollno]=s{rollno})}

  • 8/13/2019 11266 Ch6 the Relational Calculus

    11/13

    The Domain Relational Calculus

    Variables

    Range over single values from domains of attributes

    Domain relational expression {x1, x2,,xn| COND(x1, x2,,xn, xn+1, xn+2,,xn+m)}

    x1, x2,,xn, xn+1, xn+2,,xn+mare domain variables

    COND is a condition or formula of the domain

    relational calculus,

  • 8/13/2019 11266 Ch6 the Relational Calculus

    12/13

    The Domain Relational Calculus

    Atom

    R(x1, x2,,xj): R is a relation of degree j and xiis adomain variable.

    xiopxj: xiand xjare domain variables and op{=, , , }

    xiopc: c is a constant value in the domain of xi

    Formula

    Made of atoms, variables, and quantifiers

  • 8/13/2019 11266 Ch6 the Relational Calculus

    13/13

    Examples

    Find the loan-no, branch-name and amount for loans of over

    20000.

    {| loan a>2000}

    Find all the loan numbers for loans with amount greater than

    20000.

    {| b,a( loan a >20000)}