CMPT354: DatabaseSystemI · iPhone 8 Electronics $700 iPad 4 Electronics $300 Office Software $120...

71
CMPT 354: Database System I Lecture 8. The E/R Model 1

Transcript of CMPT354: DatabaseSystemI · iPhone 8 Electronics $700 iPad 4 Electronics $300 Office Software $120...

  • CMPT 354:Database System I

    Lecture 8. The E/R Model

    1

  • Motivation

    • How to figure out this database design?

    • What tables to create?• Which attributes should be added to each table?• What are the relationships between the tables?

    2

  • History of E/R Model

    • E/R Model (Entity-RelationshipModeling)• Codd wrotealonglettercriticizingpaper• Many people suggested him to give up this idea

    • Why not build RDBMS based on E/R Model?• Noquerylanguage proposed• RelationalDBMSinthe1970’s

    3

    Dr. Peter Chen

  • Outline

    • E/RBasics:Entities&Relationships

    • E/RDesignconsiderations

    • AdvancedE/RConcepts

    4

  • Outline

    • E/RBasics:Entities&Relationships• DatabaseDesign• Entities/Entitysets/Keys/Relationships

    • E/RDesignconsiderations

    • AdvancedE/RConcepts

    5

  • 6

    DatabaseDesign

    • Databasedesign:Whydoweneedit?• Agreeonstructureofthedatabasebeforedecidingonaparticularimplementation

    • Considerissuessuchas:• Whatentitiestomodel• Howentitiesarerelated• Whatconstraintsexistinthedomain• Howtoachievegood designs

    • Severalformalismsexist• WediscussoneflavorofE/Rdiagrams

  • DatabaseDesignProcess

    1. Requirementsanalysis

    • Whatdataisgoingtobestored?

    • Whatarewegoingtodowiththedata?

    • Whoshouldaccessthedata?

    7

    1.RequirementsAnalysis 2.ConceptualDesign 3.Logical,Physical,Security,etc.

    Technicalandnon-technicalpeopleareinvolved

  • 2.ConceptualDesign

    • Ahigh-leveldescription ofthedatabase

    • Sufficientlyprecise thattechnicalpeoplecanunderstandit

    • But,notsoprecisethatnon-technicalpeoplecan’tparticipate

    8

    1.RequirementsAnalysis 2.ConceptualDesign 3.Logical,Physical,Security,etc.

    DatabaseDesignProcess

    ThisiswhereE/Rfitsin.

  • 9

    3.More:

    • LogicalDatabaseDesign

    • PhysicalDatabaseDesign

    • SecurityDesign

    1.RequirementsAnalysis 2.ConceptualDesign 3.Logical,Physical,Security,etc.

    DatabaseDesignProcess

  • 10

    E/RisavisualsyntaxforDBdesignwhichispreciseenough fortechnicalpoints,butabstractedenough fornon-technicalpeople

    MakesProduct

    name category

    price

    Company

    name

    E/RModel&Diagramsused

    1.RequirementsAnalysis 2.ConceptualDesign 3.Logical,Physical,Security,etc.

    DatabaseDesignProcess

  • EntitiesandEntitySets• Anentity isanindividualobject• Eg:Aspecificpersonorproduct

    • Anentityset isacollectionofentitiesofthesametype• ThesearewhatisshowninE/Rdiagrams- asrectangles• Eg:Person,Product

    11

    ProductPerson

  • Attributes

    • Anentitysethasattributes• Representedbyovalsattachedtoanentityset

    12

    Product

    name category

    price

  • Example

    13

    Product

    name category

    price

    EntitySet

    Product

    Name:XboxCategory:Total

    MultimediaSystemPrice:$250

    Name:MyLittlePonyDollCategory:ToyPrice:$25

    Entity

    EntityAttribute

    Entitiesarenot explicitlyrepresentedinE/Rdiagrams!

  • Keys

    • Akey isasetofattributesthatuniquelyidentifiesanentity.• Everyentitysetmusthaveakey

    • Denoteelementsoftheprimarykeybyunderlining.

    14

    Product

    name category

    price

  • TheRinE/R:Relationships

    • Arelationship isbetweentwoentities

    15

    Product

    name category

    priceCompany

    name

    Makes

  • 16

    makes

    buys employs

    Product

    name category

    price

    Person

    address name ssn

    Company

    stockprice

    name

  • 17

    WhatisaRelationship?

    • Amathematicaldefinition:

    • LetA,Bbesets• A={1,2,3},B={a,b,c,d}

    1

    2

    3

    a

    b

    c

    d

    A= B=

  • 18

    WhatisaRelationship?• Amathematicaldefinition:

    • LetA,Bbesets• A={1,2,3},B={a,b,c,d}

    • AxB(thecross-product)isthesetofallpairs(a,b)• A´ B={(1,a),(1,b),(1,c),(1,d),(2,a),(2,b),(2,c),(2,d),(3,a),(3,b),(3,c),(3,d)}

    1

    2

    3

    a

    b

    c

    d

    A= B=

  • 19

    WhatisaRelationship?

    • Amathematicaldefinition:

    • LetA,Bbesets• A={1,2,3},B={a,b,c,d},

    • AxB(thecross-product)isthesetofallpairs(a,b)• A´ B={(1,a),(1,b),(1,c),(1,d),(2,a),(2,b),(2,c),(2,d),(3,a),(3,b),(3,c),(3,d)}

    • Wedefinearelationship tobeasubsetofAxB• R={(1,a),(2,c),(2,d),(3,b)}

    1

    2

    3

    a

    b

    c

    d

    A= B=

  • WhatisaRelationship?

    20

    name category priceiPhone8 Electronics $700iPad4 Electronics $300Office Software $120

    nameAppleMicrosoft

    ProductCompany

    MakesProduct

    name category

    price

    Company

    name

    Arelationship betweenentitysetsPandCisasubsetofallpossiblepairsofentitiesinPandC,withtuplesuniquelyidentifiedbyPandC’skeys

  • WhatisaRelationship?

    21

    name category priceiPhone8 Electronics $700iPad4 Electronics $300Office Software $120

    nameAppleMicrosoft

    ProductCompanyC.name P.name P.category P.priceApple iPhone8 Electronics $700Apple iPad4 Electronics $300Apple Office Toys $120Microsoft iPhone8 Electronics $700Microsoft iPad4 Electronics $300Microsoft Office Toys $120

    CompanyC×ProductP

    MakesProduct

    name category

    price

    Company

    name

    Arelationship betweenentitysetsPandCisasubsetofallpossiblepairsofentitiesinPandC,withtuplesuniquelyidentifiedbyPandC’skeys

  • WhatisaRelationship?

    22

    C.name P.name

    Apple iPhone8

    Apple iPad4

    Microsoft Office

    Makes

    name category priceiPhone8 Electronics $700iPad4 Electronics $300Office Software $120

    nameAppleMicrosoft

    ProductCompanyC.name P.name P.category P.priceApple iPhone8 Electronics $700Apple iPad4 Electronics $300Apple Office Software $120Microsoft iPhone8 Electronics $700Microsoft iPad4 Electronics $300Microsoft Office Software $120

    CompanyC×ProductP

    MakesProduct

    name category

    price

    Company

    name

    Arelationship betweenentitysetsPandCisasubsetofallpossiblepairsofentitiesinPandC,withtuplesuniquelyidentifiedbyPandC’skeys

  • WhatisaRelationship?

    • Therecanonlybeonerelationshipforeveryuniquecombinationofentities

    • Thisalsomeansthattherelationshipisuniquelydeterminedbythekeysofitsentities

    • Example:the“key”forMakes(toright)is{Product.name,Company.name}

    23

    Thisfollowsfromourmathematicaldefinitionofarelationship- it’saSET!

    MakesProduct

    name category

    price

    Company

    name

  • 24

    Product

    name category

    priceCompany

    name

    Makes

    since

    • Relationshipsmayhaveattributesaswell.

    Forexample:“since”recordswhencompanystartedmakingaproduct

    RelationshipsandAttributes

    C.name P.name Since

    Apple iPhone8 2018.09.01

    Apple iPhone8 2017.09.01

    Makes

  • Decision:Relationshipvs.Entity?

    • Q:Whatdoesthissay?

    • A:Apersoncanonlybuyaspecificproductonce

    25

    PurchasedProduct

    name category

    price

    Person

    name

    date

    Person.name Product.name Date

    Jiannan iPhone8 2018.10.01

    Jiannan iPhone8 2018.12.01

    Purchase

  • Decision:Relationshipvs.Entity?

    • Whataboutthisway?

    • Nowwecanhavemultiplepurchasesperproduct,personpair!

    26

    Product

    name category

    price

    Person

    name

    date

    Purchase

    quantityPID#

    ProductOf BuyerOf

    Wecanalwaysuseanewentityinsteadofarelationship.Forexample,topermitmultipleinstancesofeachentitycombination!

  • Exercise-1

    27

  • DrawanE/Rdiagramforgeography

    28

    Entities• Country:name,area,population,gdp• City:name,population,longitude,latitude• River:name,length• Sea:name,maxdepth

    Relationships• CitybelongstoCountry• RivercrossesCountry• RiverendsinSea

  • Outline

    • E/RBasics:Entities&Relationships• DatabaseDesign• Entities/Entitysets/Keys/Relationships

    • E/RDesignconsiderations• Relationshipscond’s:multiplicity,multi-way• Designconsiderations• ConversiontoSQL

    • AdvancedE/RConcepts

    29

  • 30

    MultiplicityofE/RRelationships

    Indicatedusingarrows

    123

    abcd

    One-to-one:

    123

    abcd

    Many-to-one:

    123

    abcd

    One-to-many:

    123

    abcd

    Many-to-many:

    X->YmeansthereexistsafunctionmappingfromXtoY(recallthedefinitionofafunction)

  • 31

    address name ssn

    Person

    buys

    makes

    employs

    CompanyProduct

    name category

    stockprice

    name

    price

    Whatdoesthissay?

  • 32

    Multi-wayRelationships

    Howdowemodelapurchaserelationshipbetweenbuyers,productsandstores?

    Purchase

    Product

    Person

    Store

  • 33

    Q:Whatdoesthearrowmean?

    ArrowsinMultiwayRelationships

    Purchase

    Product

    Person

    Store

    givenaperson,candeterminewhattheyboughtandthestorewheretheyboughtit

  • 34

    Q:Whatdoesthearrowmean?

    ArrowsinMultiwayRelationships

    Purchase

    Product

    Person

    Store

    givenastore,candeterminewhoshoppedthereandtheproducttheyboughteachstoresellsoneproductandtooneperson,ever

  • 35

    Q:Howdowesaythateverypersonshopsinatmostonestore?

    ArrowsinMultiwayRelationships

    A:Cannot.Thisisthebestapproximation.(Whyonlyapproximation?)

    Product

    Person

    StorePurchase

  • 36

    ConvertingMulti-wayRelationshipstoBinary

    Purchase

    Person

    Store

    Product

    StoreOf

    ProductOf

    BuyerOf

    date Fromwhatwehadonpreviousslidetothis- whatdidwedo?

  • Decision:Multi-wayorNewEntity+Binary?

    37

    Purchase

    Person

    Store

    Product

    StoreOf

    ProductOf

    BuyerOf

    date

    Purchase

    Product

    Person

    Store

    Multi-wayRelationship Entity+Binary

    • (B)isalsousefulwhenwewanttoadddetails(constraintsorattributes)totherelationship- “Apersonwhoshopsinatmostonestore”- “Howlongapersonhasbeenshoppingatastore”

    • (A)isusefulwhenarelationshipreallyisbetweenmultipleentities- Ex:Athree-partylegalcontract

  • 38

    DesignPrinciples

    PurchaseProduct Person

    What’swrongwiththeseexamples?

    President PersonCountry

    multiplepresidents,alsomaywanttorequirecountrytohavepresident

    productbuysonlyoneproduct,thenout

  • 39

    DesignPrinciples:What’sWrong?

    Purchase

    Product

    Store

    date

    personNamepersonAddr

    maybepeopleshouldbeentities!

  • 40

    Purchase

    Product

    Person

    Store

    dateDates

    DesignPrinciples:What’sWrong?

    datesdon’tneedtobeanentitybythemselves

  • Examples:Entityvs.Attribute

    41

    Shouldaddress(A)beanattribute?

    Employee

    Addr 1 Addr 2

    Address

    StreetAddr ZIP

    Employee

    AddrOf

    Or(B)beanentity?

  • Examples:Entityvs.Attribute

    42

    Shouldaddress(A)beanattribute?

    Howdowehandleemployeeswithmultipleaddresseshere?

    Howdowehandleaddresseswhereinternalstructureoftheaddress(e.g.zipcode,state)isuseful?Employee

    Addr 1 Addr 2

  • Examples:Entityvs.Attribute

    43

    Shouldaddress(A)beanattribute?

    Employee

    Addr 1 Addr 2

    Address

    StreetAddr ZIP

    Employee

    AddrOf

    Or(B)beanentity?

    Ingeneral,whenwewanttorecordseveralvalues,wechoosenewentity

  • Exercise-2

    44

  • DrawanE/Rdiagramforgeography

    45

    Entities• Country:name,area,population,gdp• City:name,population,longitude,latitude• River:name,length• Sea:name,maxdepth

    Relationships• Eachcitybelongstoasinglecountry• Eachrivercrossesoneorseveralcountries• Eachriverendsinasinglesea

  • FromE/RDiagramstoRelationalSchema• Keyconcept:

    BothEntitysetsandRelationships becomerelations(tablesinRDBMS)

    46

  • FromE/RDiagramstoRelationalSchema

    name price category

    iPhone 700 Electronics

    Office 150 Software

    47

    Product

    price category

    name

    Product

    • Anentitysetbecomesatable– Eachrowisoneentity– Eachrowiscomposedoftheentity’s

    attributes,andhasthesameprimarykey

    CREATE TABLE Product(name CHAR(50) PRIMARY KEY,price DOUBLE,category VARCHAR(30)

    )

  • FromE/RDiagramstoRelationalSchema

    name firstname lastname date

    iPhone Mike Jordan 01/01/18

    iPhone Jiannan Wang 01/03/18

    iPad John Smith 01/05/18

    48

    Purchased

    • Arelationshipalsobecomesatable– AddPrimaryKey– AddForeignKey

    PurchasedProduct

    name category

    pricePerson

    firstnamedate lastname

    CREATE TABLE Purchased(name CHAR(50),firstname CHAR(50),lastname CHAR(50),date DATE,PRIMARY KEY (name, firstname, lastname),FOREIGN KEY (name)

    REFERENCES Product,FOREIGN KEY (firstname, lastname)

    REFERENCES Person)

  • Exercise-3

    49

  • FromE/RDiagramtoRelationalSchema

    50

    Howdowerepresentthisasarelationalschema?

  • Outline

    • E/RBasics:Entities&Relationships• DatabaseDesign• Entities/Entitysets/Keys/Relationships

    • E/RDesignconsiderations• Relationshipscond’s:multiplicity,multi-way• Designconsiderations• ConversiontoSQL

    51

    makes CompanyProduct

    name categoryname

    price

    country

  • Outline

    • E/RBasics:Entities&Relationships• DatabaseDesign• Entities/Entitysets/Keys/Relationships

    • E/RDesignconsiderations• Relationshipscond’s:multiplicity,multi-way• Designconsiderations• ConversiontoSQL

    • AdvancedE/RConcepts• Combing Relations• Constraints• Subclass• Weak Entity Sets

    52

  • • For many-to-one relationships

    Combing Relations

    53

    makes CompanyProduct

    name categoryname

    price

    P.name C.name

    iPhone8 Apple

    iPad4 Apple

    Office Microsoft

    name category priceiPhone8 Electronics $700iPad4 Electronics $300Office Software $120

    name countryApple USMicrosoft US

    Product MakeCompany

    country

  • Combing Relations

    54

    • Remember:noseparaterelationsformany-onerelationship

    P.name C.name category priceiPhone8 Apple Electronics $700iPad4 Apple Electronics $300Office Microsoft Software $120

    name category priceiPhone8 Electronics $700iPad4 Electronics $300Office Software $120

    name countryApple USMicrosoft US

    Product MakeCompany

    name countryApple USMicrosoft US

    P.name C.name

    iPhone8 Apple

    iPad4 Apple

    Office Microsoft

  • 55

    ConstraintsinE/RDiagrams

    • FindingconstraintsispartoftheE/Rmodelingprocess.Commonlyusedconstraintsare:

    • Keys• Ex:A product name uniquelyidentifiesaproduct

    • Single-valueconstraints:• Ex:aproduct made by exactly one company

    • Participation constraints:• Ex:all products are made by a company

  • 56

    KeysinE/RDiagrams

    address name ssn

    Person

    Product

    name category

    price

    Underlinekeys:

    Note:noformalwaytospecifymultiple keysinE/Rdiagrams…

  • 57

    Single-Value Constraints

    CompanyProduct makes

    CompanyProduct makes

    Eachproductmadebyatmostonecompany.Someproductsmadebynocompany?

    Eachproductmadebyexactly onecompany.

  • ParticipationConstraints:Partialv.Total

    58

    makesProduct Company

    Arethereproductsmadebynocompany?Companiesthatdon’tmakeaproduct?

    makesProduct Company

    Boldlineindicatestotalparticipation (i.e.here:allproductsaremadebyacompany)

  • 59

    ModelingSubclasses

    Someobjectsinaclassmaybespecial• Defineanewclass?

    • Butwhatifwewanttomaintainconnectiontocurrentclass?

    • Better:defineasubclass• Ex:

    Products

    Softwareproducts

    Hardwareproducts

    Wecandefinesubclasses inE/R!

  • 60

    Product

    name

    price

    SoftwareProduct

    platforms

    Hardware Product

    weight

    isA

    ModelingSubclasses

    Childsubclassescontainalltheattributesofalloftheirparentclassesplus thenewattributesshownattachedtothemintheE/Rdiagram

  • 61

    UnderstandingSubclasses

    • Thinkintermsofrecords;ex:

    • Product

    • SoftwareProduct

    • HardwareProduct

    nameprice

    namepriceplatforms

    namepriceweight

  • 62

    name price

    iphone 8 700

    iPad 4 300

    office 100

    name platforms

    office windows

    name weight

    iphone 8 148 g

    ipad 4 650 g

    Product

    SoftwareProduct

    HardwareProduct

    SubclassestoRelations

  • IsA Review

    • IfwedeclareAIsA BtheneveryA isaB

    • WeuseIsA to

    • Adddescriptiveattributestoasubclass

    • Toidentifyentitiesthatparticipateinarelationship

    63

  • 64

    ModelingUnionTypes WithSubclasses

    Say: eachpieceoffurnitureisownedeitherbyaperson,orbyacompany

    FurniturePiecePerson Company

  • 65

    ModelingUnionTypes WithSubclasses

    Say:eachpieceoffurnitureisownedeitherbyapersonorbyacompany

    Solution1. Acceptable,butimperfect(What’swrong?)

    FurniturePiecePerson Company

    ownedByPerson ownedByComp

  • 66

    ModelingUnionTypes WithSubclasses

    Solution2:better(thoughmorelaborious)

    FurniturePiece

    Person Company

    ownedBy

    Owner

    isa

  • WeakEntitySets

    Entitysetsareweak whentheirkeycomesfromotherclassestowhichtheyarerelated.

    “Introduction to database”vs.“TheSFU introduction to database”

    uname

    UniversityCourse Offer

    cnametextbook

    67

  • WeakEntitySets

    • cname isapartialkey (denotewithdashedunderline).• Universityiscalledthesupporting entity set• Offer is called the supporting relationship

    uname

    UniversityCourse Offer

    cnametextbook

    Entitysetsareweak whentheirkeycomesfromotherclassestowhichtheyarerelated.

    68

  • WeakEntitySets to Relations

    69

    uname

    UniversityCourse Offer

    cnametextbook

    Course(cname, uname, textbook)

    University(uname)

    Offering(cname, Course.uname, University.uname)

  • E/R Summary

    • E/RBasics:Entities&Relationships• DatabaseDesign• Entities/EntitySets/Keys/Relationships

    • E/RDesignconsiderations• Relationshipscond’s:multiplicity,multi-way• Designconsiderations• ConversiontoSQL

    • AdvancedE/RConcepts• Combing Relations• Constraints• Subclass• Weak Entity Sets

    70

  • Acknowledge• Some lecture slides were copied from or inspired by thefollowing course materials• “W4111: Introduction to databases” by Eugene Wu atColumbia University• “CSE344: IntroductiontoDataManagement” by Dan Suciu atUniversityof Washington• “CMPT354: Database System I” by JohnEdgar at Simon FraserUniversity• “CS186: Introduction to Database Systems” by Joe Hellersteinat UC Berkeley• “CS145: IntroductiontoDatabases” by Peter Bailis at Stanford• “CS348:IntroductiontoDatabaseManagement” by GrantWeddell at University of Waterloo

    71