3-1 chapter 3 data modeling using the entity-relationship (er) model 3.1 using high-level conceptual...

37
3-1 3-1 Chapter 3 Data Modeling Using the Entity- Relationship (ER) Model 3.1 Using High-Level Conceptual Data Mode ls for Database Design 3.2 An Example Database Application 3.3 Entity types, Entity Sets, Attributes, and Keys 3.4 Relationships, Relationship Types, Ro les, and Structural Constraints 3.5 Weak Entity Types 3.6 Refining the ER Design for the COMPAN Y Database 3.7 ER Diagrams, Naming conventions, and Design Issues

Upload: benedict-ray

Post on 29-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

3-1 3-1

Chapter 3

Data Modeling Using the Entity-Relationship

(ER) Model

3.1 Using High-Level Conceptual Data Models for Database Design3.2 An Example Database Application3.3 Entity types, Entity Sets, Attributes, and Keys3.4 Relationships, Relationship Types, Roles, and Structural Constraints3.5 Weak Entity Types3.6 Refining the ER Design for the COMPANY Database3.7 ER Diagrams, Naming conventions, and Design Issues3.8 Summary

3-2 3-2

•Data types

•Relationships

•constraints

Figure 3.1 Phases of Database Design (Simplified)

User-defined operations(transactions)Data flow diagrams

3-3 3-3

Requirements for the COMPANY Database:

- The company is organized into DEPARTMENTs. Each department has a unique name, a unique number, and an employee who manages the department. We keep track of the start date of the department manager. A department may have several locations.

- Each department controls a number of PROJECTs. Each project has a unique name, a unique number and is located at a single location.

- We store each EMPLOYEE’s name, social security number, address, salary, sex, and birth date. Each employee works for one department but may work on several projects. We keep track of the number of hours per week that an employee currently works on each project. We also keep track of the direct supervisor of each employee.

- Each employee may have a number of DEPENDENTs. For each dependent, we keep their name, sex, birth date, and relationship to the employee.

3.2 An Example Database Application: COMPANY3.2 An Example Database Application: COMPANY

3-4 3-4

Figure 3.2 ER schema diagram for COMPANY database

3-5 3-5

ER Model Concepts entity data relationship attribute

3.3.1 Entities and Attributes

- Entities are specific objects or things in the mini-world that are represented in the database; for example, the EMPLOYEE John Smith,

the Research DEPARTMENT, the ProductX PROJECT.

- Attributes are properties used to describe an entity; for example, an EMPLOYEE entity may have a Name, SSN, Address, Sex, BirthDate.

- A specific entity will have a value for each of its attributes; for example a specific employee entity may have Name=‘John Smith’, SSN=‘123456789’, Address=‘731 Fondren, Houston, TX’, Sex=‘M’, BirthDate=’09-JAN-55’.

Independent existence

3.3 Entity types, Entity Sets, Attributes, and Keys3.3 Entity types, Entity Sets, Attributes, and Keys

3-6 3-6

Figure 3.3 Two entities and their attribute values

employee department

3-6a 3-7

Types of Attributes:

• Simple (Atomic): Each entity has a single atomic value for the attribute; for example SSN or Sex. (不可分 )

• Composite: The attribute may be composed of several components;

