Data Modeling with ER Diagrams What is an Entity-Relationship Model? How is an E-R model represented as an E-R diagram? How can a video store be modeled as an E-R diagram? What is a role in a relationship type? How can we record the history of video rentals? How are cardinalities and related to roles? How can we represent purchase orders? How can we represent time cards and pay statements? Entity-Relationship Modeling An E-R model is a data model that includes Entity classes Attributes of each class Relationship types between classes Constraints Types of attributes Designation of key attributes Cardinalities of relationship types An E-R Model is typically represented graphically – E-R diagram, the technique we use UML diagram, an emerging standard for specifying E-R models and software design Entity Relationship Diagrams Sample diagram for entity class Customer Attribute lastName firstName accountId Key Attribute balance otherUsers Entity Class Customer Multivalued Attribute numberRentals Derived Attribute Composite Attribute address street city zipcode state Component Attribute E-R Diagram for Class Video Sample diagram for entity class Video Key Attribute v ideotapeId title genre Entity Class Video Attribute date Acquired length rating Relationship Types Example of representing relationship type Owns between classes Store and Video Enti ty Cl ass Al ternate Rel ati onshi p Nam e Rel ati onshi p T ype IsOwnedBy Store Rel ati onshi p Attri bute Owns purchase Date cost Vi deo Enti ty Cl ass Constraints on Relationship Types Example of cardinality and participation constraints Mandatory Optional partic ipation Cons traint partic ipation Cons traint Is Ow nedBy Store Ow ns 1 V ideo M To-one purc has e c os t Date relations hip role to-many relations hip role Examples of current Modeling Video rentals and previous Rentals rentals Differences are in cardinalities and M 1 Cus tomer Rents V ideo attribute names dateDue c os t dateRented Cus tomer M Previously Rented date Returned M c os t dateRented V ideo Modeling Video Rentals as an Entity Class Diagram shows entity class Rental and its relationship types with Customer and Video Note cardinalities and participation constraints A Rental entity cannot exist without being related to both a customer and a video Problem occurs because Rental has no key attribute 1 Cus tomerHas 1 Rental 1 Has M dateDue dateRented c os t V ideo A weak entity class is Weak Entity Classes An entity class with no key of its own An entity class whose entities cannot exist without being related to other entities An identifying relationship type is A relationship type that determines the keys of the weak entities Weak EntityOw ner Enti Clas s Clas s 1 Cus tomerHas 1 Rental 1 Has V ideo M dateDue dateRented c os t Identif y ing Relations hip Ty pe Roles in Relationship Types Each entity in a relationship plays a specific role in it Roles are particularly important in relationship types that relate an entity class to itself Each person in an IsChildOf relationship is either a Role parent or a child Name wi fe ch i l d 1 Is M a rri e d To M Is Ch i l d Of P e rso n Is ParentOf 1 h u sb a n d 1 ..2 ssn p a re n t M In/max Cardinality Constraint Employee Roles and Cardinalities Is this an accurate model of the relationships between employees and stores? For example, does this diagram answer these questions correctly? Does a store have to have a manager? Can an employee be the manager of more than one store? What other questions can you ask about roles and cardinalities? WorksIn M Paid To 1 M worker Employee 1 Store 1 manager Manage s 1 1 M PayStatement M Has datePaid TimeCard amount startTime endTime M Has Purchase Orders What does the diagram below mean? How do we buy many copies of a single movie? Does each video have its own videoId before it is received? What does it mean for an order of a video to have a quantity? The problem with the diagram is with class Video The diagram fails to distinguish between a movie and a copy of a movie (a video) We need to revise the diagram to show Movie as an entity class id 1 Supplier M Has PurchaseOrder 1 Orders Video M quantity Modeling Videos and Movies This diagram more accurately specifies the true nature of Video and Movie It also has an accurate specification of PurchaseOrder purchas e Date Id 1 PurchaseOrder Has l i neNumber M PurchaseOrder quantity Detai l M uni tCost M Buys From Orders vi deoId 1 1 movi eId Suppl i er title Movi e 1 genre name address l ength rati ng Is Copy M Of date Acqui red Vi deo Time Cards and Pay Statements Classes TimeCard and PayStatement are weak Does this diagram show the correct identifying relationships and discriminators? For example, can an employee have 2 time cards that begin at the same time but are at different stores? M Employee M TimeCard Has 1 Has Store 1 1 startTime endTime Discriminator Paid To M PayStatement Discriminator datePaid amount