Tuesday, December 4, 2012

Database Management System Chapter-3

 

 

Chapter 3:  Relational Model

 

n Structure of Relational Databases

n Relational Algebra

n Tuple Relational Calculus

n Domain Relational Calculus

n Extended Relational-Algebra-Operations

n Modification of the Database

n Views

Example of a Relation

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Basic Structure

 

n Formally, given sets D1, D2, …. Dn a relation r is a subset of
D1D2  x … x Dn
Thus a relation is a set of n-tuples (a1, a2, …, an) where
each ai 
Î Di

n Example:  if

            customer-name = {Jones, Smith, Curry, Lindsay}
        customer-street = {Main, North, Park}
        customer-city     = {Harrison, Rye, Pittsfield}
Then r = {   (Jones, Main, Harrison),
                   (Smith, North, Rye),
                   (Curry, North, Rye),
                   (Lindsay, Park, Pittsfield)}
is a relation over customer-name x customer-street x customer-city

Attribute Types

 

n Each attribute of a relation has a name

n The set of allowed values for each attribute is called the domain of the attribute

n Attribute values are (normally) required to be atomic, that is, indivisible

H E.g. multivalued attribute values are not atomic

H E.g. composite attribute values are not atomic

n The special value null  is a member of every domain

n The null value causes complications in the definition of many operations

H  we shall ignore the effect of null values in our main presentation and consider their effect later

 

Relation Schema

 

n A1, A2, …, An are attributes

n R = (A1, A2, …, An ) is a relation schema

            E.g.   Customer-schema =
                     (customer-name, customer-street, customer-city)

n r(R) is a relation on the relation schema R

            E.g.     customer (Customer-schema)

 

Relation Instance

 

n The current values (relation instance) of a relation are specified by a table

n An element t of r is a tuple, represented by a row in a table

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Relations are Unordered

 

nOrder of tuples is irrelevant (tuples may be stored in an arbitrary order)

n E.g. account relation with unordered tuples

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Database

 

n A database consists of multiple relations

n Information about an enterprise is broken up into parts, with each relation storing one part of the information

        E.g.:   account :    stores information about accounts
                   depositor : stores information about which customer
                                     owns which account
                   customer : stores information about customers

n Storing all information as a single relation such as
   bank(account-number, balance, customer-name, ..)
results in

H repetition of information (e.g. two customers own an account)

H the need for null values  (e.g. represent a customer without an account)

n Normalization theory (Chapter 7) deals with how to design relational schemas

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The customer Relation

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


The depositor Relation

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


E-R Diagram for the Banking Enterprise

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Keys

 

n Let K Í R

n K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R)

H by “possible r” we mean a relation r that could exist in the enterprise we are modeling.

H Example:  {customer-name, customer-street} and
                 {customer-name}
are both superkeys of Customer, if no two customers can possibly have the same name.

n K is a candidate key if K is minimal
Example:  {customer-name} is a candidate key for Customer, since it is a superkey (assuming no two customers can possibly have the same name), and no subset of it is a superkey.

Determining Keys from E-R Sets

 

n Strong entity set.  The primary key of the entity set becomes the primary key of the relation.

n Weak entity set.  The primary key of the relation consists of the union of the primary key of the strong entity set and the discriminator of the weak entity set.

n Relationship set.  The union of the primary keys of the related    entity sets becomes a super key of the relation.

H For binary many-to-one relationship sets, the primary key of the “many” entity set becomes the relation’s primary key.

H For one-to-one relationship sets, the relation’s primary key can be that of either entity set.

H For many-to-many relationship sets, the union of the primary keys becomes the relation’s primary key

 

Schema Diagram for the Banking Enterprise

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Query Languages

 

n Language in which user requests information from the database.

n Categories of languages

H procedural

H non-procedural

n “Pure” languages:

H Relational Algebra

H Tuple Relational Calculus

H Domain Relational Calculus

n Pure languages form underlying basis of query languages that people use.

 

 

 

Relational Algebra

n Procedural language

n Six basic operators

H select

H project

H union

H set difference

H Cartesian product

H rename

n The operators take two or more relations as inputs and give a new relation as a result.

Select Operation – Example

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    Relation r

 

    sA=B ^ D > 5 (r)

 
 


    

 

 

 

 

Select Operation

 

n Notation:  s p(r)