for example Address(Apt#, House#, Street, City, State, ZipCode, County) or Name (FirstName, MiddleName, LastName) . Composition may form a hierarchy where some components are themselves composite. (see 3-9)

(1)

(2)• Single-valued: single value for that entity (e.g., age)

• Multi-valued: An entity may have multiple values for that attribute; for example Color of a CAR or PreviousDegrees of a STUDENT. Denoted as {Color} or {PreviousDegrees}. (may have lower and upper bounds)

3-6a 3-8

-In general, composite and multi-valued attributes may be nested arbitrarily to any number of levels although this is rare. For example, PreviousDegrees of a STUDENT is a composite multi-valued attribute denoted by {PreviousDegrees (College, Year, Degree, Field)}.

multi-valued composite

(3) • stored

• derived

Age (derived attribute) is derivable from BirthDate (stored attribute), I.e., Current Date-BirthDate

(4) Null Values

not applicable, e.g., Apartment Number, College Degree

unknown

- the attribute value exists but is missing

e.g., Height

- it is not known whether the value exists

e.g., HomePhone

3-6b 3-10

Figure 3.4 A hierarchy of composite attributes

Null

77001TexasHouston

Kirby2311

User sometimes refers to composite attribute, but at other times refers to its components.

3-6c 3-11

3.3.2 Entity Types, Entity Sets, Keys, and Value Sets3.3.2 Entity Types, Entity Sets, Keys, and Value Sets

Entities with the same basic attributes are grouped or typed into an entity type. For example, the EMPLOYEE entity type or the PROJECT entity type (see 3-12) entity type (intension) entity set (extension)

An attribute of an entity type for which each entity must have a unique value is called a Key attribute of the entity type. For example SSN of EMPLOYEE. key (uniqueness) constraint

A key attribute may be composite. For example, VehicleRegistrationNumber is a key of the CAR entity type with components (Number, State).

An entity type may have more than one key. For example, the CAR entity type may have two keys: VehicleIdentificationNumber and VehicleRegistraionNumber(Number, State). (see 3-13)

3-8 3-12

Figure 3.6 Two entity types and some of the member entities of each

3-9 3-13

Figure 3.7 CAR entity type

Component of composite attribute

multivalued attribute

Registration attributeVehicleID attribute key attribute

Uniqueness property must hold for every extension of the entity set

3-7 3-14

EMPLOYEEBdateSalary

Address

SexName

SSN

LnameMinitFname

Department

Locations

Number

Name

NumberOfEmployee

Entity type

singled

multiple-valued

key attribute

derived attribute

3-10 3-15

Value Sets (Domains) of AttributesValue Sets (Domains) of Attributes

Each simple attribute of an entity type is associated with a value set (or domain of values). e.g. Age:16-70, Name: char string, … An attribute A of entity type E whose value set is V: e.g. V={1,2} A: E P(V) power set P(V)={,{1},{2},{1,2}} null empty set singled-valued attribute singleton multivalued attribute no restriction

For a composite attribute A, the value set V is

Value sets are not displayed in ER diagram.

)(...)()( 21 nVPVPVP

3-11 3-16

Figure 3.5 A complex attribute AddressPhone with multivalued and composite component.

{AddressPhone({Phone(AreaCode, PhoneNumber)}, Address(StreetAddress(Number, Street, ApartmentNumber), City, State, Zip))}

( ): composite attribute{ }: multivalued attribute

An employee may have more than one AddressPhone, i.e., office and home.

3-12 3-17

Refer to Fig. 3.2 (3-4)

3.3.3 Initial Conceptual Design of the COMPANY Database3.3.3 Initial Conceptual Design of the COMPANY Database

Specification (refer to 3-3)

Figure 3.8 { …. } : multivalued attribute ( ) : component of a composite attribute

key attribute

key attribute

3-13 3-18

3.4 Relationships, Relationship Types, 3.4 Relationships, Relationship Types,

Roles, and Structural Constrains Roles, and Structural Constrains

- A relationship relates two or more distinct entities with a specific meaning; for example, EMPLOYEE John Smith work on the ProductX PROJECT or EMPLOYEE Franklin Wong work for the Research DEPARTMENT. (see 3-19)

- Relationships of the same type are grouped or typed into a relationship type. For example, the WORKS_ON relationship type in which EMPLOYEEs and PROJECTs participate, or the MANAGES relationship type in which EMPLOYEEs and DEPARTMENTs participate.

DEPARTMENT

EMPLOYEE

PROJECT

MANAGES

WORKS_ON

3.4.1 Relationship Types, Sets and Instances

relationship type

nEEER 21 :),,,( 21 Reee n relationship instance

3-14 3-19

Figure 3.9 Some instances of the WORKS_FOR relationship many-to-one

3-13 3-20

3.4.2 Relationship Degree, Role Names, and Recursive Relationships

-The degree of a relationship type is the number of participation entity types. Both MANAGES and WORKS_ON are binary relationships. ternary relationship: degree 3 (see 3-21)

-More than one relationship type can exist with the same participation entity types; for example, MANAGES and WORKS_FOR are distinct relationships between EMPLYEE and DEPARTMENT participate.

-Relationship as Attributes WORKS_FOR relationship DEPARTMENT OF EMPLOYEE (see 3-17)

EMPLOYEES OF DEPARTMENT : multivalued

DEPARTMENT

EMPLOYEE

WORKS_FOR MANAGES

3-15 3-21

Figure 3.10 The ternary relationship SUPPLY s supplies part p to project j

3-13 3-22

-Role Names and Recursive Relationships.

- A relationship can relate two entities of the same entity type; for example, a SUTERVISION relationship type relates one EMPLOYEE (in the role of supervisee ) to another EMPLOYEE (in the role of supervisor). This is called a recursive relationship type. (see 3-23)

For example: in the WORKS_FOR relationship type

EMPLOYEE WORKS_FOR DEPARTMENTrole: employee / worker department / employer

3-17 3-23

e 2 e 3

e 1

e 6 e 7

e 4

e 5

3-16 3-24

- Cardinality ratio ( of a binary relationship): 1:1, 1:N, N:1, or M:N MANAGES (1:1, see 3-25) WORKS_FOR (1:N, see 3-19) WORKS_ON ( M:N, see 3-26) - Participation constraint(on each participation entity type): total (called existence dependency) or partial.

Every employee must work for a department.

EMPLOYEE in WORKS_FOR : total

total participation (完全參與 ) : double line

partial participation (部分參與 ) : single line

structural constraints

3.4.3 Constrains on Relationship Types

3-18 3-25

Figure 3.12

MANAGES one-to-one 1:1 relationship

Partial participation of EMPLOYEE Total participation of DEPARTMENT

3-19 3-26

Figure 3.13 WORK_ON M:N relationship

3-20 3-27

- A relationship type can have attributes; for example, HoursPerWeek of WORKS_ON; its value for each relationship instance describes the number of hours per week that an EMPLOYEE works on a PROJECT.

WORKS_ONEMPLOYEE PROJECT

HOUR

3.3.4 Attributes of Relationship Types

3-20 3-28

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

MANAGESEMPLOYEE DEPARTMENT

StartDate

1 1

- For M:N relationship types, some attributes may be determined by the combination of participating entities. must be specified as relationship attributes.

WORKS_ONEMPLOYEE PROJECT

HOURS

M N

3-20 3-29

- For a 1:1 relationship type, a relationship attribute can be migrated to either participating entity types.

- For a 1:N relationship type, a relationship attribute can be migrated only to the entity type at the N side.

WORKS_FOREMPLOYEE DEPARTMENT

StartDate

N 1

3-21 3-30

3.5 Weak Entity Types 3.5 Weak Entity Types (vs. Strong Entity types)

- An entity type that does not have a key attribute

- A weak entity type must participate in an identifying relationship type with an owner or identifying entity type.

- Entities are identified by the combination of : A partial key of the weak entity type set of attributes can uniquely identify weak entities that are related to the

same owner entity

The particular entity they are related to in the identifying entity type

3-21 3-31

Example:

Suppose that a DEPENDENT entity is identified by the dependent’s first name and birthdate, and the specific EMPLOYEE that the dependent is related to.

DEPENDENT is a weak entity type with EMPLOYEE as its identifying entity type via the identifying relationship type DEPENDENT_OF.

A weak entity type always has a total participation constraint

with respect to its identifying relationship.

(existence dependency)

3-22 3-32

3-223.6 Refine the ER Design for 3.6 Refine the ER Design for COMPANYCOMPANY Database Database

1.MANAGES, a 1:1 relationship type between EMPLOYEE and DEPARTMENT.

EMPLOYEE participation is partial. DEPARTMENT participation is not clear from the requirements. We question the users, who say that a department must have a manager at all times,which implies total participation.The attribute StartDate is assigned to this relationship type.

2.WORKS_FOR, a 1:N relationship type between DEPARTMENT and EMPLOYEE. Both participations are total.

3.CONTROLS, a 1:N relationship type between DEPARTMENT and PROJECT. The participation of PROJECT is total,whereas that of DEPARTMENT is determined to be partial, after consultation with the users.

3-22 3-33

4.SUPERVISION, a 1:N relationship type between EMPLOYEE ( in the supervisor role) and EMPLOYEE (in the supervisee role). Both participations are determined to be partial, after the users indicate that not every employee is a supervisor and not every employee has a supervisor.

5.WORKS_ON,determined to be an M:N relationship type with attribute Hours, after the users indicate that a project can have several employees working on it. Both participations are determined to be total.

6.DEPENDENTS_OF, a 1:N relationship type between EMPLOYEE and DEPENDENT, which is also the identifying relationship for the weak entity type DEPENDENT,which is also the identifying relationship for the weak entity type DEPENDENT. The participation of EMPLOYEE is partial, whereas that of DEPENDENT is total.

3-23 3-34

Figure 3.14Summary of ER diagram notation

for each entity e in E, e must participate in at least min and at most max relationship instances in R at all times.

3-23a 3-35

Alternative (min, max) notation for relationship structural constraints:

-Specified on each participation of an entity type E in a relationship type R.- Specifies that each entity e in E participates in at least min and at most max relationship instances in R.- Default (no constraint): min=0, max=n.

-- Must have- Derived from the mini-world constraints.

Examples:(a) A department has exactly one manager and an employee can manage at most one department.- Specify (0,1) for participation of EMPLOYEE in MANAGES- Specify (1,1) for participation of DEPARTMENT in MANAGES

(b) An employee can work for exactly one department but a department can have any number of employees.- Specify (1,1) for participation of EMPLOYEE in WORKS_FOR- Specify (0,n) for participation of DEPARTMENT in WORKS_FOR

.1max,0minmax,min

min=0 partial participation min>0 total participation

22

11

,

,

ed

ed

3e

3

52

42

31

21

11

,

,

,

,

,

d

ed

ed

ed

ed

ed

3-23b 3-36

Proper Naming of Schema Constructs

Figure 3.14 ER diagram for the COMPANY Scheme

3-24 3-37

3.7.2 Proper Naming of Schema Constructs3.7.2 Proper Naming of Schema Constructs

names for

entity typesattributesrelationship typesroles

nouns

verbs

3.7.3 Design Choices for ER Conceptual Design3.7.3 Design Choices for ER Conceptual Design

1. A concept may be first modeled as an attribute and then refined into a relationship.

2. An attribute that exists in several entity types may be refined into its own independent entity type.