Data modeling using the entity-relationship model Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.) • Data modeling • Entity-relationship model - Entity types - strong entities - weak entities - Relationships among entities - Attributes - attribute classification - Constraints - cardinality constraints - participation constraints • ER-to-Relation-mapping Sept. 2012 Yangjun Chen ACS-3902 1 Data modeling using the entity-relationship model Entity-relationship model (ER model) P. 41-2: “This model and its variations are frequently used for the conceptual design of database applications, and many database design tools employ its concepts.” ER model: • is used to create a conceptual data model that reflects all the user data requirements. •It includes detailed descriptions of •entity types, •relationships, and •constraints •no implementation details so can be used for communication with non-technical users Sept. 2012 Yangjun Chen ACS-3902 2 Data modeling using the entity-relationship model Example The company database keeps track of a company’s employees, departments, and projects: Requirements: concerning the department: 1. 2. 3. 4. 5. company is organized into departments a department has a unique name, a unique number, and a specific employee is its’ manager we track the start date for the manager function a department may be in several locations a department controls a number of projects concerning the project: 6. a project has a unique name, a unique number, and is in a single location Sept. 2012 Yangjun Chen ACS-3902 3 Data modeling using the entity-relationship model example continued concerning the employee: 7. each employee has a name, social insurance number, address, salary, sex, and birth date 8. an employee is assigned to one department but may work on several projects which are not necessarily controlled by the same department 9. we track the number of hours per week that an employee works on each project 10. we keep track of the direct supervisor of each employee 11. we track the dependents of each employee (for insurance purposes) concerning the dependent: 12. we record each dependent’s first name, sex, birth date, and relationship to the employee Sept. 2012 Yangjun Chen ACS-3902 4 Data modeling using the entity-relationship model The entities: employee dependent department project Sept. 2012 Yangjun Chen ACS-3902 5 Data modeling using the entity-relationship model The entities: fname minit lname name sex ssn address dependent salary name employee sex birthdate relationship bdate name number location department Sept. 2012 Yangjun Chen project name number ACS-3902 location 6 Data modeling using the entity-relationship model With relationships: 1 N department works for 1 1 employee supervisee manages 1 supervision controls N supervisor N 1 N M works on 1 project dependents of N partial constraint total constraint dependent Sept. 2012 Yangjun Chen ACS-3902 7 Data modeling using the entity-relationship model name number With attributes, etc: fname name ssn bdate degree 1 lname minit sex 1 startdate employee supervisor department works for N salary address 1 location 1 controls manages 1 N hours N supervisee number of employees N M 1 project works on supervision dependents of name number location N Figure 3.2 dependent name Sept. 2012 sex birthdate relationship Yangjun Chen ACS-3902 P.46 8 Data modeling using the entity-relationship model Let’s consider a university environment: At a high level, we need to: •offer courses •register students •assign instructors to courses •assign grades •anything else? Sept. 2012 Yangjun Chen ACS-3902 9 Data modeling using the entity-relationship model Concepts (you should understand) Attribute classification •null values •simple attribute •multivalued attribute not applicable, unknown, missing •composite attribute •complex (nested) attributes •key attribute •domain Attribute storage •stored & derived attribute Sept. 2012 Yangjun Chen ACS-3902 10 Data modeling using the entity-relationship model Concepts (you should understand) Relationships •degree of a relationship •recursive relationship •role names •constraints cardinality: m-n, 1-n, 1-1 participation: partial, total (existence dependency) Sept. 2012 Yangjun Chen ACS-3902 11 Data modeling using the entity-relationship model Entities •entity type •strong entity - key attribute - entity has a key attribute or a combination of attributes which can be used as a key. •weak entity No key attributes. Entities belonging to a weak entity type are identified by being related to specific entities from another entity type in combination with some of their attribute values. - identifying owner - identifying relationship - partial key Sept. 2012 Yangjun Chen ACS-3902 12 Data modeling using the entity-relationship model Instructors: let’s assume this classification includes instructors, professors, part-time people (at least for now). These people have SINs, employee numbers, names, addresses, offices, phones, ... employee no SIN name address instructor office degree Sept. 2012 phone Yangjun Chen ACS-3902 13 Data modeling using the entity-relationship model Is there a key attribute? What are the domains? Can any attribute be null? Is any attribute composite, derived, complex, multivalued? employee no SIN name address instructor office degree phone Is this a weak entity or a strong entity? Should department be an attribute? Sept. 2012 Yangjun Chen ACS-3902 14 Data modeling using the entity-relationship model Departments: obviously instructors are employed by the University and associated with a department A department has a name, number, office, chair, ... name Dnumber office department phone chair Sept. 2012 Yangjun Chen ACS-3902 15 Data modeling using the entity-relationship model Is there a key attribute? What are the domains? Can any attribute be null? Is any attribute composite, derived, complex, multivalued? name Dnumber office department phone chair Should chair be an attribute, or is there a relationship between two entity types? Sept. 2012 Yangjun Chen ACS-3902 16 Data modeling using the entity-relationship model Employs relationship: If we assume the relationship between department and instructor is 1:N then we only associate each department with a single instructor, but we associate any number of instructors with a single department department 1 employs N instructor 1:N is the cardinality of the relationship the relationship is of degree 2; it is a binary relationship Both entities are considered strong entities Sept. 2012 Yangjun Chen ACS-3902 17 Data modeling using the entity-relationship model Consider some instances department employs d1 e1 e2 e3 e4 e5 e6 e7 d2 d3 Sept. 2012 instructor Yangjun Chen ACS-3902 18 Data modeling using the entity-relationship model Chair relationship: A department has a chair who has special responsibilities. One person (instructor) is designated as such. department 1 1 chair instructor 1:1 is the cardinality of the relationship the relationship is of degree 2; it is a binary relationship. Sept. 2012 Yangjun Chen ACS-3902 19 Data modeling using the entity-relationship model Continuing the discussion on data modeling •Weak entity again •Data analysis (at entity instance level) •Mapping ER to relational database schema •ER model comparison Sept. 2012 Yangjun Chen ACS-3902 20 Data modeling using the entity-relationship model Weak entity types a weak entity does not have a key of its own - may have a partial key the identifying relationship will have total participation for the weak entity e.g. consider courses and sections at UWinnipeg Sept. 2012 Yangjun Chen ACS-3902 21 Data modeling using the entity-relationship model Consider courses and course sections In the fall and winter we have: 91.1453/3-001 F Intro Computers staff MW 16:30-17:45 3C13 .. 91.1453/3-002 W Intro Computers staff MW 16:30-17:45 3C13 .. 91.1453/3-050 F Intro Computers staff T 18:00-21:00 3C13 .. 91.1453/3-051 W Intro Computers staff T 18:00-21:00 3C13 .. Section numbers are 001, 002, 050, 051, … Sections have a section number, a term, days and times, … Sept. 2012 Yangjun Chen ACS-3902 22 Data modeling using the entity-relationship model Consider courses and course sections name term course no Section no course 1 Offered-in N section description credit hours meeting Section is a weak entity - it has a discriminator (partial key), section number. Section totally participates in the offered in relationship PK (primary key) of Section is … (offered_in is an identifying relationship) Is meeting multivalued? Sept. 2012 Yangjun Chen ACS-3902 23 Data modeling using the entity-relationship model Data analysis: instructor m teaches n course n m textbook uses Note that teaches and uses are both binary relationships: •we expect situations where a specific Instructor teaches a specific Course, and where •a specific Course uses a specific text Sept. 2012 Yangjun Chen ACS-3902 24 Data modeling using the entity-relationship model Consider instances instructor Sept. 2012 course Yangjun Chen textbook ACS-3902 25 Data modeling using the entity-relationship model instructor course Z Intro to X jones Intro to Y smith Advanced X Courses offered are Intro to X, Intro to Y, Z, Advanced X, Advanced Y Advanced Y Sept. 2012 Yangjun Chen ACS-3902 26 Data modeling using the entity-relationship model Consider instances of Instructor teaches Course instructor teaches course Z Intro to X jones •Jones teaches Intro to X Intro to Y smith Advanced X Advanced Y Sept. 2012 Yangjun Chen ACS-3902 27 Data modeling using the entity-relationship model instructor teaches course Z Intro to X jones •Jones teaches Intro to X Intro to Y smith Advanced X Advanced Y There is a relationship between Instructor Jones and Course Intro to X Sept. 2012 Yangjun Chen ACS-3902 28 Data modeling using the entity-relationship model instructor teaches course Z Intro to X jones •Jones teaches Intro to X Intro to Y smith Advanced X Advanced Y This line connects the three dots Sept. 2012 Yangjun Chen ACS-3902 29 Data modeling using the entity-relationship model instructor teaches course Z Intro to X jones Intro to Y smith •Jones teaches Intro to X •Jones teaches Advanced X Advanced X Advanced Y Sept. 2012 Yangjun Chen ACS-3902 30 Data modeling using the entity-relationship model instructor teaches course Z Intro to X jones Intro to Y •Jones teaches Intro to X •Jones teaches Advanced X •Smith teaches Intro to Y smith Advanced X Advanced Y Sept. 2012 Yangjun Chen ACS-3902 31 Data modeling using the entity-relationship model instructor teaches course Z Intro to X jones Intro to Y •Jones teaches Intro to X •Jones teaches Advanced X •Smith teaches Intro to Y smith Advanced X •Smith teaches Advanced Y Advanced Y Sept. 2012 Yangjun Chen ACS-3902 32 Data modeling using the entity-relationship model instructor teaches course Z Intro to X jones Intro to Y •Jones teaches Advanced X •Smith teaches Intro to Y smith Advanced X Advanced Y Sept. 2012 •Jones teaches Intro to X Yangjun Chen ACS-3902 •Smith teaches Advanced Y •Smith and Jones teach Z together There are two relationships: one between Jones and Z; the other between Smith and Z 33 Data modeling using the entity-relationship model Now let us examine Course uses Textbook instructor teaches course uses Z Intro to X jones textbook UML distilled Intro to Y smith Advanced X Advanced Y The mythical man-month Suppose we have two textbooks: The mythical man-month, and UML distilled Sept. 2012 Yangjun Chen ACS-3902 34 Data modeling using the entity-relationship model instructor teaches course uses Z Intro to X jones textbook UML distilled Intro to Y smith Advanced X The mythical man-month Advanced Y Intro to Y uses The mythical man-month Sept. 2012 Yangjun Chen ACS-3902 35 Data modeling using the entity-relationship model instructor teaches course uses Z Intro to X jones textbook UML distilled Intro to Y smith Advanced X The mythical man-month Advanced Y Z uses both texts Sept. 2012 Yangjun Chen ACS-3902 36 Data modeling using the entity-relationship model Let’s jump ahead to see what relations we eventually create Section 9.1: ER-to-Relational mapping 1. Create a relation for each strong entity type 2. Create a relation for each weak entity type •include primary key of owner (an FK - foreign key) •owner’s PK + partial key becomes PK 3. For each binary 1:1 relationship choose an entity and include the other’s PK in it as an FK. Include any attributes of the relationship 4. For each binary 1:n relationship, choose the n-side entity and include an FK w.r.t the other entity. Include any attributes of the relationship Sept. 2012 Yangjun Chen ACS-3902 37 Data modeling using the entity-relationship model 5. For each binary M:N relationship, create a relation for the relationship •include PKs of both participating entities and any attributes of the relationship •PK is the concatenation of the participating entity PKs 6. For each multivalued attribute create a new relation •include the PK attributes of the entity type •PK is the PK of the entity type and the multivalued attribute 7. For each n-ary relationship, create a relation for the relationship •include PKs of all participating entities and any attributes of the relationship •PK may be the concatenation of the participating entity PKs Sept. 2012 Yangjun Chen ACS-3902 38 Data modeling using the entity-relationship model 1 N department works for 1 1 employee supervisee 1 supervision 1 controls N supervisor N manages N M works on 1 project dependents of N dependent Sept. 2012 Yangjun Chen ACS-3902 39 Data modeling using the entity-relationship model Consider the following requirements (very typical situation) A company may be composed of Divisions, each of which may be composed of Departments, each of which may be composed of Groups. We could model this as follows: group n dep composed of 1 department n 1 div composed of division n c composed of Sept. 2012 Yangjun Chen ACS-3902 1 company 40 Data modeling using the entity-relationship model It fits the requirements, but •it is not flexible •it does not accommodate any new groupings •it does not allow the company to grow/change •it is very rigid => it is a static model group n dep composed of 1 department n 1 div composed of division n c composed of Sept. 2012 Yangjun Chen ACS-3902 1 company 41 Data modeling using the entity-relationship model Consider the following model •it is flexible •it does accommodate any new groupings •it does allow the company to grow/change •it is not rigid => it is a dynamic model •but not all constraints are implied in the model => we would need supporting application code. What are these constraints? composed of m n higher_level higher_level Sept. 2012 Yangjun Chen lower_level ACS-3902 organization 42 Data modeling using the entity-relationship model Consider the following model •it is flexible, it does accommodate any new groupings, it does allow the company to grow/change, it is a dynamic model again •not all constraints are implied in the model => we would need supporting application code. What are these constraints? Constraint-des n composed of org composed of m higher_ level Sept. 2012 m lower_level organization type n lower_level higher_level organization m Yangjun Chen example of ACS-3902 n 43 Data modeling using the entity-relationship model organization org-name Dell sell-division ... ... Composed-of sub-org org Sell-division Dell Dell Production-division ... ... ... ... ... ... ... ... Example-of org org-type company Dell sell-division division ... ... organization-type org-type company division ... ... org-composed-of sub-org-type org-type company division division department ... ... Sept. 2012 Yangjun Chen ACS-3902 constraint 1:n 1:n 44 Data modeling using the entity-relationship model Other common situations: •How would we model bill-of-materials? •How would we model reporting relationships? •How would we model geographical relationships? •How would we model procedures and activities? •How would we model purchase orders? •How would we model documents? … with indexes? … with versions? Sept. 2012 Yangjun Chen ACS-3902 45