13 Entity Relationship Model

13
1 CS 338: Computer Applications in Business: Databases (Fall 2014) ©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.) CS 338: Computer Applications in Business: Databases The Entity- Relationship (ER) Model ©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.) Rice University Data Center Fall 2014 Chapter 7 Announcements Assignment 2 is due on Nov 7 Midterm 2 is on Nov 12 Mid-semester survey Start: what would you like me to start doing to help you excel in the course? Stop: what would you like me to stop doing that is not helping you learn? Continue: what am I doing that is helping you learn? I will try to do more of it. 2

Transcript of 13 Entity Relationship Model

Page 1: 13 Entity Relationship Model

1

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

CS 338: Computer Applications in Business: Databases

The Entity-Relationship (ER) Model

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.) Rice University Data Center

Fall 2014

Chapter 7

Announcements

• Assignment 2 is due on Nov 7

• Midterm 2 is on Nov 12

• Mid-semester survey

• Start: what would you like me to start doing to help you excel in the course?

• Stop: what would you like me to stop doing that is not helping you learn?

• Continue: what am I doing that is helping you learn? I will try to do more of it.

2

Page 2: 13 Entity Relationship Model

2

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Entity Sets

4

Entity type (or entity set)

• Collection (or set) of similar entities that have the same attributes • An entity type describes the schema or intension of a set of entities

that share the same structure

• The collection of entities of a particular entity type is grouped into an

entity set (also known as the extension of the entity type)

• ER model defines entity sets, not individual entities • But entity sets are described in terms of their attributes

Displaying an Entity Type in ER Models

• In ER diagrams, an entity type is displayed as a rectangular box enclosing entity name

• Attributes are displayed as ovals

• Each attribute is connected to its entity type by a straight line

• Composite attributes are attached to their component attributes by straight lines

• Multivalued attributes are displayed in double ovals

5

Page 3: 13 Entity Relationship Model

3

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Sample Database Application: COMPANY Revisited

COMPANY database keeps track of employees, departments and projects

1. Company is organized into DEPARTMENTS Each department has a unique name, a unique number, and a particular employee who

manages the department.

Keep track of the start date when the employee began managing the department.

A department may have several locations

2. A department controls a number of PROJECTS Each project has a unique name, a unique number, and a single location

3. EMPLOYEE: require each employee’s Social Security Number, name, address,

salary, gender, and birth date An employee is assigned to one department, but may work on several projects (which

are not necessarily controlled by the same department)

Keep track of the current number of hours per week that an employee works on each

project

Also keep track of the direct supervisor of each employee (who is another employee)

4. Keep track of the DEPENDENTS of each employee for insurance purposes Keep track of the dependent’s first name, sex, birth date, and relationship to the

employee

Requirements gathered for COMPANY

6

Sample Database Application: COMPANY Revisited

• Based on the requirements, we can identify four initial entity types in the COMPANY database:

• DEPARTMENT

• PROJECT

• EMPLOYEE

• DEPENDENT

7

Page 4: 13 Entity Relationship Model

4

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Initial ER Diagram for COMPANY

• Four entity types

• Most attributes are simple, single-valued, and stored • Works_on and Locations are multivalued • Employee’s name is composite

• Employee has one key, dependent has none, department and project have two keys

12

Weak Entity Types

• Entity types that do not have key attributes of their own • Identified by their relationship to specific entities from another entity type

• Weak entity types are represented by surrounding their boxes and diamonds with double lines

• Identifying relationship

• Relates a weak entity type to the identifying entity, which has the rest of the key 13

• Dependent is meaningless in COMPANY DB independently of Employee

• Identified by relationship to Employee

• Dependent_name distinguishes one dependent from other dependents for the same employee: partial key

Page 5: 13 Entity Relationship Model

5

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Sample Database Application: COMPANY Revisited

• Initial design is typically not complete

• We have not represented that an employee can work on several projects

• We have not represented the number of hours per week an employee works on

• Some aspects in the requirements will be represented as relationships

• Example: attribute Manager of DEPARTMENT refers to an employee who manages the department

• Example: Controlling_department of PROJECT refers to the department that controls the project

• In the ER model, these references should not be represented as attributes but as relationships

14

Relationships in General

Relationship

An association (or interaction) between entities

Indicator: an attribute of one entity refers to another entity