n p is called the selection predicate

n Defined as:

                        sp(r) = {t | t Î r and p(t)}

     Where p is a formula in propositional calculus consisting of terms connected by : Ù (and), Ú (or), Ø (not)
Each
term is one of:

            <attribute>     op        <attribute> or <constant>

     where op is one of:  =, ¹, >, ³. <. £

n Example of selection:
 
s branch-name=“Perryridge(account)

 

Project Operation – Example

=

 
 

 

 

 

 


Relation r:

 

 

 

 

 

 

 

 

 

 

 

 

 

ÕA,C (r)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Project Operation

n Notation:

       
ÕA1, A2, …, Ak (r)

     where A1, A2 are attribute names and r is a relation name.

n The result is defined as the relation of k columns obtained by erasing the columns that are not listed

n Duplicate rows removed from result, since relations are sets

n E.g. To eliminate the branch-name attribute of account
         
Õaccount-number, balance (account)

Union Operation – Example

s

 
 

 

 

 

 


Relations r, s:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

     r È s:

 

 

 

 

 

 

 

 

 

 

 

Union Operation

n Notation:  r È s

n Defined as:

            r  È s = {t | t Î r or t Î s}

 

n For r È s to be valid.

     1.  r, s must have the same arity (same number of attributes)

     2.  The attribute domains must be compatible (e.g., 2nd column
        of r deals with the same type of values as does the 2nd
     column of s)

n E.g. to find all customers with either an account or a loan
   
Õcustomer-name (depositor)   È Õcustomer-name (borrower)

Set Difference Operation – Example

s

 
 

 

 

 


Relations r, s:

 

 

 

 

 

 

 

 

 

 

 

 

 

r s:

 

 

 

 

 

 

 

 

 

 

 

Set Difference Operation

n Notation r – s

n Defined as:

            r – s  = {t | t Î r and t Ï s}   

n Set differences must be taken between compatible relations.

H r and s must have the same arity

H attribute domains of r and s must be compatible

 

Cartesian-Product Operation-Example

 

r

 

s

 

 

 


Relations r, s:

 

 

 

 

 

 

 

 

 

 

r x s:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Cartesian-Product Operation

n Notation r x s

n Defined as:

            r x s = {t q | t Î r and q Î s}

n Assume that attributes of r(R) and s(S) are disjoint.  (That is,
R
Ç S = Æ).

n If attributes of r(R) and s(S) are not disjoint, then renaming must be used.

Composition of Operations

n Can build expressions using multiple operations

n Example:  sA=C(r x s)

n r x s           

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 


sA=C(r x s)

 

 

 

 

 

 

 

 

 

Rename Operation

n Allows us to name, and therefore to refer to, the results of relational-algebra expressions.

n Allows us to refer to a relation by more than one name.

Example:

                                   r x (E)

returns the expression E under the name X

If a relational-algebra expression E has arity n, then

                                          rx (A1, A2, …, An) (E)

returns the result of expression E under the name X, and with the

attributes renamed to A1, A2, …., An.

 

Banking Example

branch (branch-name, branch-city, assets)

customer (customer-name, customer-street, customer-only)

 

account (account-number, branch-name, balance)

 

loan (loan-number, branch-name, amount)

 

depositor (customer-name, account-number)

 

borrower (customer-name, loan-number)

Example Queries

n  Find all loans of over $1200

 

samount > 1200 (loan)

 

 

nFind the loan number for each loan of an amount greater than                            

    $1200

 

 

Õloan-number (samount > 1200 (loan))

 

 

                      

Example Queries

n Find the names of all customers who have a loan, an account, or both, from the bank

 

Õcustomer-name (borrower) È Õcustomer-name (depositor)

 

nFind the names of all customers who have a loan and an

   account at bank.

Õcustomer-name (borrower) Ç Õcustomer-name (depositor)

 

 

 

 

 

 

 

 

 

 

Example Queries

n Find the names of all customers who have a loan at the Perryridge branch.

 

Õcustomer-name (sbranch-name=“Perryridge

    (sborrower.loan-number = loan.loan-number(borrower x loan)))

 

nFind the names of all customers who have a loan at the
    Perryridge branch but do not have an account at any branch of  
    the bank.

 

 

