Comp 521 f 14 Lecture 03

download Comp 521 f 14 Lecture 03

of 36

Transcript of Comp 521 f 14 Lecture 03

  • 8/11/2019 Comp 521 f 14 Lecture 03

    1/36

    Comp 521 Files and Databases Fall 2014 1

    The RelationalModel

    Chapter 3

  • 8/11/2019 Comp 521 f 14 Lecture 03

    2/36

    Comp 521 Files and Databases Fall 2014 2

    Why the Relational Model?

    Most widely used model by industry. IM! In"ormi#! Mi$roso"t! %ra$le! &ybase! My&'(!

    )ost*res! &+lite! ,i-e...

    It is simple! ele*ant! and e""i$ient

    ntities and relations are represented as tables /ables allow "or arbitrary re"eren$in*

    /ables $an re"er to other tables

    e$ent $ompetitor obe$toriented model %be$t&tore! 6ersant! %ntos

    7 synthesis emer*in* object-relational model8 )ost*res! In"ormi#! 9ni&'(! %2! %ra$le! D2

  • 8/11/2019 Comp 521 f 14 Lecture 03

    3/36

    Comp 521 Files and Databases Fall 2014 3

    Relational Database: Definitions

    Relational database:a set o" relations Relation:made up o" 2 parts

    Instance a table!with rows and $olumns.:rows ; cardinality! :"ields ; degree /arity.

    Schema spe$i"iesname o" relation! plus aname and type "or ea$h $olumn.

    8 e.*. &tudentssid strin*! name strin*! loginstrin*! age inte*er! ga real.

    Can thin< o" a relation as a set o" rowsor t!les.

  • 8/11/2019 Comp 521 f 14 Lecture 03

    4/36

    Comp 521 Files and Databases Fall 2014 4

    "#amle Instance of St!dents Relation

    sid name lo*in a*e *pa

    53=== >ones ones?$s 1@ 3.4

    53=@@ &mith smith?$s 1@ 3.2

    53=50 &mith smith?math 1A 3.@

    Cardinality ; 3! de*ree ; 5

    7ll rows in a relation instan$e ha$e tobe distinct ea$h relation is de"ined tobe a set o" uni+ue tuples

  • 8/11/2019 Comp 521 f 14 Lecture 03

    5/36

    Comp 521 Files and Databases Fall 2014 5

    Relational %!ery &ang!ages

    7 maor stren*th o" the relational modelis that it supports simple and power"ul'!eryingo" data.

    %"ten declarati$e instead o" imerati$e 'ueries $an be written intuiti-ely! and

    the DM& is responsible "or e""i$iente-aluation. )re$ise semanti$s "or relational +ueries.

    7llows the optimiBer to e#tensi-ely reorderoperations! and still ensure that the answerdoes not $han*e.

  • 8/11/2019 Comp 521 f 14 Lecture 03

    6/36

    Comp 521 Files and Databases Fall 2014 =

    The S%& %!ery &ang!age

    De-eloped by IM &ystem in the 1A0s eed "or a standard sin$e it is used by many

    -endors

    &tandards

    &'(@=

    &'(@A minor re-ision

    &'(A2 maor re-ision

    &'(AA maor e#tensions! almost current std

    &'(03adds EM( and M! a

  • 8/11/2019 Comp 521 f 14 Lecture 03

    7/36

    Comp 521 Files and Databases Fall 2014

    The S%& %!ery &ang!age

    /o "ind all 1@ year old students! we $anwrite

    &(C/

    F%M &tudents &J, &.a*e;1@

    8/o "ind ust names and lo*ins! repla$e the "irst line

    8Jhen a relation is re"eren$ed only on$e!the use o" -ariables is optional

    &(C/ &.name! &.lo*in

    sid name lo*in a*e *pa

    53=== >ones ones?$s 1@ 3.4

    53=@@ &mith smith?$s 1@ 3.2

    G&H in thise#pressionindi$ates a"ormal -ariablewhi$h ta

  • 8/11/2019 Comp 521 f 14 Lecture 03

    8/36

    Comp 521 Files and Databases Fall 2014 @

    ""e$ti-ely G>oinsH or $onne$ts two tables

    %!erying M!ltile Relations

    Jhat does the"ollowin* +uery$omputeK

    &(C/ &.name! .$idF%M &tudents &! nrolled J, S.sid=E.sid7D.*rade;G7H

    sid cid grade

    53688 Carnatic101 C

    53688 Reggae203 B

    53650 Topology112 A53666 History105 B

    i-en the "ollowin*

    instan$es o" nrolled and&tudents

    S.name E.cid

    Smit Topology

    we *et

    sid name lo*in a*e *pa

    53=== >ones ones?$s 1@ 3.4

    53=@@ &mith smith?$s 1@ 3.2

    53=50 &mith smith?math 1A 3.@

    St!dents"

    Enrolled"

  • 8/11/2019 Comp 521 f 14 Lecture 03

    9/36

  • 8/11/2019 Comp 521 f 14 Lecture 03

    10/36

    Comp 521 Files and Databases Fall 2014 10

    Destroying and )ltering Relations

    Destroys the relation &tudents. /hes$hema in"ormation andthe tuples aredeleted.

    D%) /7( &tudents

    /he s$hema o" &tudents is altered by

    addin* a new "ieldL e-ery tuple in the$urrent instan$e is e#tended with a n!ll-alue in the new "ield.

    7(/ /7( &tudents7DD C%(9M"irstear inte*er

  • 8/11/2019 Comp 521 f 14 Lecture 03

    11/36

    Comp 521 Files and Databases Fall 2014 11

    )dding and Deleting T!les

    Can insert a sin*le tuple usin*

    I&/ I/% &tudents sid! name! lo*in! a*e! *pa67(9& 53=5! N&mithO! Nsmith?physO! 1@! 3.5

    Can delete all tuples satis"yin* some$ondition e.*.! name ; &mith

    D(/F%M&tudents &

    J,&.name ; N&mithO

    *o+erf!l $ariants of these commands are a$ailable, more later

  • 8/11/2019 Comp 521 f 14 Lecture 03

    12/36

    Comp 521 Files and Databases Fall 2014 12

    Modifying "#isting T!les

    Can modi"y all tuples satis"yin* some$ondition e.*.! $han*e &mithPs name to

    >ones

    9)D7/ students&/ name ; P>onesPJ, name ; P&mithPL

  • 8/11/2019 Comp 521 f 14 Lecture 03

    13/36

    Comp 521 Files and Databases Fall 2014 13

    Integrity (onstraints I(s

    IC$ondition that must be true "or anyinstan$e o" the databaseL e.*.! domainconstraints. ICs are spe$i"ied when s$hema is de"ined.

    ICs are $he$

  • 8/11/2019 Comp 521 f 14 Lecture 03

    14/36

    Comp 521 Files and Databases Fall 2014 14

    *rimary 0ey (onstraints

    7 set o" "ields is a 1ey"or a relation i" 1. o two tuples $an ha-e same -alues in

    all

  • 8/11/2019 Comp 521 f 14 Lecture 03

    15/36

    Comp 521 Files and Databases Fall 2014 15

    *rimary and (andidate 0eys in S%&

    )ossibly many candidate 1eys spe$i"ied usin*9I'9! one o" whi$h is $hosen as therimary1ey.

    C7/ /7(nrolled sid C,720

    $id C,720! *rade C,72! )IM7 T sid!$id

    GFor a *i-en student and $ourse!there is a sin*le *rade.H -s.

    G&tudents $an ta

  • 8/11/2019 Comp 521 f 14 Lecture 03

    16/36

    Comp 521 Files and Databases Fall 2014 1=

    3oreign 0eys2 Referential Integrity

    3oreign 1ey &et o" "ields in one relation that isused to Gre"eren$eH a tuple in another relation.Must $orrespond to primary

  • 8/11/2019 Comp 521 f 14 Lecture 03

    17/36

    Comp 521 Files and Databases Fall 2014 1

    3oreign 0eys in S%&

    %nly students listed in the &tudents relationshould be allowed to enroll "or $ourses.

    C7/ /7(nrolled sid C,720! $id C,720! *rade C,72!

    )IM7 T sid!$id! F%I T sid FC&&tudents

    sid name lo*in a*e *pa

    53=== >ones ones?$s 1@ 3.453=@@ &mith smith?$s 1@ 3.2

    53=50 &mith smith?math 1A 3.@

    sid cid grade

    53688 Carnatic101 C

    53688 Reggae203 B

    53650 Topology112 A53666 History105 B

    nrolled&tudents

  • 8/11/2019 Comp 521 f 14 Lecture 03

    18/36

    Comp 521 Files and Databases Fall 2014 1@

    "nforcing Referential Integrity

    Consider &tudents and nrolledL sidin nrolled is a"orei*n

  • 8/11/2019 Comp 521 f 14 Lecture 03

    19/36

    Comp 521 Files and Databases Fall 2014 1A

    Referential Integrity in S%&

    &'(UA2 and &'(1AAAsupport all 4 options ondeletes and updates.

    De"ault is % 7C/I%

    delete/!date isrejected C7&C7D also delete

    all tuples that re"er todeleted tuple

    &/ 9(( U&/ DF79(/sets "orei*n

  • 8/11/2019 Comp 521 f 14 Lecture 03

    20/36

    Comp 521 Files and Databases Fall 2014 20

    Where do I(s (ome 3rom?

    ICs are based upon the semanti$s o" therealworld enterprise that is bein*des$ribed in the database relations.

    Je $an $he$< a database instan$e to see i"

    an IC is -iolated! but we $an 6in"erthat an IC is true by loo

  • 8/11/2019 Comp 521 f 14 Lecture 03

    21/36

    Comp 521 Files and Databases Fall 2014 21

    &ogical D6 Design: "R to Relational

    ntity sets to tables

    C7/ /7(

    mployeesssn C,711!name C,720!lot I/!)IM7 T ssnEmployees

    ssn

    name

    lot

  • 8/11/2019 Comp 521 f 14 Lecture 03

    22/36

    Comp 521 Files and Databases Fall 2014 22

    Relationshi Sets to Tables

    In translatin* arelationship set to arelation! attributes o" therelation must in$lude

    Teys "or ea$hparti$ipatin* entity setde$lared as "orei*n

  • 8/11/2019 Comp 521 f 14 Lecture 03

    23/36

    Comp 521 Files and Databases Fall 2014 23

    Re$ie+: 0ey (onstraints

    a$h dept has atmost one mana*er!a$$ordin* to the1ey constraint onMana*es.

    Translation to

    relational model?

    Many-to-Many1-to-1 1-to Many Many-to-1

    Employees

    ssn

    name

    lot

    (epartments

    did

    dname

    )!dget

    &anages

    since

  • 8/11/2019 Comp 521 f 14 Lecture 03

    24/36

    Comp 521 Files and Databases Fall 2014 24

    Translating "R Diagrams +ith 0ey (onstraints

    Map relationshipto a table ote that didis

    the

  • 8/11/2019 Comp 521 f 14 Lecture 03

    25/36

    Comp 521 Files and Databases Fall 2014 25

    Re$ie+: *articiation (onstraints

    Does e-ery department ha-e a mana*erK I" so! this is aarticiation constraint the parti$ipation o"

    Departments in Mana*es is said to be total-s.artial.

    -ery did-alue in Departments table must appear in a row o"the Mana*es table with a nonnull ssn-alue

    Employees

    ssn

    name

    lot

    (epartments

    did

    dname

    )!dget

    *or$s+,n

    since

    &anages

    since

  • 8/11/2019 Comp 521 f 14 Lecture 03

    26/36

    Comp 521 Files and Databases Fall 2014 2=

    *articiation (onstraints in S%&

    Je $an $apture parti$ipation $onstraints in-ol-in*one entity set in a binary relationship! but littleelse without resortin* to C,CT$onstraints.

    C7/ /7( DeptVM*r

    did I/! dname C,720! bud*et 7(! ssn C,711 %/ 9((! sin$e D7/!

    )IM7 T did! F%I T ssn FC&mployees! % D(/ % 7C/I%

  • 8/11/2019 Comp 521 f 14 Lecture 03

    27/36

    Comp 521 Files and Databases Fall 2014 2

    Re$ie+: Wea1 "ntities

    7 +ea1 entity $an be identi"ied uni+uely only by$onsiderin* the primary

  • 8/11/2019 Comp 521 f 14 Lecture 03

    28/36

    Comp 521 Files and Databases Fall 2014 2@

    Translating Wea1 "ntity Sets Jea< entity set and identi"yin* relationship

    set are translated into a sin*le table.

    Jhen the owner entity is deleted! all ownedwea< entities are also be deleted.

    C7/ /7( DepV)oli$y pname C,720! a*e I/! $ost 7(! ssn C,711 %/ 9((!

    )IM7 T pname! ssn! F%I T ssn FC&mployees! % D(/ C7&C7D

  • 8/11/2019 Comp 521 f 14 Lecture 03

    29/36

    Comp 521 Files and Databases Fall 2014 2A

    Re$ie+: IS) 7ierarchies

    8$erla constraints Can >oe be an ,ourlyVmps as wellas a Contra$tVmps entityK )llo+ed/disallo+ed

    (o$ering constraints Does e-ery mployees entity alsoha-e to be an ,ourlyVmps or a Contra$tVmps entityK9es/no

    Employees

    ssn name

    lot

    Ho!rly+Emps

    Ho!rly+.age

    Ho!rs+.or$ed

    Contract+emps

    salary,SA

    It is o"ten use"ul tosubdi-ide entities into$lasses! li

  • 8/11/2019 Comp 521 f 14 Lecture 03

    30/36

    Comp 521 Files and Databases Fall 2014 30

    Translating IS) 7ierarchies to Relations

    General approach: 3 relations mployees! ,ourlyVmps and

    Contra$tVmps.

    8 7o!rly"ms -ery employee is re$orded inmployees. For hourly emps! e#tra in"o re$orded in,ourlyVmps ho!rly+ages! ho!rs+or1ed! ssnLmust delete ,ourlyVmps tuple i" re"eren$edmployees tuple is deleted.

    8 'ueries in-ol-in* all employees easy! those in-ol-in*ust ,ourlyVmps re+uire a oin to *et some attributes.

    7lternati-e >ust ,ourlyVmps and Contra$tVmps. 7o!rly"ms ssn! name2 lot2 ho!rly+ages2

    ho!rs+or1ed. a$h employee must be in one o" these two sub$lasses.

  • 8/11/2019 Comp 521 f 14 Lecture 03

    31/36

    Comp 521 Files and Databases Fall 2014 31

    Re$ie+: 6inary $s. Ternary Relationshis

    e$all whatwere theadditional$onstraints

    implied bythe thebetterdesi*nK

    ad desi*n

    Employees

    ssn name lot

    (ependents

    pname age

    Co/ers

    -olicies

    costpolicyid

    etter desi*n

    (ependents

    pname age

    Employees

    ssn

    namelot

    -!rcaser Bene0iciary

    -olicies

    costpolicyi

    d

  • 8/11/2019 Comp 521 f 14 Lecture 03

    32/36

    Comp 521 Files and Databases Fall 2014 32

    6inary $s. Ternary Relationshis (ontd.

    Tey $onstraintsallow us to$ombine )ur$haserwith )oli$ies! andene"i$iary withDependents.

    )arti$ipation$onstraints leadto %/ 9(($onstraints.

    Jhat i" )oli$ies is awea< entity setK*eneri$ poli$ynumbers

    C7/ /7( )oli$ies poli$yid I/! $ost 7(! ssn C,711 %/ 9((! )IM7 T poli$yid. F%I T ssn FC& mployees! % D(/ C7&C7D

    C7/ /7( Dependents pname C,720! a*e I/! poli$yid I/! )IM7 T pname! poli$yid. F%I T poli$yid FC& )oli$ies! % D(/ C7&C7D

  • 8/11/2019 Comp 521 f 14 Lecture 03

    33/36

    Comp 521 Files and Databases Fall 2014 33

    ;ie+s

    7 $ie+is ust a relation! but we store adefinition! rather than a set o" tuples.

    C7/ 6IJoun*7$ti-e&tudents name! *rade7& &(C/ &.lo*in! .*rade

    F%M &tudents &! nrolled J, &.sid ; .sid and &.a*eW21

    6iews $an be dropped usin* the D%) 6IJ$ommand.

    ,ow to handle D%) /7(i" thereOs a -iew on the tableK8 D%) /7($ommand has options to let the user spe$i"y

    this.

  • 8/11/2019 Comp 521 f 14 Lecture 03

    34/36

    Comp 521 Files and Databases Fall 2014 34

    ;ie+s to s!ort IS) relations

    /he $ommon elements o" an I&7 hierar$hy$an be supported usin* -iews.

    For e#ample! $onsider this implementationo" 7lternate 2 "rom slide 30

    C7/ 6IJ mployeessn! name! lot7& &(C/ ,.ssn! ,.name! ,.lotF%M ,ourlyVmps9I%

    &(C/ C.ssn! C.name! C.lotF%M Contra$tVmps

  • 8/11/2019 Comp 521 f 14 Lecture 03

    35/36

    Comp 521 Files and Databases Fall 2014 35

    ;ie+s and Sec!rity

    6iews $an be used to present ne$essaryin"ormation or a summary! while hidin*details in underlyin* relations.

    i-en oun*&tudents! but not &tudents or

    nrolled! we $an "ind students who ha-e areenrolled! but not their sid5s! cid5s2 or e-en theira*es.

    lo*in *rade

    smith?$s C

    smith?$s smith?math 7

    ones?$s

  • 8/11/2019 Comp 521 f 14 Lecture 03

    36/36

    Comp 521 Files and Databases Fall 2014 3=

    Relational Model: S!mmary

    7 tabular representation o" data. &imple and intuiti-e! $urrently the most widely

    used.

    Inte*rity $onstraints $an be spe$i"ied by theD7! based on appli$ation semanti$s. DM&$he$