Lecture 3 - Entity Relationship Model1Fall 2001 Database Systems 1Database Design ProcessReal WorldRequirements AnalysisDatabaseRequirementsConceptual DesignConceptualModelLogical DesignPhysical DesignLogicalSchemaFunctionalRequirementsFunctional AnalysisAccessSpecificationsApplication Pgm DesignE-R ModelingChoice of aDBMSData ModelMappingFall 2001 Database Systems 2Entity-Relationship Model• An entity is a collection of real-world objects thathave many common properties.• Examples: Students, Instructors, Courses,Sections– Student entities have properties: name,address, major, graduation-year– A student may be “John Smith”, “22 Sage Rd.”,“Computer Science”, “2000”• An attribute is a data item that describes a propertyof an entity
Lecture 3 - Entity Relationship Model2Fall 2001 Database Systems 3EntitiesStudentssidstudent_namemid_initialfirstnamelastnamehobbiesprimary identifiermulti valued attributecomposite attributeFall 2001 Database Systems 4Mapping Entities to Relations• Each entity in an E-R model is mapped to a separaterelation– the primary identifier is mapped to the primary key(underlined!)– all regular attributes are mapped to an attribute in thetable– each subpart of a composite attribute is mapped to adifferent attribute– each multi-valued attributed is mapped to a separaterelation that inherits the primary key of the parentrelation
Lecture 3 - Entity Relationship Model3Fall 2001 Database Systems 5Mapping EntitiesStudentssidstudent_namemid_initialfirstnamelastnamehobbiesStudents( sid, lastname,firstname, mid_initial )Hobbies( sid, hobby )Foreign key sid referencesthe Students relation!Fall 2001 Database Systems 6Map the Auction EntitiesOwnersoidowner_namemid_initialfirstnamelastnameemailphone#itemsoldBuyersbuyidbuyer_nameemailphonecc_numaddressstreet citystate zip
Lecture 3 - Entity Relationship Model4Fall 2001 Database Systems 7Map the Auction EntitiesItemsiidname locationdescriptionBidsbidamountdate/timedate timeFall 2001 Database Systems 8Relationships• Given a set of entities E1,E2,…,Ek, a relationship R defines arule of correspondence between these entities.– An instance r(e1,e2,…,ek) of the R relation means entitiese1,e2,…,ek are in a relation r at this instance.– If two people are married, they are in a relationship:married(Bob, Margaret)– If a student A takes a course C offered by professor B,then A,B,C are in a relationship.
Lecture 3 - Entity Relationship Model5Fall 2001 Database Systems 9RelationshipsItemsBuyers BidsplaceownOwnersacceptdatebinary relationshipternary relationshipFall 2001 Database Systems 10Cardinalities of Relationships• Participation cardinalities of a relationship R for anentity E are:– min-card(E, R) : the minimum number of entitiesin E that should be mapped via R– max-card(E, R): the maximum number ofentities in E that can be mapped via R• Own is a relation between owner and item– Should each owner be selling items?– How many items can an owner sell?
Lecture 3 - Entity Relationship Model6Fall 2001 Database Systems 11Cardinalities of RelationshipsE R F E R F E R FOne-to-one relationshipmin-card(E, R)=0max-card(E,R)=1min-card(F,R)=0max-card(F,R)=1Many-to-one relationshipmin-card(E, R)=0max-card(E,R)=Nmin-card(F,R)=1max-card(F,R)=1Many-to-many relationshipmin-card(E, R)=0max-card(E,R)=Nmin-card(F,R)=0max-card(F,R)=NFall 2001 Database Systems 12CardinalitiesItemsBuyers BidsplaceownOwnersacceptdate(1,1) (0,N)(0,N)(0,1)(0,N)(1,1)(0,N)
Lecture 3 - Entity Relationship Model7Fall 2001 Database Systems 13Cardinalities• If max-card(E,R)=1 then E has single-valued participation inR• If max-card(E,R)=N then E has multi-valued participation inR• Given a binary relation R between E and F, R is said to be– one-to-one if both E and F have single-valuedparticipation– one-to-many if E has single and F has multi-valuedparticipation– many-to-many if both E and F have multi-valuedparticipationFall 2001 Database Systems 14Mapping Relationships to “Relations”• Map one-to-one and one-to-many (or many-to-one)relationships into the existing relations (derived from entities)– If E-R-F is one-to-many, then include the primary key of therelation for F in the relation for E.– If E-R-F is one-to-one, then include key for E in F, or thekey for F in E.– If E-R-F is many-to-many, create a new relation for R thathas the primary keys for both E and F.– If R has attributes, migrate them to the relation with theforeign keys!Contains Foreign Key(s)
Lecture 3 - Entity Relationship Model9Fall 2001 Database Systems 17Problem• Consider the design of a database to manage airlinereservations:– For flights, it contains the departure and arrivalairports, dates and times– For flights, it also contains a number of differentpricing plans with different conditions (Saturdaystay, advance booking, etc.)– For passengers, it contains the name, telephonenumber and seat type preference– Reservations include the seat assigned to apassenger– Passengers can have multiple reservationsFall 2001 Database Systems 18Solutionairport flightpassengerdepartarrivedatedatetimetimereservationseatpricing plannameconditionsname phone seat pref(0,N)(0,N)(0,N)(0,N) (1,1)(1,1)
Lecture 3 - Entity Relationship Model11Fall 2001 Database Systems 21Entity Relationship Model• Ternary relationships may be represented by binaryrelationshipsItemsBuyersStoresbuy_from(0,N)buy_itemsell_item(0,N)(0,N) (0,N)(0,N)(0,N) Is this conceptuallyequivalent to theprevious ternaryrelationship?Fall 2001 Database Systems 22Weak Entities• The existence of a weak entity W depends on the existenceof another (strong) entity E through a relationship R.• (Alternate) Two different weak entities may have the sameidentity (key) if they are related to two different strong entities.BranchBank has(0,N) (1,1)namenumber address
Lecture 3 - Entity Relationship Model12Fall 2001 Database Systems 23Weak Entities• Weak entities can be mapped to the relationalmodel by:– Map each weak entity E that depends on a strongentity F to a new relation R– Relation R contains all the attributes in E and theprimary key of F– The primary key for R is the primary key of E and theprimary key of FFall 2001 Database Systems 24ConcertsdateClassicalpiecesconductorsoloistsOtherperformerslocationorchestraGeneralization Hierarchies• Lower items inherit attributes of their parentsOption 1. Translate intoa single relation with aflag for the “type” of entity[many null values]Option 2. Translate intothree entities and two“is-a” relationships, thentranslate the resulting graph.
Lecture 3 - Entity Relationship Model13Fall 2001 Database Systems 25Extensions• All relational DBMSs come with extensions that give moreflexibility to the DBA• Examples from Informix– composite attributes - translate as a record address oftype ROW(street string, city string, state string, zipstring)– multi-valued attributes - translate into collection typessuch as sets, lists, multi-sets (bags)– hierarchies - create typed tables and translate into atype hierarchy.• REMEMBER, the extensions complicate the data modeland make certain SQL queries much harder or impossible,leaving the database programmer with a much harder jobof maintaining the database!