Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of...

51
Entity-Relationship (ER) Data Model 概概概概概概 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Transcript of Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of...

Page 1: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Entity-Relationship (ER) Data Model

概念資料模式

(Based on Chapter 3 in Fundamentals of Database Systems

by Elmasri and Navathe, Ed. 4)

Page 2: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

2

Outline1 Using High-Level Conceptual Data Models for

Database Design2 An Example Database Application (COMPANY)3 ER Model Concepts

3.1 Entities and Attributes3.2 Entity Types, Value Sets, and Key

Attributes3.3 Relationships and Relationship Types3.4 Structural Constraints and Roles3.4 Weak Entity Types

4 ER Diagrams Notation5 Relationships of Higher Degree

Page 3: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

3

2 COMPANY Database Requirements for the COMPANY Database:

(需求的文字敘述 ) The company is organized into

DEPARTMENTs. Each department has a name, 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.

Page 4: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

4

Each department controls a number of PROJECTs.

Each project has a name, number, and is located at a single location.

Page 5: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

5

We store each EMPLOYEE's 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.

Page 6: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

6

Each employee may have a number of DEPENDENTs.

For each dependent, we keep their name, sex, birth date, and relationship to the employee.

Page 7: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

7

3 ER Model Concepts (將文字敘述化為圖形表示 ) Entities (實體 )and Attributes(屬性 ) Entity Types, Value Sets, and Key

Attributes Relationships(關係 ) and

Relationship Types Structural Constraints(結構限制 )

and Roles(角色 ) Weak Entity Types(弱實體 )

Page 8: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

8

Entities An entity is a “thing” in the real world

with an independent existence(獨立存在 ) ( conceptual or physical ) .

Entities are specific objects or things in the mini-world that are represented in the database;

for example the EMPLOYEE John Smith, the DEPARTMENT Research, the PROJECT ProductX.

Page 9: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

9

Attributes Attributes(屬性 ) are properties

(特性 ) used to describe an entity; for example an EMPLOYEE entity

may have a Name, SSN, Address, Sex, BirthDate.

屬性的集合可以表示一個實體 實體以黑點表示

Page 10: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

10

Page 11: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

11

Types of Attributes Simple(單一 ) versus Composite(組合 )

Single-value (單值 )versus Multi-valued(多值 )

Stored (儲存 )versus Derived(推導 )

Page 12: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

12

Simple Attributes: Each entity has a single atomic

value for the attribute; for example SSN or Sex.

Page 13: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

13

Composite Attributes: The attribute may be composed of

several components; for example

Address(Apt#, House#, Street, City, State, ZipCode, Country) or

Name(FirstName, MiddleName, LastName).

Page 14: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

14

Single-valued Attributes Most attributes have a single value

for a particular entity; such attributes are called single-valued.

For example, Age is a single-valued attribute of person.

Page 15: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

15

Multi-valued Attributes: 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}.

Page 16: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

16

Stored VS. Derived Attributes

In some cases two (or more) attribute values are related.

Some attribute values can be derived from related entities.

For example, The value of Age can be determined

from the current date and the value of that person’s BirthDate.

Page 17: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

17

Null Values In some case a particular entity

may not have an applicable value for an attributes.

For such situations, a special value called null is created.

Page 18: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

18

Entity Types(實體型態 ) 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.

ER Model 不對個別實體描述 ,只對型態描述

Page 19: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

19

Entity Sets The collection of all entities of a

particular entity type in the database at any point in time is called an entities set.

entities set 就是 entity type.

Page 20: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

20

Page 21: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

21

Key Attributes of an Entities Type

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.

Page 22: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

22

A key attribute may be composite. For example,

VehicleRegistrationNumber is a key of the CAR entity type with components (Number, State).

Page 23: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

23

An entity type may have more than one key.

For example, the CAR entity type may have two keys: VehicleIdentificationNumber and VehicleRegistrationNumber(Number,

State).

Page 24: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

24

Value Sets (Domains) of Attributes

Each simple attributes of an entity type is associated with a value set (or domain of values), which specifies the set of values that may be assigned to that attribute for each individual entity.

Page 25: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

25

Relationships(關係 ) A relationship relates two or

more distinct entities with a specific meaning;

For example, EMPLOYEE John Smith works on the

ProductX PROJECT or EMPLOYEE Franklin Wong manages

the Research DEPARTMENT.

Page 26: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

26

Relationship Types 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.

Page 27: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

27

Relationship Types A relationship type R among n

entity types E1, E2,…,En defines a set of associations – or a relationship set – among entities from these types.

ER Model只描述關係型態(Relationship type),不描述個別的關係

Page 28: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

28

Degree of a Relationship Type The degree of a relationship type

is the number of participating entity types.

Both MANAGES and WORKS_ON are binary relationships.

Page 29: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

29

Relationship types More than one relationship type

can exist with the same participating entity types;

for example, MANAGES and WORKS_FOR are distinct relationships between EMPLOYEE and DEPARTMENT participate.

Page 30: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

30

Page 31: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

31

Relationships of Higher Degree

Relationship types of degree 2 are called binary

Relationship types of degree 3 are called ternary and of degree n are called n-ary

In general, an n-ary relationship is not equivalent to n binary relationships

Page 32: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

32

Page 33: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

33

Roles Names( 當實體在關係中的角色不清楚的時候用 ) Each entity type that participates in

a relationship type plays a particular role in the relationship.

The role name signifies the role that a participating entity from the entity type plays in each relationship instance, and helps to explain what the relationship means.

Page 34: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

34

Roles Name For example,

in the WORK_FOR relationship type, EMPLOYEE plays the role of

employee or worker and DEPARTMENT plays the role of

department or employer.

Page 35: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

35

Recursive(自身 ) Relationships A relationship can relate two entities

of the same entity type ; (使用角色 ) for example,

a SUPERVISION 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.

Page 36: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

36

Page 37: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

37

Structural constraints on relationships: Cardinality ratio (of a binary

relationship): 1:1, 1:N, N:1, or M:N.

Participation constraint (on each participating entity type): total (called existence

dependency ) or partial.

Page 38: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

38

Cardinality ratios for Binary Relationships. The cardinality ratio(對應的個數 )

for a binary relationship specifies the number of relationship instances that an entity can participate in.

The possible cardinality ratios for binary relationship types are 1:1, 1:N, N:1, M:N.

Page 39: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

39

Page 40: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

40

Page 41: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

41

Participation Constraints The participation constraint(參加限制 ) specifies whether the existence of an entity depends on its being related to another entity via the relationship type.

There are two types of participation constraints – total and partial.

Page 42: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

42

Relationships as Attributes. 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.

Page 43: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

43

Weak 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.

Page 44: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

44

Weak Entity Types Entities are identified by the

combination of : A partial key of the weak entity type The particular entity they are related

to in the identifying entity type

Page 45: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

45

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.

Page 46: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

46

Page 47: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

47

Page 48: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

48

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 min<max, min>0, max>1. Derived from the mini-world constraints.

Page 49: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

49

Examples (a): A department has exactly one

manager and an employee can manage at most one department. Specify (1,1) for participation of

DEPARTMENT in MANAGES Specify (0,1) for participation of

EMPLOYEE in MANAGES

Page 50: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

50

Examples (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

Page 51: Entity-Relationship (ER) Data Model 概念資料模式 (Based on Chapter 3 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Introduction to Database Systems

51