Õcustomer-name (sbranch-name = “Perryridge”

    (sborrower.loan-number = loan.loan-number(borrower x loan)))  –          
     
Õcustomer-name(depositor)

 

 

 

 

 

 

 

Example Queries

n Find the names of all customers who have a loan at the Perryridge branch.

-Query 1
 
Õcustomer-name(sbranch-name = “Perryridge” (
 
sborrower.loan-number = loan.loan-number(borrower x loan)))

 

 

 

 

 

Query 2

     Õcustomer-name(sloan.loan-number = borrower.loan-number(
             (
sbranch-name = “Perryridge”(loan)) x  borrower))

 

 

 

 

 

 

Example Queries

Find the largest account balance

n Rename account relation as d

n The query is:

Õbalance(account) - Õaccount.balance

    (saccount.balance < d.balance (account x rd (account)))

 

 

 

 

 

 

 

 

 

 

    

Formal Definition

n A basic expression in the relational algebra consists of either one of the following:

H A relation in the database

H A constant relation

n Let E1 and E2 be relational-algebra expressions; the following are all relational-algebra expressions:

H E1 È E2

H E1 - E2

H E1 x E2

H sp (E1), P is a predicate on attributes in E1

H Õs(E1), S is a list consisting of some of the attributes in E1

H   r x (E1), x is the new name for the result of E1

Additional Operations

We define additional operations that do not add any power to the

relational algebra, but that simplify common queries.

n Set intersection

n Natural join

n Division

n Assignment

Set-Intersection Operation

n Notation: r Ç s

n Defined as:

n r Ç s ={ t | t Î r and t Î s }

n Assume:

H r, s have the same arity

H attributes of r and s are compatible

n Note: r Ç s = r - (r - s)

 

 

 

 

 

 

 

 

 

 

 

Set-Intersection Operation - Example

n Relation r, s:

 

 

 

 

 

 

 

 

 

r

 
 

 

 

 

 


n r Ç s

 

 

 

 

 

 

 

 

 

 

 


Natural-Join Operation

n Let r and s be relations on schemas R and S respectively.
Then,  r     s  is a relation on schema R
È S obtained as follows:

H Consider each pair of tuples tr from r and ts from s

H If tr and ts have the same value on each of the attributes in R Ç S, add a tuple t  to the result, where

4 t has the same value as tr on r

4 t has the same value as ts on s

n Example:

R = (A, B, C, D)

S = (E, B, D)

H Result schema = (A, B, C, D, E)

H r     s is defined as:
     
Õr.A, r.B, r.C, r.D, s.E (sr.B = s.B Ù r.D = s.D (r  x  s))

 

 

 

 

 

 

 

 

Natural Join Operation – Example

n Relations r, s:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Division Operation

n Suited to queries that include the phrase “for all”.

n Let r and s be relations on schemas R and S respectively where

H R = (A1, …, Am, B1, …, Bn)

H S = (B1, …, Bn)

The result of  r ¸ s is a relation on schema

RS = (A1, …, Am)

 

      r ¸ s = { t  |  t Î Õ R-S(r) Ù " u Î s ( tu Î r ) }

Division Operation – Example

 

 

 

 

            Relations r, s:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

B

 

1

2

 

s

 

A

 

a

b

 
 

 


r ¸ s:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Another Division Example

 

Relations r, s:

 

r ¸ s:

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Division Operation (Cont.)

n Property

H Let q – r  ¸ s

H Then q is the largest relation satisfying q x s Í r

n Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S  
Í R

 

     r ¸ s = ÕR-S (r) –ÕR-S ( (ÕR-S (r) x s) – ÕR-S,S(r))

     To see why

H ÕR-S,S(r) simply reorders attributes of r

H ÕR-S(ÕR-S (r) x s) – ÕR-S,S(r)) gives those tuples t in

 
ÕR-S (r) such that for some tuple u Î s, tu Ï r.

 

Assignment Operation

n  The assignment operation (¬) provides a convenient way to express complex queries.

H  Write query as a sequential program consisting of

4 a series of assignments

4 followed by an expression whose value is displayed as a result of the query.

H Assignment must always be made to a temporary relation variable.

n  Example:  Write r ¸ s as

                        temp1 ¬ ÕR-S (r)
                    temp2
¬ ÕR-S  ((temp1 x s) – ÕR-S,S (r))
                    result = temp1 – temp2

