CS445 - Introduction to Database Management Systems Fall Semester 2015 LECTURE 6 The Entity-Relationship Model Introduction TEXTBOOK REFERENCE: CHAPTERS 2,3 R&G 1 Review The Big Picture Data Modeling Relational (Mostly Done) E-R Storing Data File Indexes Buffer Pool Management Query Languages SQL (Mostly Done) Relational Algebra (Mostly Done) Query Optimization External Sorting Join Algorithms Query Plans, Cost Estimation 2 1
Steps in Database Design Requirements Analysis User needs; what must database do? Conceptual Design High level description (often done w/er model) Logical Design Translate ER into Relational Schema Schema Refinement Consistency, normalization (eliminates redundancies and related anomalies) Physical Design Consider typical workloads (and sometimes modify database design), select file types and indexes. 3 A Problem with the Relational Model With complicated schemas, it may be hard for a person to understand the structure from the data definition: tables, attributes, keys, and (especially) foreign keys. From assignment #2: Student(sid,s,sex,age,year,gpa) Enroll(sid,grade,d,cno,sectno) Section(d,cno,sectno,p) Prof(p,d) Dept(d,numphds) Course(cno,c,d) What is really going on here? More importantly, how and why was this particular set of schema developed? 4 2
One Solution: The E-R Model Relational model has Tables (relations) with attributes, keys, foreign keys, domain definitions (for attributes) Entity-Relationship model has (in its most basic form) Entities with entity sets, attributes, keys, and domain definitions for attributes Relationships among entities and relationship sets with uniqueness or cardinality constraints. 5 E-R Model: Conceptual Design Define entities and relationships. What information about entities and relationships should be in database? What are the integrity constraints or business rules that hold? A database `schema in the ER Model can be represented pictorially (ER diagrams). Can map an ER diagram into a relational schema, use DDL to enter into DBMS (coming). 6 3
ER Model: Google 9/25/2015 CS445 DATABASES: LECTURE 11 7 ER Model: Scary Often on walls for production databases! 9/25/2015 CS445 DATABASES: LECTURE 11 8 4
ER Model Basics: Use https://www.draw.io/! Entity: Real-world thing, distinguishable from other objects. Entity described by set of attributes Entity Set: A collection of similar entities. e.g., all employees. All entities in an entity set (in boxes) have the same set of attributes (in circles). Each entity set has a key (underlined). Each attribute has a domain (not shown below). ER Diagram UML Equivalent 9 ER Model Basics (Contd.) d did budget Works_In Departments Relationship: Association among two or more entities. E.g., Lane works in Math and CS Department. relationships can have their own attributes. Relationship Set: Collection of similar relationships 10 5
Example: The E-R Model Works_In Relationship Set Departments Entity Set d did budget Works_In Departments Entity Set 11 Instance: Works_In Relationship Set What information do we need to record a relationship? Must indicate which employee and which department we want connected. That is, we need the key value for an employee and the key value for the department! 1/1/14 123-22-3666 231-31-5368 131-24-3650 223-32-6316 3/3/14 2/2/15 3/1/15 9/23/15 51 56 60 Works_In Departments 12 6
Cardinality Constraints on Relationship Types How many entities can participate? x..y means at least x and at most y * means many did d budget 0..* Works_In 0..1 Departments A given department can have 0 or many employees. A given employee can work in 0 or 1 department. 13 Choosing Cardinality Constraints The correct choice of cardinalities depends on the application requirements Which of the following is correct??? 1..1 0..* Employee Works in Department 0..* 0..1 Employee Works in Department One to many: 1..1 0..* Many to one: 0..* 0..1 0..* 1..* Employee Works in Department Many to many: 0..* 1..* 14 7
Key Constraint Bad terminology! Doesn t necessarily relate to keys! Instead, a key constraint limits participation to at most 1 entity. 0..* 0..1 Employee Works in Department Employee Works in Department Text uses different notation: A key constraint (arrow) limits participation to at most 1 entity. (That is, each Employee entity can participate in at most one Works in relationship.) 15 Participation Constraint A participation constraint forces entities to participate in relationships. 1..1 0..1 Employee manages Department Employee manages Department Text uses different notation: A participation constraint (bold line) limits participation to at least 1 entity. (That is, each Department entity must participate in at least one manages relationship.) 16 8
Key and Participation Constraints Both constraints can be mixed. When neither is used 0..* is assumed. 1..1 0..1 Employee manages Department Employee manages Department Each employee can be managed by at most one department (arrow limits to one, not bold may be zero) Each Department is managed by one and only one employee (arrow limits to one, bold forces at least one) 17 Practice A club has a, office, and phone and is uniquely identified by its Clubs sponsor various events, and each event has one main sponsor but may have other co-sponsors Each event has a title, date, location, and description, and is uniquely identified by the title and date Draw the ER diagram in standard notation, showing all key and participation constraints. 9/25/2015 CS445 DATABASES: LECTURE 11 18 9
Relationship types can have role s did d budget Employee 1..1 0..1 manages managed-by manages Department 19 Key and Participation Constraints Summary Using the text s notation, we can have both the Manages and Works_In relationships in one ER diagram: did d budget Manages Departments Means: at least one Works_In Means: exactly one 20 10
ER Model Basics: Another Example Same entity set can participate in different relationship sets, or in different roles in the same set. did d budget subordinate supervisor Departments Works_In Reports_To 21 Weak Entities A weak entity can be identified uniquely only by considering the primary key of another (owner) entity. Owner entity set and weak entity set must participate in a one-tomany relationship set (one owner, many weak entities). Weak entity set must have total participation in this identifying relationship set. Owner entity set Exactly one: Participation Weak entity set and key constraint! cost p age Policy Dependents Weak entities have only a partial key (dashed underline) 22 11
Weak Entities: UML policy 1..1 0..* Dependents p age cost 23 12