Published on


1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. The E-R Model <ul><li>Prof. Sin-Min Lee </li></ul><ul><li>Department of Computer Science </li></ul>CS 157A Lecture 4
  3. 7. Class Hierarchies Employees ssn name lot ISA Hourly Emps Contract Emps Hourly wage Hours worked contractid * Sometimes it’s natural to classify the entities in an entity set into subclass, then we will use Class Hierarchies.
  4. 8. Aggregation name ssn lot Employees monitors until since Sponsors Department Projects pid Started-on pbudget dname did budget * A relationship set is an association between entity sets. Sometimes we have to model a relationship between a collection of entities and relationships, then we need to use aggregation .
  5. 9. Conceptual design using the ER model <ul><li>Should a concept be modeled as an entity or an attribute ? </li></ul><ul><li>Should a concept be modeled as an entity or a relationship ? </li></ul><ul><li>What are the relationships ? Should we use binary or ternary relationships ? </li></ul><ul><li>Should we use aggregation ? </li></ul>
  6. 10. UNARY RELATIONSHIP <ul><li>Unary relationship : is an associate of 1 entity set. </li></ul><ul><li>* Note : an entity set is a group of related entities. </li></ul>Student * Entity set = * Relation = Dr. Lee * Attribute =
  7. 11. BINARY RELATIONSHIP <ul><li>Binary relationship : is an associate of 2 entities sets . </li></ul>Employees Works_In Department * Entity set = * Relation = * Attribute = ssn name lot did budget dname
  8. 12. TERNARY RELATIONSHIP Works_In Employees Department Locations Ternary relationship : is an associate of 3 entities sets . * Entity set = * Relation = ssn name lot did dname budget cap add * Attribute =
  9. 13. QUARTERY RELATIONSHIP Employees Works_In Locations Department Product Quartery relationship : is an associate of 4 entities sets. * Entity set = * Relation = * Attribute =
  10. 15. Additional features of the ER model <ul><li>In the ER model allow us to draw important distinctions about the data. </li></ul><ul><li>One of the features of the ER model is : </li></ul><ul><li>Key Constraints </li></ul><ul><li>Note : A key constraints is also known as Primary key. </li></ul><ul><li>A primary key is a candidate key selected to uniquely identify all other attribute values in any given row, can not contain null entries. </li></ul>
  11. 16. An example of Key Constraints on Manage Manages Department Employees ssn name lot Since dname did budget ssn = Social Security Number. did = Department id.
  12. 18. Weak Entities A weak entity : is an entity of which is existence depends on other entities. Policy Department Employees ssn name lot cost Pname --------- age
  13. 21. Entity versus Attribute Works-In2 Department Employees ssn name lot from dname did budget to * Entity set = * Relation = * Attribute =
  14. 22. Entity versus Relationship Manages Department Employees ssn name apptnum did budget dname Mgr-Appts lot since dbudget
  15. 23. Binary versus Ternary Relationship Covers Dependents Employees ssn name Pname --------- age Policies lot policyid cost
  16. 24. Using a Ternary Relationship instead of Aggregation Sponsors Departments Projects pid Started-on did budget pbudget dname Employees name ssn lot
  17. 25. ONE TO MANY RELATIONSHIP customer customer-name customer-city customer-id borrower customer-street loan loan-number amount
  18. 26. MANY TO ONE RELATIONSHIP customer customer-name customer-city customer-id borrower customer-street loan loan-number amount
  19. 27. ONE TO ONE RELATIONSHIP customer customer-name customer-city customer-id borrower customer-street loan loan-number amount
  20. 28. E-R Diagram with Composite, Multivalued, and Derived Attributes Customer date-of-birth phone-number name customer-id first-name age last-name middle-name zip-code state city address street Street-number apartment-number street-name
  21. 29. WEAK ENTITY SETS <ul><li>An entity set may not have sufficient attributes to form a primary key. Such an entity set is termed a weak entity set . An entity set that has a primary key is termed a strong entity set . </li></ul><ul><li>For a weak entity set to be meaningful, it must be associated with another entity set, called the identifying or owner entity set . The relationship associating the weak entity set with the identifying entity set is called the identifying relationship . </li></ul><ul><li>The identifying relationship is many to one from the weak entity set to the identifying entity set, and the participation of the weak entity set in the relationship is total . </li></ul>
  22. 30. DISCRIMINATOR <ul><li>The discriminator of a weak entity set is a set of attributes that allows this distinction to be made. For example, the discriminator of a weak entity set payment is the attribute payment-number , since, for each loan a payment number uniquely identifies one single payment for that loan. The discriminator of a weak entity set is also called the partial key of the entity set. </li></ul><ul><li>Note: although each payment entity is distinct, payments for different loans may share the same payment-number . Thus, payment entity set does not have a primary key; it is a weak entity set. </li></ul><ul><li>The primary key of a weak entity set is formed by the primary key of the identifying entity set, plus the weak entity set’s discriminator. </li></ul>
  23. 31. E-R DIAGRAM WITH A WEAK ENTITY SET <ul><li>In a E-R diagrams, a doubly outlined box indicates a weak entity set, and a doubly outlined diamond indicates the corresponding identifying relationship. We underline the discriminator of a weak entity set with a dashed line . </li></ul>loan E-R diagram with a weak entity set loan-payment payment payment-date payment-amount payment-number amount loan-number
  24. 36. Entity/Relationship Model <ul><li>Diagrams to represent designs. </li></ul><ul><li>Entity like object, = “thing.” </li></ul><ul><li>Entity set like class = set of “similar” entities/objects. </li></ul><ul><li>Attribute = property of entities in an entity set, similar to fields of a struct. </li></ul><ul><li>In diagrams, entity set  rectangle; attribute  oval. </li></ul>Students ID name phone height
  25. 37. Relationships <ul><li>Connect two or more entity sets. </li></ul><ul><li>Represented by diamonds. </li></ul>Students Courses Taking
  26. 38. Relationship Set <ul><li>Think of the “value” of a relationship set as a table. </li></ul><ul><li>One column for each of the connected entity sets. </li></ul><ul><li>One row for each list of entities, one from each set, that are connected by the relationship. </li></ul><ul><ul><li>Students Courses </li></ul></ul><ul><ul><li>Sally CS180 </li></ul></ul><ul><ul><li>Sally CS111 </li></ul></ul><ul><ul><li>Joe CS180 </li></ul></ul><ul><ul><li>… … </li></ul></ul>
  27. 39. Multiway Relationships <ul><li>Usually binary relationships (connecting two E.S.) suffice. </li></ul><ul><li>However, there are some cases where three or more E.S. must be connected by one relationship. </li></ul><ul><li>Example: relationship among students, courses, TA's (and graders). </li></ul><ul><li>Possibly, this E/R diagram is OK: </li></ul>Students Courses Taking Assisting TA/Graders
  28. 40. <ul><li>Works in CS180, because each TA (or grader) is a TA of all students. Connection student-TA is only via the course. </li></ul><ul><li>But what if students were divided into sections, each headed by a TA? </li></ul><ul><ul><li>Then, a student in CS180 would be related to only one of the TA's for CS180. Which one? </li></ul></ul><ul><li>Need a 3-way relationship to tell. </li></ul>
  29. 41. <ul><li>Students Courses TAs </li></ul><ul><li>Ann CS180 Jan </li></ul><ul><li>Sue CS180 Pat </li></ul><ul><li>Bob CS180 Jan </li></ul><ul><li>… … … </li></ul>Students Courses TAs Enrolls
  30. 42. Beers-Bars-Drinkers Example <ul><li>Our running example for the course. </li></ul>name addr license name manf name addr Beers Drinkers Bars Serves Frequents Likes
  31. 43. Multiplicity of Relationships <ul><li>Representation of Many-One </li></ul><ul><li>E/R: arrow pointing to “one.” </li></ul><ul><ul><li>Rounded arrow = “exactly one.” </li></ul></ul>Many-many Many-one One-one
  32. 44. Example: Drinkers Have Favorite Beers name addr license name manf name addr Beers Drinkers Bars Serves Frequents Likes Favorite
  33. 45. One-One Relationships <ul><li>Put arrows in both directions. </li></ul><ul><li>Design Issue: </li></ul><ul><li>Is the rounded arrow justified? </li></ul><ul><li>Design Issue: </li></ul><ul><li>Here, manufacturer is an E.S. </li></ul><ul><li>In earlier diagrams it is an attribute. </li></ul><ul><li>Which is right? </li></ul>Manfs Beers Best- seller
  34. 46. Attributes on Relationships <ul><li>Shorthand for 3-way relationship: </li></ul>Bars Beers Sells price Bars Beers Sells price Prices
  35. 47. <ul><li>A true 3-way relationship. </li></ul><ul><ul><li>Price depends jointly on beer and bar. </li></ul></ul><ul><li>Notice arrow convention for multiway relationships: “all other E.S. determine one of these.” </li></ul><ul><ul><li>Not sufficiently general to express any possibility. </li></ul></ul><ul><ul><li>However, if price, say, depended only on the beer, then we could use two 2-way relationships: price-beer and beer-bar. </li></ul></ul><ul><ul><li>Or better: just make price an attribute of beer. </li></ul></ul>
  36. 48. Converting Multiway to 2-Way <ul><li>Baroque in E/R, but necessary in certain “object-oriented” models. </li></ul><ul><li>Create a new connecting E.S. to represent rows of a relationship set. </li></ul><ul><ul><li>E.g., (Joe's Bar, Bud, $2.50) for the Sells relationship. </li></ul></ul><ul><li>Many-one relationships from the connecting E.S. to the others. </li></ul>Bars Beers The- Bar Price The- Beer The- Price BBP
  37. 49. Roles <ul><li>Sometimes an E.S. participates more than once in a relationship. </li></ul><ul><li>Label edges with roles to distinguish. </li></ul>Husband Wife d 1 d 2 d 3 d 4 … … Drinkers Married husband wife
  38. 50. <ul><li>Notice Buddies is symmetric, Married not. </li></ul><ul><ul><li>No way to say “symmetric” in E/R. </li></ul></ul><ul><li>Design Question </li></ul><ul><li>Should we replace husband and wife by one relationship spouse ? </li></ul>Buddy1 Buddy2 d 1 d 2 d 1 d 3 d 2 d 1 d 2 d 4 … … Drinkers Buddies 1 2
  39. 51. More Design Issues <ul><li>1. Subclasses. </li></ul><ul><li>2. Keys. </li></ul><ul><li>3. Weak entity sets. (Next class.) </li></ul>
  40. 52. Subclasses <ul><li>Subclass = special case = fewer entities = more properties. </li></ul><ul><li>Example: Ales are a kind of beer. In addition to the properties (= attributes and relationships) of beers, there is a “color” attribute for ales. </li></ul>
  41. 53. E/R Subclasses <ul><li>Assume subclasses form a tree (no multiple inheritance). </li></ul><ul><li>isa triangles indicate the subclass relation. </li></ul>name manf Beers Ales color isa
  42. 54. Different Subclass Viewpoints <ul><li>1. E/R viewpoint : An entity has a component in each entity set to which it logically belongs. </li></ul><ul><ul><li>Its properties are the union of the properties of these E.S. </li></ul></ul><ul><li>2. Contrasts with object-oriented viewpoint : An object (entity) belongs to exactly one class. </li></ul><ul><ul><li>It inherits properties of its superclasses. </li></ul></ul>name manf Beers Ales color isa Pete’s Ale
  43. 55. Multiple Inheritance <ul><li>Theoretically, an E.S. could be a subclass of several other entity sets. </li></ul>name manf Beers name manf Wines Grape Beers isa isa
  44. 56. Problems <ul><li>How should conflicts be resolved? </li></ul><ul><li>Example: manf means vintner for wines, bottler for beers. What does manf mean for “grape beers”? </li></ul><ul><li>Need ad-hoc notation to resolve meanings. </li></ul><ul><li>In practice, we shall assume a tree of entity sets connected by isa , with all “isas” pointing from child to parent. </li></ul>
  45. 57. Keys <ul><li>A key is a set of attributes whose values can belong to at most one entity. </li></ul><ul><li>In E/R model, every E.S. must have a key. </li></ul><ul><ul><li>It could have more than one key, but one set of attributes is the “designated” key. </li></ul></ul><ul><li>In E/R diagrams, you should underline all attributes of the designated key. </li></ul>
  46. 58. Example <ul><li>Suppose name is key for Beers . </li></ul><ul><li>Beer name is also key for ales. </li></ul><ul><ul><li>In general, key at root is key for all. </li></ul></ul>name manf Beers Ales color isa
  47. 59. Example: A Multiattribute Key <ul><li>Possibly, the combination of hours + room also forms a key, but we have not designated it as such. </li></ul>dept room Courses number hours