H The result to the right of the ¬ is assigned to the relation variable on the left of the ¬.

H May use variable in subsequent expressions.

Example Queries

n Find all customers who have an account from at least the “Downtown” and the Uptown” branches.

 

Query 1

ÕCN(sBN=“Downtown(depositor    account)) Ç

        ÕCN(sBN=“Uptown(depositor    account))

 

 

 

where CN denotes customer-name and BN denotes
branch-name.

 

 

 

Query 2

            Õcustomer-name, branch-name (depositor     account)
                   
¸ rtemp(branch-name) ({(“Downtown”), (“Uptown”)})

 

 

Example Queries

n Find all customers who have an account at all branches located in Brooklyn city.

 

Õcustomer-name, branch-name (depositor     account)
           
¸ Õbranch-name (sbranch-city = “Brooklyn” (branch))

 

 

 

 

 

 

 

 

Extended Relational-Algebra-Operations

n Generalized Projection

n Outer Join

n Aggregate Functions

Generalized Projection

n Extends the projection operation by allowing arithmetic functions to be used in the projection list.

       
Õ F1, F2, …, Fn(E)

n E is any relational-algebra expression

n Each of F1, F2, …, Fn  are are arithmetic expressions involving constants and attributes in the schema of E.

n Given relation credit-info(customer-name, limit, credit-balance), find how much more each person can spend:

            Õcustomer-name, limit – credit-balance (credit-info)

Aggregate Functions and Operations

n Aggregation function takes a collection of values and returns a single value as a result.

            avg:  average value
        min:  minimum value
        max:  maximum value
        sum:  sum of values
        count:  number of values

n Aggregate operation in relational algebra

                            G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E)

H E is any relational-algebra expression

H G1, G2 …, Gn is a list of attributes on which to group (can be empty)

H Each Fi is an aggregate function

H Each Ai is an attribute name

 

 

 

 

 

 

 

 

 

 

 

 

 

Aggregate Operation – Example

 

 

n  Relation r:

 

 

sum-C

 

g sum(c) (r)

 

27

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Aggregate Operation – Example

n Relation account grouped by branch-name:

 

 

 

 

 

 

 

 

 

 

 

 

 


branch-name g sum(balance) (account)

 

 

 

 

 

 

 

 

 

 

 

 


n Result of aggregation does not have a name

H Can use rename operation to give it a name

H For convenience, we permit renaming as part of aggregate operation

 

Outer Join

n An extension of the join operation that avoids loss of information.

n Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result of the join.

n Uses null values:

H null signifies that the value is unknown or does not exist

H All comparisons involving null are (roughly speaking) false by definition.

4 Will study precise meaning of comparisons with nulls later

Outer Join – Example

n Relation loan

 

 

 

 

 

 

 

 


nRelation borrower

 

 

 

 

 

 

 

 

 

 


n Inner Join

loan     Borrower

 

 

 

 

 

 

 

 

 


nLeft Outer Join

    loan          Borrower

 

 

 

 

 

 

 

 

 

 

 


n Right Outer Join

       loan          borrower

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Null Values

 

n It is possible for tuples to have a null value, denoted by null, for some of their attributes

n null signifies an unknown value or that a value does not exist.

n The result of any arithmetic expression involving null is null.

n Aggregate functions simply ignore null values

H Is an arbitrary decision.  Could have returned null as result instead.

H We follow the semantics of SQL in its handling of null values

n For duplicate elimination and grouping, null is treated like any other value, and two nulls are assumed to be  the same

H Alternative: assume each null is different from each other

H Both are arbitrary decisions,  so we simply follow SQL

 

Null Values

 

n Comparisons with null values return the special truth value unknown

H If false was used instead of unknown, then    not (A < 5)
               would not be equivalent to               A >= 5

n Three-valued logic using the truth value unknown:

H OR: (unknown or true)         = true,
       (unknown or false)        = unknown
       (unknown or unknown) = unknown

H AND:   (true and unknown)         = unknown,  
           (false
and unknown)        = false,
           (unknown
and unknown) = unknown

H NOT:  (not unknown) = unknown

H In SQL “P is unknown” evaluates to true if predicate P evaluates to unknown

n Result of select  predicate is treated as false if it evaluates to unknown

 

Modification of the Database

 

n The content of the database may be modified using the following operations:

