DBMS - Ch2

download DBMS - Ch2

of 96

  • date post

    12-Oct-2015
  • Category

    Documents

  • view

    6
  • download

    0

Embed Size (px)

description

DBMS - Ch2

Transcript of DBMS - Ch2

  • 5/21/2018 DBMS - Ch2

    1/96

    Chapter 2: Relational Model

  • 5/21/2018 DBMS - Ch2

    2/96

    Chapter 2: Relational Model Structure of Relational Databases

    Fundamental Relational-Algebra-Operations

    Additional Relational-Algebra-Operations

    Etended Relational-Algebra-Operations

    !ull "alues Modi#cation of the Database

  • 5/21/2018 DBMS - Ch2

    3/96

    Eample of a Relation

  • 5/21/2018 DBMS - Ch2

    4/96

    Attribute $%pes Each attribute of a relation has a name

    $he set of allo&ed 'alues for each attribute is called the domainof the attribute

    Attribute 'alues are (normall%) re*uired to be atomic+ that is,indi'isible

    Eg the 'alue of an attribute can be an account number,but cannot be a set of account numbers

    Domain is said to be atomic if all its members are atomic

    $he special 'alue null is a member of e'er% domain

    $he null 'alue causes complications in the de#nition of man%operations

    .e shall ignore the e/ect of null 'alues in our mainpresentation and consider their e/ect later

  • 5/21/2018 DBMS - Ch2

    5/96

    Relation Schema Formall%, gi'en domains D0, D2, 1 Dna relationris a subset of

    D0 D2 1 Dn

    $hus, a relation is a set of n-tuples (a0,a2, 1, an) &here each ai

    Di

    Schema of a relation consists of

    attribute de#nitions

    name

    t%pedomain

    integrit% constraints

  • 5/21/2018 DBMS - Ch2

    6/96

    Relation 3nstance $he current 'alues (relation instance) of a relation are speci#ed

    b% a table

    An element tof ris a tuple, represented b% a row in a table

    Order of tuples is irrele'ant (tuples ma% be stored in anarbitrar% order)

    JonesSmithCurryLindsay

    customer_name

    MainNorthNorthPark

    customer_street

    HarrisonRyeRyePittsfield

    customer_city

    customer

    attributes

    (or columns)

    tuples(or rows)

  • 5/21/2018 DBMS - Ch2

    7/96

    Database A database consists of multiple relations

    3nformation about an enterprise is bro4en up into parts, &itheach relation storing one part of the information

    Eg

    account : information about accounts

    depositor : &hich customer o&ns &hich account

    customer : information about customers

  • 5/21/2018 DBMS - Ch2

    8/96

    $he customer Relation

  • 5/21/2018 DBMS - Ch2

    9/96

    $he depositor Relation

  • 5/21/2018 DBMS - Ch2

    10/96

    .h% Split 3nformation AcrossRelations5

    Storing all information as a single relation such as

    bank(account_number, balance, customer_name, )

    results in

    repetition of information

    eg,if t&o customers o&n an account (.hat gets repeated5)

    the need for null 'alues

    eg, to represent a customer &ithout an account

    !ormali6ation theor% (Chapter 7) deals &ith ho& to designrelational schemas

  • 5/21/2018 DBMS - Ch2

    11/96

    8e%s 9et 8 R

    K is a superkey of Rif 'alues for Kare sucient to identif% a uni*uetuple of each possible relation r(R)

    b% ;possibler < &e mean a relation rthat could eist in theenterprise &e are modeling

    Eample: =customer_name, customer_street> and

    =customer_name>

    are both super4e%s of Customer, if no t&o customers can possibl%

    ha'e the same name

    3n real life, an attribute such as customer_id&ould be usedinstead of customer_name to uni*uel% identif% customers, but

    &e omit it to 4eep our eamples small, and instead assume

    customer names are uni*ue

  • 5/21/2018 DBMS - Ch2

    12/96

    8e%s (Cont)

    Kis a candidate keyif Kis minimal

    Eample: =customer_name> is a candidate 4e% for Customer,

    since it is a super4e% and no subset of it is a super4e%

    Primary key: a candidate 4e% chosen as the principal means ofidentif%ing tuples &ithin a relation

    Should choose an attribute &hose 'alue ne'er, or 'er% rarel%,

    changes

    Eg email address is uni*ue, but ma% change

  • 5/21/2018 DBMS - Ch2

    13/96

    Foreign 8e%s A relation schema ma% ha'e an attribute that corresponds to the

    primar% 4e% of another relation $he attribute is called a foreignkey

    Eg customer_nameand account_numberattributes of depositorare foreign 4e%s to customerand accountrespecti'el%

    Onl% 'alues occurring in the primar% 4e% attribute of the

    referenced relationma% occur in the foreign 4e% attribute ofthe referencing relation.

  • 5/21/2018 DBMS - Ch2

    14/96

    Schema Diagram

  • 5/21/2018 DBMS - Ch2

    15/96

    ?uer% 9anguages 9anguage in &hich user re*uests information from the database

    Categories of languages

    @rocedural

    !on-procedural, or declarati'e

    ;@ure< languages:

    Relational algebra

    $uple relational calculus

    Domain relational calculus

    @ure languages form underl%ing basis of *uer% languages thatpeople use

  • 5/21/2018 DBMS - Ch2

    16/96

    Relational Algebra @rocedural language

    Si basic operators

    select:

    proect:

    union: set di/erence:

    Cartesian product:

    rename:

    $he operators ta4e one or t&o relations as inputs and producea ne& relation as a result

  • 5/21/2018 DBMS - Ch2

    17/96

    Select Operation B Eample Relation r

    A B C D

    1

    5

    12

    23

    7

    7

    3

    10

    A=B D > 5(r)A B C D

    1

    23

    7

    10

  • 5/21/2018 DBMS - Ch2

    18/96

    @roect Operation B Eample Relationr:

    A B C

    10

    20

    30

    40

    1

    1

    1

    2

    A C

    1

    1

    1

    2

    =

    A C

    1

    1

    2

    A,C(r)

  • 5/21/2018 DBMS - Ch2

    19/96

    nion Operation B Eample Relations r, s:

    rs:

    A B

    1

    2

    1

    A B

    2

    3

    rs

    A B

    1

    2

    1

    3

  • 5/21/2018 DBMS - Ch2

    20/96

    BEample

    Relations r, s:

    r s:

    A B

    1

    2

    1

    A B

    2

    3

    r

    s

    A B

    11

    ar es an ro uc pera on

  • 5/21/2018 DBMS - Ch2

    21/96

    ar es an- ro uc pera on BEample

    Relationsr, s:

    rxs:

    A B

    1

    2

    A B

    111

    12222

    C D

    1010

    20

    101010

    2010

    E

    aab

    baabb

    C D

    1010

    2010

    E

    aabbr

    s

  • 5/21/2018 DBMS - Ch2

    22/96

    Rename Operation Allo&s us to name, and therefore to refer to, the results of

    relational-algebra epressions

    Allo&s us to refer to a relation b% more than one name

    Eample:

    x(E)

    returns the epression Eunder the nameX

    3f a relational-algebra epression Ehas arit% n, then

    returns the result of epression Eunder the nameX, and &ith the

    attributes renamed to! , " , #$, n

    )(),...,,( 21E

    nAAAx

  • 5/21/2018 DBMS - Ch2

    23/96

    Composition of Operations Can build epressions using multiple operations

    Eample: ADC(r x s)

    r x s

    ADC(r x s)

    A B

    111

    12222

    C D

    1010

    20

    101010

    2010

    E

    aab

    baabb

    A B C D E

    122

    1010

    20

    aab

  • 5/21/2018 DBMS - Ch2

    24/96

    an4ing Eamplebranc% (branc%_name, branc%_cit&, assets)

    customer (customer_name, customer_street,customer_cit&)

    account (account_number, branc%_name, balance)

    loan (loan_number, branc%_name, amount)

    depositor (customer_name, account_number)

    borrower(customer_name, loan_number)

  • 5/21/2018 DBMS - Ch2

    25/96

    Eample ?ueries

    Find all loans of o'er 02GG

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

    amount> 1200(loan)

    loan_number(amount> 1200(loan))

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

    customer_name

    (borrower)customer_name

    (depositor)

  • 5/21/2018 DBMS - Ch2

    26/96

    Eample ?ueries Find the names of all customers &ho ha'e a loan at the @err%ridge

    branch

    Find the names of all customers who have a loan at the

    Perryridge branch but do not have an account at any branch ofthe bank.

    customer_name(branch_name = Perryridge

    (borrower.loan_number = loan.loan_number(borrower x loan)))

    customer_name(depositor)

    customer_name(branch_name=Perryridge

    (borrower.loan_number = loan.loan_number(borrower x loan)))

  • 5/21/2018 DBMS - Ch2

    27/96

    Eample ?ueries Find the names of all customers &ho ha'e a loan at the @err%ridge

    branch

    customer_name(loan.loan_number = borrower.loan_number(

    (branch_name = Perryridge(loan)) x borrower))

    customer_name(branch_name = Perryridge(

    borrower.loan_number = loan.loan_number(borrower x loan)))

  • 5/21/2018 DBMS - Ch2

    28/96

    Additional Operations

    Additional Operations

    Set intersection

    !atural oin

    Aggregation

    Outer Hoin

    Di'ision

    All abo'e, other than aggregation, can be epressed using basic

    operations &e ha'e seen earlier

  • 5/21/2018 DBMS - Ch2

    29/96

    - BEample

    Relation r, s:

    rs

    A B

    121

    A B

    23

    r s

    A B

    2

  • 5/21/2018 DBMS - Ch2

    30/96

    !atural Hoin Operation BEample

    Relations r, s: