66
หน่วยการเรียนที4 ER-Diagram

บทที่ 4 er diagram

  • Upload
    -

  • View
    4.911

  • Download
    7

Embed Size (px)

Citation preview

Page 1: บทที่ 4 er diagram

หนวยการเรยนท 4

ER-Diagram

Page 2: บทที่ 4 er diagram

2ระบบฐานขอมล

Entity Sets เราสามารถท าแบบจ าลองของ Database ใหอยในลกษณะดงน

เปนกลมของ entities

relationship ระหวาง entities.

Entity คอ สงทคงอย สามารถระบไดในความจรง และสามารถแยกแยะออกไดจากสงอนๆ.ตวอยางเชน: บคคลใดบคคลหนง , บรษท , เหตการณ, สถานท

Entities แตละตวสามารถม property ไดหลาย propertyตวอยางเชน: บคคล ม ชอ , ทอย , อาย, เพศ ฯลฯ

Entity set คอกลมของ entities ทมคณสมบตทเหมอนกน ซงใช propertiesรวมกน ตวอยางเชน : กลมของบคคลหลายๆ คน, บรษทหลายๆ บรษท, เหตการณหลายๆ เหตการณ, สถานทหลายๆ สถานท

Page 3: บทที่ 4 er diagram

3ระบบฐานขอมล

Entity Sets (ตอ)Entity ใน E-R Model แบงออกเปน 2 ประเภทคอ

1. Regular Entity ไดแก Entity ซงสมาชกภายใน Entity มคณสมบตIdentity ไดดวยตวของมนเอง ( Identity คอ Entity ทม Property ทสามารถก าหนดเปนเอกลกษณเฉพาะใหกบแตละ Entity ได)

EmpID NAME SEX SALARY

00001 สมชาย นลกลด M 8,500

00002 เจรญ กาวหนา M 9,000

00003 ชตมา สกลด F 10,000

Entity “EMPLOYEE”

EMPLOYEE

Page 4: บทที่ 4 er diagram

4ระบบฐานขอมล

Entity Sets (ตอ)2. Weak Entity ไดแก Entity ซงสมาชกภายใน Entity จะมคณสมบต

Identity ได ตองอาศย Property ของ Regular Entity มาประกอบกบ Property ของตวมนเอง

EmpID Date Time_In Time_Out

00001 15/9/46 7.30 17.30

00002 15/9/46 7.30 17.30

00003 15/9/46 7.45 17.45

00001 16/9/46 8.00 19.00

00002 16/9/46 8.25 17.30

00003 16/9/46 8.00 19.00

Entity “TIME_IN_OUT”

TIME_IN_OUT

Page 5: บทที่ 4 er diagram

5ระบบฐานขอมล

Property (Attributes)

Property คอคณลกษณะตางๆ ของ Entity ตวอยางเชนEMPLOYEE = (EmpId, NAME,SEX,SALARY)

EmpID NAME SEX SALARY

00001 สมชาย นลกลด M 8,500

00002 เจรญ กาวหนา M 9,000

00003 ชตมา สกลด F 10,000

Property

Entity

Page 6: บทที่ 4 er diagram

6ระบบฐานขอมล

ประเภทของ Property :

1. Simple Property ไดแก Property ทคาภายใน Property นนไมมการแบงยอยอก

Page 7: บทที่ 4 er diagram

7ระบบฐานขอมล

ประเภทของ Property (ตอ)

2. Composite Property คอ Property ทคาภายใน Property นน มการแบงแยกเปน Property ยอยอก

EMPLOYEEEmpID

NAME SEX SALARY

FNAME

SNAME

Page 8: บทที่ 4 er diagram

8ระบบฐานขอมล

ประเภทของ Property (ตอ)

3. Key คอ Property หรอกลมของ Property ทมคาในแตละสมาชกของ Entity ไมซ ากน

EMPLOYEEEmpID

NAME SEX SALARY

FNAME

SNAME

Page 9: บทที่ 4 er diagram

9ระบบฐานขอมล

ประเภทของ Property (ตอ)

4. Single-valued Property เปน Property ทมคาขอมลภายในProperty เพยงคาเดยว เชน EmpID, NAME, SEX, SALARY

5. Multi-valued Property เปน Property ทมคาขอมลภายในProperty ไดหลายคา

Page 10: บทที่ 4 er diagram

10ระบบฐานขอมล

Resolving Multivalued Attribute Problems

ใน conceptual model สามารถรองรบ multivalued attributes ได แตไมสามารถสรางใน relational DBMS ได มวธแกไข 2 แบบคอ

สรางแอตทรบวสใหมส าหรบแตละ multivalued attribute’s components เดม

ท าใหเกดปญหาทางดานโครงสรางตามมาได

สรางเอนทตใหม ซงประกอบดวย multivalued attribute’s components เดม

Page 11: บทที่ 4 er diagram

11ระบบฐานขอมล

Splitting the Multivalued Attribute into New Attributes

Page 12: บทที่ 4 er diagram

12ระบบฐานขอมล

Components of the Multivalued Attribute

Page 13: บทที่ 4 er diagram

13ระบบฐานขอมล

A New Entity Set Composed of a Multivalued Attribute’s Components

Page 14: บทที่ 4 er diagram

14ระบบฐานขอมล

ประเภทของ Property (ตอ)

6. Derived Property คอ Property ทไดมาจากการน า Property อนมาค านวณ ซงคาจะมการเปลยนแปลงทกครงท Property ทน ามาค านวณเปลยนแปลง

ตวอยางเชน อาย, ไดมาจากการน า วนเกด มาค านวณ

ไมจ าเปนตองเกบขอมลไวในฐานขอมล

Page 15: บทที่ 4 er diagram

15ระบบฐานขอมล

Depiction of a Derived Attribute

Page 16: บทที่ 4 er diagram

16ระบบฐานขอมล

สรปสญลกษณใน E-R Diagram (แบบ Chen)สญลกษณ ความหมาย

Entity

Weak Entity

Relationship

Weak Relationship

Composite Entity หรอ Gerund

Attribute

Multi – Value Attribute

Primary Key Attribute

Composite Attribute

Derived Attribute

Page 17: บทที่ 4 er diagram

17ระบบฐานขอมล

Relationship Sets

Relationship หมายถงความสมพนธระหวาง Entity

EMPLOYEEEmpID

NAME SEX SALARY DEP_NAME

DEPARTMENT

DEP_ID

WORK_IN

Page 18: บทที่ 4 er diagram

18ระบบฐานขอมล

RelationshipRelationship สามารถน า Property ทท าหนาทเปน Key ของ Entity ท

มความสมพนธกน มาสรางเปน Relationship ได และสามารถม Property เปนของตวเองได

STUDENT

SID SNAME

COURSE

CID CNAME

TAKE

GRADE

Page 19: บทที่ 4 er diagram

19ระบบฐานขอมล

Relationship

PERSON CITYWAS_BORN_IN

LIVES_IN

Relationship ระหวาง Entity ใดๆ สามารถมความสมพนธระหวางสมาชกใน Entity เหลานนไดมากกวา 1 ความสมพนธ

Page 20: บทที่ 4 er diagram

20ระบบฐานขอมล

The M:N Relationship Between STUDENT and CLASS

Page 21: บทที่ 4 er diagram

21ระบบฐานขอมล

A Composite Entity in an ERD

Page 22: บทที่ 4 er diagram

22ระบบฐานขอมล

Degree ของ Relationship Set คอจ านวนของ entity sets ทม relationship set รวมกน.

Relationship sets ทเกดขนระหวาง 2 entity sets ใดๆ เรยกวา binary (หรอ มdegree เทากบ 2) ซงพบมากทสดในแผนภาพ E-R

Relationship sets ทเกดขนระหวาง 3 entity เรยกวา ternary หรอเรยก Relationship sets ทเกดขนระหวาง entity ทมากกวา 2 entity วา N-ary Relationship

Page 23: บทที่ 4 er diagram

23ระบบฐานขอมล

Three Types of Relationships

Page 24: บทที่ 4 er diagram

24ระบบฐานขอมล

The Implementation of a Ternary Relationship

Page 25: บทที่ 4 er diagram

25ระบบฐานขอมล

Recursive Relationshipเปน Relationship ทเกดขนกบ Entity เดยว ในกรณท Property ของ Entity

นน สามารถสรางความสมพนธกบอก Property หนงภายใน Entity เดยวกน

EMPLOYEE

EmpID NAME SEX SALARY

MANAGER_OF

Page 26: บทที่ 4 er diagram

26ระบบฐานขอมล

An ER Representation of Recursive Relationships

Page 27: บทที่ 4 er diagram

27ระบบฐานขอมล

The 1:1 Recursive Relationship “EMPLOYEE is Married to EMPLOYEE”

Page 28: บทที่ 4 er diagram

28ระบบฐานขอมล

Implementation of the 1:M “EMPLOYEE Manages EMPLOYEE” Recursive Relationship

Page 29: บทที่ 4 er diagram

29ระบบฐานขอมล

Implementation of the M:N Recursive “PART Contains PART” Relationship

Page 30: บทที่ 4 er diagram

30ระบบฐานขอมล

Various Implementations of a 1:1 Recursive Relationship

Page 31: บทที่ 4 er diagram

31ระบบฐานขอมล

Composite Entities

Also known as bridge entities

Composed of the primary keys of each of the entities to be connected

May also contain additional attributes that play no role in the connective process

Page 32: บทที่ 4 er diagram

32ระบบฐานขอมล

The M:N Relationship Between STUDENT and CLASS

Page 33: บทที่ 4 er diagram

33ระบบฐานขอมล

A Composite Entity in an ERD

Page 34: บทที่ 4 er diagram

34ระบบฐานขอมล

Converting the M:N Relationship into Two 1:M Relationships

Page 35: บทที่ 4 er diagram

35ระบบฐานขอมล

RELATIONSHIP Strength Existence dependence

Entity’s existence depends on the existence of one or more other entities

Existence independence

Entity can exist apart from one or more related entities

Weak (non-identifying) relationships

One entity is not existence-independent on another entity

Strong (Identifying) Relationships

Related entities are existence-dependent

Page 36: บทที่ 4 er diagram

36ระบบฐานขอมล

Existence Dependency

If an entity’s existence depends on the existence of one or more other entities, it is said to be existence-dependent.

Page 37: บทที่ 4 er diagram

37ระบบฐานขอมล

A Weak (Non-Identifying) Relationship Between COURSE and CLASS

Page 38: บทที่ 4 er diagram

38ระบบฐานขอมล

A Weak Relationship Between COURSE and CLASS

Page 39: บทที่ 4 er diagram

39ระบบฐานขอมล

A Strong (Identifying) Relationship Between COURSE and CLASS

Page 40: บทที่ 4 er diagram

40ระบบฐานขอมล

Relationship Strength and Weak Entities

Weak entity meets two conditions

Existence-dependent:

Cannot exist without entity with which it has a relationship

Has primary key that is partially or totally derived from the parent entity in the relationship

Page 41: บทที่ 4 er diagram

41ระบบฐานขอมล

A Weak Entity in an ERD

Page 42: บทที่ 4 er diagram

42ระบบฐานขอมล

A Weak Entity in a Strong Relationship

Page 43: บทที่ 4 er diagram

43ระบบฐานขอมล

Connectivity ใชอธบายประเภทของความสมพนธของเอนทต

( one-to-one, one-to-many, and many-to-many).

Connectivity

Page 44: บทที่ 4 er diagram

44ระบบฐานขอมล

Connectivity

Dean Faculty

One-to-One Relationship

Faculty TeacherWork_IN1 M

One-to-Many Relationship

Dean FacultyDean_of

1 1

Faculty Teacher

Chen

Crow’s Foot

Chen

Crow’s Foot

Page 45: บทที่ 4 er diagram

45ระบบฐานขอมล

Connectivity

Student CourseEnrollM M

Many-to-Many Relationship

Student Couse

Chen

Crow’s Foot

Page 46: บทที่ 4 er diagram

46ระบบฐานขอมล

Cardinalities

คอการระบจ านวน ทสมาชกใน Entity หนงสามารถมความสมพนธกบสมาชกในอกเอนทตหนงได

Page 47: บทที่ 4 er diagram

47ระบบฐานขอมล

Connectivity and Cardinality in an ERD

Page 48: บทที่ 4 er diagram

48ระบบฐานขอมล

หมายถง ลกษณะของความสมพนธทเกดขนระหวางเอนทต

แบบเลอกได (optional)

แบบบงคบ (mandatory)

ใชเครองหมายวงกลมเลก เพอแทนความสมพนธแบบ เลอกได

การมสวนรวมในความสมพนธ (Relationship Participation)

Page 49: บทที่ 4 er diagram

49ระบบฐานขอมล

CLASS is Optional to COURSE

COURSE and CLASS in a Mandatory Relationship

Relationship Participation

Page 50: บทที่ 4 er diagram

50ระบบฐานขอมล

Cardinalities และ Participation แบบ Chen

Page 51: บทที่ 4 er diagram

51ระบบฐานขอมล

Cardinalities และ Participation แบบ Crow’s Foot

Mandatory One

Mandatory Many

Optional One

Optional Many

Page 52: บทที่ 4 er diagram

52ระบบฐานขอมล

Total participation (แสดงแทนดวยเสนค): ทกๆ entity ใน entity set participates อยางนอยเทากบ 1 relationship ใน relationship set (บางครงเรยก Mandatory)

Partial participation: บาง entities อาจจะไม participate กบ relationship ใน relationship set เลยกได (บางครงเรยก Optional)

ตวอยาง การมสวนรวมในความสมพนธ แบบอน

Page 53: บทที่ 4 er diagram

53ระบบฐานขอมล

The generalization hierarchy depicts the parent-child relationship.

The supertype contains the shared attributes, while the subtype contains the unique attributes.

A subtype entity inherits its attributes and its relationships from the supertype entity.

Entity Supertypes and Subtypes

Page 54: บทที่ 4 er diagram

54ระบบฐานขอมล

A Generalization Hierarchy

Page 55: บทที่ 4 er diagram

55ระบบฐานขอมล

The supertype entity set is usually related to several unique and disjointed (nonoverlapping) subtype entity sets.

The supertype and its subtype(s) maintain a 1:1 relationship.

Entity Supertypes and Subtypes

Page 56: บทที่ 4 er diagram

56ระบบฐานขอมล

A Generalization Hierarchy With Overlapping Subtypes

Overlapping

NonOverlapping

Page 57: บทที่ 4 er diagram

57ระบบฐานขอมล

Generalization และ Specialization แบบอน

เปนการแบงสมาชกของ Entity Set ออกเปน Entity Set ยอย ในลกษณะ Top-down design process;

Entity Set ยอย (Subset Entity) ใน lower-level จะม Property และ participate ทเหมอนกบ Entity ใน higher-level (Superset Entity)นอกจากน Entity Set ยอย ยงสามารถม Property เพมเตมทใชบงบอกความแตกตางของสมาชกใน Entity ยอยเหลานน

จะใช รป สามเหลยม ทมลาเบลวา “ISA” (E.g. customer “is a” person) เปนสญลกษณ.

Page 58: บทที่ 4 er diagram

58ระบบฐานขอมล

Specialization Example

Page 59: บทที่ 4 er diagram

59ระบบฐานขอมล

Generalization

เปนการรวมจ านวนของ Entity Set ทมคณสมบตทเหมอนกนขนไปยง higher-level entity set.

Specialization และ generalization จะมลกษณะตรงขามกน แตใชในการอธบายความหมายเดยวกนใน E-R diagram.

EMPLOYEE

EmpID NAME SEX SALARY

MALE FEMALE

MILITARY_STATUS

Page 60: บทที่ 4 er diagram

60ระบบฐานขอมล

Coverage Property

คอ ความสมพนธระหวางจ านวนสมาชกของ Superset Entity ทมความสมพนธไปยง Subset Entity สามารถแบงไดเปน 4 แบบคอ

Total Coverage คอ การทสมาชกใน Superset Entity มความสมพนธกบสมาชกใน Subset Entity อยางนอย 1 Entity ใชตวอกษรตว “t” แทนสญลกษณ

Partial Coverage คอ มบางสมาชกใน Superset Entity ไมมความสมพนธกบสมาชกใน Subset Entity ใดเลย ใชตวอกษร “p” แทนสญลกษณ

Exclusive Coverage คอ สมาชกใน Superset Entity มความสมพนธกบสมาชกใน Subset Entity ใด Entity หนงเทานน ใชตวอกษร “e” แทนสญลกษณ (หรอเรยก Disjoint)

Page 61: บทที่ 4 er diagram

61ระบบฐานขอมล

Coverage Property (ตอ)Overlapping Coverage คอ สมาชกใน Superset Entity มความสมพนธกบ

สมาชกใน Subset Entity ไดมากกวา 1 Entity ใชตวอกษร “o” แทนสญลกษณ

PERSON

MALE FEMALE EMPLOYEESECRETARYMANAGER

ADVERTISING_EMPLOYEE

SALE_EMPLOYEE

PROGRAMMER

(t,e) (p,e)

(p,o)

Page 62: บทที่ 4 er diagram

62ระบบฐานขอมล

Coverage Property (ตอ)ใน E-R Diagram รปแบบอน จะใชค าอธบายดงน

ใช Total และ Partial เพออธบายความสมพนธ ของ Superset Entity กบ Subset Entity วา ตองมหรอไมตองมกได

ใช Disjoint แทนความหมายของ Exclusive Coverage

ใช Overlapping แทนความหมายของ Overlapping Coverage

Page 63: บทที่ 4 er diagram

63ระบบฐานขอมล

A Supertype/Subtype Relationship in an ERD

Page 64: บทที่ 4 er diagram

64ระบบฐานขอมล

สรปสญลกษณใน E-R Diagram (แบบ Chen)สญลกษณ ความหมาย

Entity

Weak Entity

Relationship

Weak Relationship

Composite Entity หรอ Gerund

Attribute

Multi – Value Attribute

Primary Key Attribute

Composite Attribute

Derived Attribute

Page 65: บทที่ 4 er diagram

65ระบบฐานขอมล

Page 66: บทที่ 4 er diagram

66ระบบฐานขอมล

The Completed Tiny College ERD