Represent such references as relationships not attributes

Examples:

EMPLOYEE John Smith Works on the ProductX PROJECT

15

Page 6: 13 Entity Relationship Model

6

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Relationships

Relationship

An association (or interaction) between entities

Indicator: an attribute of one entity refers to another entity

Represent such references as relationships not attributes

• Relationship type R among n entity types E1, E2, ..., En

• Defines a set of associations among entities from these entity types

• Relationship instance ri

• Each ri associates n individual entities (e1, e2, ..., en)

• Each entity ej in ri is a member of entity set Ej

• Relationships uniquely identified by keys of participating entities

• Degree of a relationship type

• Number of participating entity types

• e.g., binary, ternary

16

Degree of Relationship Examples

In ER diagrams, we represent the relationship type as follows

• Diamond-shaped box is used to display a relationship type

• Connected to the participating entity types via straight lines

17

Page 7: 13 Entity Relationship Model

7

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Diagramming Relationship Type

• Diamond for relationship type

• Connected to each participating entity type

• Could be binary, ternary, or higher degree

• Remember:

• Represents a set of entities of each type, some of which are related to entities of the other type(s)

• Some entities might participate in several relationships

• Some entities might not participate in the relationship at all

18

19

Degree of Relationship Examples

Page 8: 13 Entity Relationship Model

8

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Relationships with Repeated Entity Sets

20

• Some relationships involve multiple entities from the same entity set • e.g., spouse (two persons), games (two

teams)

• e.g., recursive relationships, such as supervises (two employees)

Recursive relationships

A relationship type with the same participating entity type but in different roles

• Role name

• Signifies role that participating entity plays in relationship instance

• Required when entity type participates multiple times in a relationship

Relationship Constraints

• Cardinality ratio

• Specifies maximum number of relationship instances in which each entity can participate

• Types 1:1, 1:N, or M:N

• Participation constraint

• Specifies whether existence of entity depends on its being related to another entity

• Types: total and partial

• Thus, minimum number of relationship instances in which entities can participate: • At least 1 for total participation

• At least 0 for partial

• Diagrammatically, use a double line from relationship type to entity type

21

Page 9: 13 Entity Relationship Model

9

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Relationship Constraints

• Partial Participation

• If there is no participation constraint, it is possible that an entity will not participate in a relationship set

• Total Participation

• If there is a participation constraint, an entity must participate at least once

• Presented by double line from entity set to relationship set

E1 E2 R

E1 E2 R

22

• Alternative: Structural constraint

• Specify any min and max participation

• Generalization of cardinality ratio and participation constraint

• Replaces cardinality ratio numerals and single/double line notation

• Associate a pair of integer numbers (min, max) with each participation of an entity type E in a relationship type R, where 0 ≤ min ≤ max and max ≥ 1

• max=N finite, but unbounded

23

Relationship Constraints

Page 10: 13 Entity Relationship Model

10

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Relationship Attributes

• Relationship types can also have attributes

• Property that depends on both/all participating entities

• Example: Percentage of control that department has on a project

• Attributes of 1:1 or 1:N relationship types can be migrated to one of the participating entity types

• For a 1:N relationship type, relationship attribute can be migrated only to entity type on N-side of relationship

• Attributes on M:N relationship types must be specified as relationship attributes

24

Percent CONTROLS

Summary of Notation for ER Diagrams

Continued next page…

25

Page 11: 13 Entity Relationship Model

11

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Summary of Notation for ER Diagrams

26

Refining Example ER Design

• Recall preliminary ER design

• Change attributes that reference entity types into relationship types • Weak entities use identifying relationship

• Determine cardinality ratio and participation constraints for each relationship type • Weak entity type always has structural constraint

of (1,1) participation in identifying relationship

27

Page 12: 13 Entity Relationship Model

12

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

28

29

Page 13: 13 Entity Relationship Model

13

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Appropriate ER Model Design

• Choose names that convey the meaning attached to the various constructs

• Nouns give rise to entity type names

• Verbs indicate names of relationship types • Choose binary relationship names to make ER diagram readable from

left to right and from top to bottom

• Review all attributes • Refine into a relationship if attribute references an entity type

• Attribute that exists in several entity types may be better modeled as an independent entity type

• Entities that must participate in a relationship with another entity type and with cardinality constraint of 1 might be better modeled as weak entity

30