H Deletion

H Insertion

H Updating

n All these operations are expressed using the assignment operator.

 

Deletion

 

n A delete request is expressed similarly to a query, except instead of displaying tuples to the user, the selected tuples are removed from the database.

n Can delete only whole tuples; cannot delete values on only particular attributes

n A deletion is expressed in relational algebra by:

            r ¬ rE

     where r is a relation and E is a relational algebra query.

 

Deletion Examples

 

n Delete all account records in the Perryridge branch.

 

account ¬ account s branch-name = “Perryridge” (account)

 

 

 

 

nDelete all loan records with amount in the range of 0 to 50

 

 

 

loan ¬ loans amount ³ 0 and amount £ 50 (loan)

 

 

 

nDelete all accounts at branches located in Needham.

 

 

r1 ¬ s branch-city = “Needham” (account      branch)

r2 ¬ Õbranch-name, account-number, balance (r1)

r3 ¬ Õ customer-name, account-number (r2     depositor)

account ¬ account – r2

depositor ¬ depositor – r3

 

 

 

 

Insertion

 

n To insert data into a relation, we either:

H specify a tuple to be inserted

H write a query whose result is a set of tuples to be inserted

n in relational algebra, an insertion is expressed by:

            r ¬  r  È  E

     where r is a relation and E is a relational algebra expression.

n The insertion of a single tuple is expressed by letting E be a constant relation containing one tuple.

 

Insertion Examples

 

n Insert information in the database specifying that Smith has $1200 in account A-973 at the Perryridge branch.

account ¬  account  È {(“Perryridge”, A-973, 1200)}

depositor ¬  depositor  È {(“Smith”, A-973)}

 

 

 

nProvide as a gift for all loan customers in the Perryridge
     branch, a $200 savings account.  Let the loan number serve
     as the account number for the new savings account.

 

 

 

r1 ¬ (sbranch-name = “Perryridge” (borrower    loan))

account ¬ account È Õbranch-name, account-number,200 (r1)

depositor ¬ depositor È Õcustomer-name, loan-number(r1)

 

 

 

 

Updating

 

n A mechanism to change a value in a tuple without charging all values in the tuple

n Use the generalized projection operator to do this task

            r ¬ Õ F1, F2, …, FI, (r)

n Each Fi is either

H the ith attribute of r, if the ith attribute is not updated, or,

H if the attribute is to be updated Fi  is an expression, involving only constants and the attributes of r, which gives the new value for the attribute

 

Update Examples

 

n Make interest payments by increasing all balances by 5 percent.

 

account ¬ Õ AN, BN, BAL * 1.05 (account)

 

 

where AN, BN and BAL stand for account-number, branch-name and balance, respectively.

 

 

 

nPay all accounts with balances over $10,000 6 percent interest
     and pay all others 5 percent

 

 

 

account ¬     Õ AN, BN, BAL * 1.06 (s BAL > 10000 (account))
                      
È  ÕAN, BN, BAL * 1.05 (sBAL £ 10000 (account))

 

 

 

 

 

Views

 

n In some cases, it is not desirable for all users to see the entire logical model (i.e., all the actual relations stored in the database.)

n Consider a person who needs to know a customer’s loan number but has no need to see the loan amount.  This person should see a relation described, in the relational algebra, by

            Õcustomer-name, loan-number (borrower    loan)

n Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view.

 

View Definition

 

n A view is defined using the create view statement which has the form

 

            create view v as <query expression

 

     where <query expression> is any legal relational algebra query expression.  The view name is represented by v.

n Once a view is defined, the view name can be used to refer to the virtual relation that the view generates.

n View definition is not the same as creating a new relation by evaluating the query expression 

H Rather, a view definition causes the saving of an expression; the expression is substituted into queries using the view.

 

 

View Examples

 

n Consider the view (named all-customer) consisting of branches and their customers.

 

create view all-customer as

  Õbranch-name, customer-name (depositor    account)

           È Õbranch-name, customer-name (borrower    loan)

 

 

 

nWe can find all customers of the Perryridge branch by writing:

 

 

 

 

Õbranch-name

            (sbranch-name = Perryridge (all-customer))    

 

 

 

 

 

Updates Through View

 

n Database modifications expressed as views must be translated to modifications of the actual relations in the database.

n Consider the person who needs to see all loan data in the loan relation except amount.  The view given to the person, branch-loan, is defined as:

                        create view branch-loan as

                                    Õbranch-name, loan-number (loan)

n Since we allow a view name to appear wherever a relation name is allowed, the person may write:

 

            branch-loan ¬ branch-loan È {(“Perryridge”, L-37)}

    

n The previous insertion must be represented by an insertion into the actual relation loan from which the view branch-loan is constructed.

n An insertion into loan requires a value for amount. The insertion can be dealt with by either.

H rejecting the insertion and returning an error message to the user.

H inserting a tuple (“L-37”, “Perryridge”, null) into the loan relation

n Some updates through views are impossible to translate into database relation updates

H create view v as sbranch-name = “Perryridge” (account))

     v ¬ v È (L-99, Downtown, 23)

n Others cannot be translated uniquely

H all-customer ¬ all-customer  È {(“Perryridge”, “John”)}

4 Have to choose loan or account, and
create a new loan/account number!

 

 

Views Defined Using Other Views

 

n One view may be used in the expression defining another view

n A view relation v1 is said to depend directly on a view relation v2  if v2 is used in the expression defining v1

n A view relation v1 is said to depend on view relation v2 if either v1 depends directly to v2  or there is a path of dependencies from v1 to v2

n A view relation v is said to be recursive  if it depends on itself.

 

View Expansion

 

n A way to define the meaning of views defined in terms of other views.

n Let view v1 be defined by an expression e1 that may itself contain uses of view relations.

n View expansion of an expression repeats the following replacement step:

            repeat
                   
Find any view relation v
i in e1
                    Replace the view relation v
i by the expression defining vi
        until no more view relations are present in e
1

n As long as the view definitions are not recursive, this loop will terminate

 

Tuple Relational Calculus

 

n A nonprocedural query language, where each query is of the form

            {t | P (t) }

n It is the set of all tuples t such that predicate P is true for t

n t is a tuple variable, t[A] denotes the value of tuple t on attribute A

n t Î r denotes that tuple t is in relation r

n P is a formula similar to that of the predicate calculus

 

Predicate Calculus Formula

 

1. Set of attributes and constants

2. Set of comparison operators:  (e.g., <, £, =, ¹, >, ³)

3. Set of connectives:  and (Ù), or (v)‚ not (Ø)

4. Implication (Þ): x Þ y, if x if true, then y is true

                                    x Þ y º Øx v y

5. Set of quantifiers:

$ t Î r (Q(t)) º ”there exists” a tuple in t in relation r
                        such that predicate Q(t) is true

"t Î r (Q(t)) º Q is true “for all” tuples t in relation r

 

 

 

Banking Example

 

n branch (branch-name, branch-city, assets)

n customer (customer-name, customer-street, customer-city)

n account (account-number, branch-name, balance)

n loan (loan-number, branch-name, amount)

n depositor (customer-name, account-number)

n borrower (customer-name, loan-number)

 

Example Queries

 

n Find the loan-number, branch-name, and  amount for loans of over $1200

 

{t | t Î loan Ù t [amount] > 1200}

 

 

 

nFind the loan number for each loan of an amount greater than $1200

 

 

 

{t | $ s Î loan (t[loan-number] = s[loan-number] Ù s [amount] > 1200)}

 

 

 

Notice that a relation on schema [loan-number] is implicitly defined by the query

 

 

 

Example Queries

 

n Find the names of all customers having a loan, an account, or both at the bank

 

{t | $s Î borrower( t[customer-name] = s[customer-name])
    
Ú $u Î depositor( t[customer-name] = u[customer-name])

 

 

 

nFind the names of all customers who have a loan and an account
      at the bank

 

 

 

{t | $s Î borrower( t[customer-name] = s[customer-name])
     
Ù $u Î depositor( t[customer-name] = u[customer-name])

 

 

Example Queries

 

n Find the names of all customers having a loan at the Perryridge branch

 

{t | $s Î borrower(t[customer-name] = s[customer-name]
     
Ù $u Î loan(u[branch-name] = “Perryridge”
                        
Ù  u[loan-number] = s[loan-number]))}

 

 

 

 

nFind the names of all customers who have a loan at the
     Perryridge branch, but no account at any branch of the bank

 

{t | $s Î borrower( t[customer-name] = s[customer-name]
      
Ù $u Î loan(u[branch-name] = “Perryridge”
                          
Ù  u[loan-number] = s[loan-number]))
      
Ù not $v Î depositor (v[customer-name] =
                                                      t[customer-name]) }

 

 

 

Example Queries

 

n Find the names of all customers having a loan from the Perryridge branch, and the cities they live in

 

{t | $s Î loan(s[branch-name] = “Perryridge”
       
Ù $u Î borrower (u[loan-number] = s[loan-number]
               
Ù  t [customer-name] = u[customer-name])
           
Ù $ v Î customer (u[customer-name] = v[customer-name]
                                            
Ù  t[customer-city] = v[customer-city])))}

 

 

 

Example Queries

 

n Find the names of all customers who have an account at all branches located in Brooklyn:

 

{t | $ c Î customer (t[customer.name] = c[customer-name]) Ù

                  " s Î branch(s[branch-city] = Brooklyn Þ
           
$ u Î account ( s[branch-name] = u[branch-name]
          
Ù $ s Î depositor (  t[customer-name] = s[customer-name]
                            
Ù  s[account-number] = u[account-number] )) )}

 

 

 

 

 

 

 

Safety of Expressions

 

n It is possible to write tuple calculus expressions that generate infinite relations.

n For example, {t | Ø t Î r} results in an infinite relation if the domain of any attribute of relation r is infinite

n To guard against the problem, we restrict the set of allowable expressions to safe expressions.

n An expression {t | P(t)} in the tuple relational calculus is safe if every component of t appears in one of the relations, tuples, or constants that appear in P

H NOTE: this is more than just a syntax condition.

4 E.g. { t | t[A]=5 Ú true } is not safe --- it defines an infinite set with attribute values that do not appear in any relation or tuples or constants in P.

 

Domain Relational Calculus

 

n A nonprocedural query language equivalent in power to the tuple relational calculus

n Each query is an expression of the form:

 

                        { < x1, x2, …, xn > | P(x1, x2, …, xn)}

H x1, x2, …, xn  represent domain variables

H P represents a formula similar to that of the predicate calculus

 

Example Queries

 

n Find the loan-number, branch-name, and  amount for loans of over $1200

{< l, b, a > | < l, b, a > Î loan Ù a > 1200}

 

 

 

 

nFind the names of all customers who have a loan of over $1200

 

 

 

{< c > | $ l, b, a (< c, l > Î borrower Ù < l, b, a > Î loan Ù a > 1200)}

 

 

 

nFind the names of all customers who have a loan from the
       Perryridge branch and the loan amount:

 

 

 

 

 

{< c, a > | $ l (< c, l > Î borrower Ù $b(< l, b, a > Î loan Ù

                                                                   b = “Perryridge”))}

  or {< c, a > | $ l (< c, l > Î borrower Ù < l, “Perryridge, a > Î loan)}

 

 

Example Queries

 

n Find the names of all customers having a loan, an account, or both at the Perryridge branch:

 

{< c > | $ l ({< c, l > Î borrower
                 
Ù $ b,a(< l, b, a > Î loan Ù b = “Perryridge”))
      
Ú $ a(< c, a > Î depositor
                
Ù $ b,n(< a, b, n > Î account Ù b = “Perryridge”))}

 

 

 

nFind the names of all customers who have an account at all
     branches located in Brooklyn:

 

 

 

 

 

{< c > | $ s, n (< c, s, n > Î customer) Ù

            " x,y,z(< x, y, z > Î branch Ù y = Brooklyn) Þ
                
$ a,b(< x, y, z > Î account Ù < c,a > Î depositor)}

 

 

 

 

 

 

Safety of Expressions

 

                        { < x1, x2, …, xn > | P(x1, x2, …, xn)}

is safe if all of the following hold:

     1.    All values that appear in tuples of the expression are values              from dom(P) (that is, the values appear either in P or in a tuple             of a relation mentioned in P).

    2.    For every “there exists” subformula of the form $ x (P1(x)), the            subformula is true if and only if there is a value of x in dom(P1)         such that P1(x) is true.

     3. For every “for all” subformula of the form "x (P1 (x)), the                           subformula is true if and only if P1(x) is true for all values x                   from dom (P1).

End of Chapter 3

 

0 comments:

Post a Comment