entity-relationship modeling - walailak universitymit.wu.ac.th/mit/images/editor/files/dbs 2556...
TRANSCRIPT
Connolly, Thomas and Begg, Carolyn. 2010. Database Systems: A
Practical Approach to Design, Implementation, and Management. 5th
Ed. Pearson Education.
Entity-relationship modeling
ITM-661 ระบบฐานขอ้มลู (Database system) 1
... การจะพัฒนาทกุสิง่ทกุอยา่งใหเ้จรญินัน้จะตอ้งสรา้งและเสรมิขึน้จากพืน้ฐานเดมิทีม่อียูก่อ่นทัง้สิน้ ถา้พืน้ฐานไมด่ ีหรอืคลอนแคลนบกพรอ่งแลว้ ทีจ่ะเพิม่เตมิเสรมิตอ่ใหเ้จรญิขึน้ไปอกีนัน้ ยากนักทีจ่ะท าได ้จงึควรจะเขา้ใจใหแ้จง้ชดัวา่ นอกจากจะมุง่สรา้งความเจรญิแลว้ ยงัตอ้งพยายามรักษาพืน้ฐานใหม้ัน่คง ไม่บกพรอ่งพรอ้มๆกนัไปดว้ย..
พระบรมราโชวาทของพระบาทสมเด็จพระเจา้อยูห่วั
ในพธิพีระราชทานปรญิญาบตัร จฬุาลงกรณ์มหาวทิยาลยั
10 ก.ค.2523
2
Objectives
How to use Entity–Relationship (ER) modeling in database design.
Basic concepts associated with ER model.
Diagrammatic technique for displaying ER model using Unified Modeling Language (UML).
How to identify and resolve problems with ER models called connection traps.
How to build an ER model from a requirements specification.
3
เนือ้หา
Entity-relationship modeling
Entity types
Relationship types
Attributes
Structural constraints
Problems with ER model
4
Entity-relationship diagram (ERD)
ERD เป็นเครือ่งมอืทีใ่ชป้ระกอบการวเิคราะหแ์ละการจ าลองขอ้มลู ผูท้ีค่ดิคน้แบบจ าลองนีค้อื Dr. Peter Chen ในปีคศ. 1976
ปัจจบุนันีก้็ยงัไดค้วามนยิมในการใชง้านอยา่งแพรห่ลาย รวมทัง้เป็นพืน้ฐานของการพัฒนาเครือ่งมอืในการพัฒนาระบบ (CASE tool) และ Diagram แบบอืน่ๆเชน่ UML ซึง่เป็นเครือ่งมอืในการวเิคราะหแ์ละออกแบบเชงิวตัถ ุ
ERD เป็นแบบจ าลองขอ้มลูทีน่ยิมใชใ้นระดบั Conceptual
เนน้การอธบิายทีเ่กีย่วขอ้งกับ Entity, Relationship และ Attribute
รวมถงึความหมาย (Semantic) หรอืเงือ่นไขอืน่ของขอ้มลูดว้ย
นักวเิคราะหร์ะบบใช ้ERD เพือ่วเิคราะหก์ารใชข้อ้มลูของผูใ้ช ้
กลุม่ตา่งๆ
6
First-cut ERD for Staff user views of DreamHome
7
ERD of Branch user views of DreamHome with PK
8
ER diagram for Staff user views of DreamHome with PK
9
Conceptual data model for Staff user view showing all attributes
10
ขอ้สงัเกต
การเขยีน ERD สามารถเขยีนโดยมรีายละเอยีดตา่งกนั
Entity – relationship
Entity – relationship with PK
Entity – relationship – attribute
11
Concepts of the ER Model
Entity types
Relationship types
Attributes
12
Entity Type
Entity type
Group of objects with same properties, identified by enterprise as having an independent existence.
student(id, name, sex, DOB)
Entity occurrence
Uniquely identifiable object of an entity type.
4810524, สมชาย รักด,ี ช, 16/05/2534
4810547, สมศร ีมสีขุ, ญ, 04/11/2535
4810512, ขนษิฐา ใจเย็น, ญ, 26/12/2535
13
ตวัอยา่ง
ผูค้น เชน่ พนักงาน ลกูคา้ ผูป้ระมลู ผูส้มคัร ผูป่้วย ผูต้อ้งหา
สถานทีเ่ชน่ บรษัิท โรงงาน อาคาร โรงแรม โรงพยาบาล โรงเรยีน สถานตี ารวจ สถานทีเ่กดิเหต ุสนามเทนนสิ
สิง่ของทีจั่บตอ้งได ้เชน่ สนิคา้ ยา วตัถดุบิ คอมพวิเตอร ์อาหาร ยานพาหนะ เครือ่งดนตร ี
สิง่ของทีจั่บตอ้งไมไ่ด ้เชน่ หลกัสตูร วชิา เพลง การประชมุ ซอฟตแ์วร ์
14
ตัวอยา่ง
15
การเขยีนสญัลักษณ ์
กรณีไมม่aีttributeอืน่
กรณีมแีต ่primary key
กรณีมaีttribute
Entity name
16
ตวัอยา่ง
การประชมุ
ลกูคา้
วชิา
นักศกึษา
การตัง้ชือ่เป็นค านามที่กระชบัและชดัเจน
17
ขอ้ควรระวงั
ใน ERD หนึง่ๆ จะม ีentity ชือ่ซ ้ากนัไมไ่ด ้
ชือ่ภาษาองักฤษไมจ่ าเป็นตอ้งเป็นพหพูจนถ์งึแมจ้ะหมายถงึสิง่ของจ านวนหลายชิน้
ชือ่ทีต่ัง้สามารถเขา้ใจไดอ้ยา่งชดัเจนวา่ขอ้มลูทีเ่ก็บคอือะไร
ชือ่ตอ้งกระชบั ไมย่าวเยิน่เยอ้ เชน่ ไมใ่ช ่“ลกูคา้ของบรษัิท” ควรใช ้“ลกูคา้”
18
ประเภทของ Entity
Strong entity type
ไมข่ึน้กบัขอ้มลูอืน่
ขอ้มลูสว่นใหญจั่ดอยูใ่นประเภทนี ้
19
Weak entity type
เป็นขอ้มลูทีม่ลีกัษณะขึน้กบัขอ้มลูอืน่ (Existence
dependent)
ขอ้มลูใน Weak entity มทีีม่าจากจาก Entity อืน่เป็นหลกัจงึ
ตอ้งพึง่พงิกบัขอ้มลูเหลา่นัน้ หากขอ้มลูหลกัถกูลบทิง้หรอื
เปลีย่นแปลง จะสง่ผลกระทบตอ่ขอ้มลูใน Weak entity
การออกแบบทีร่ะบวุา่ขอ้มลูนัน้มคีณุสมบตัทิีพ่ ึง่พงิเมือ่สรา้ง
ฐานขอ้มลูขึน้มาจะตอ้งเขยีนค าสัง่ DDL ทีก่ าหนดความผกูพัน
นีไ้วด้ว้ย
20
ตวัอยา่งความสมัพันธร์ะหวา่ง Weak และ Strong entity type
อาจารยเ์ป็น Strong entity type แตป่ระวตักิารศกึษา(ของอาจารย)์ เป็น Weak entity type
Ph.D. (Computer science) UNSW
M.Sc. (Applied Statistics) NIDA
B.Sc. (Mathematics) PSU
สมศร ีสอนนาน สารสนเทศศาสตร ์4555 วชิาการ12 111
21
ตวัอยา่ง Strong และ Weak entity
เลขที ่111 15 เมษายน 2552
ใบส ัง่ซือ้สนิคา้
เรยีน บรษัิท คนกนัเอง จ ากดั
รวมทัง้ส ิน้ 12,000 บาท
ขอสัง่ซือ้สนิคา้ดงัรายการตอ่ไปนี ้
รายการ จ านวน ราคาตอ่หน่วย รวม(บาท)
1. แชมพ ู 50 100 5000 2. สบู ่ 100 20 2000 3. ยาสฟัีน 100 50 5000
22
ใบสัง่ซือ้เป็น Strong entity type แตร่ายละเอยีดการสัง่ซือ้(ของใบสัง่ซือ้นัน้) เป็น Weak entity type
เลขที ่111 15 เมษายน 2552
ใบส ัง่ซือ้สนิคา้
บรษัิท คนกนัเอง จ ากดั ยอดรวม 12,000 บาท
รายละเอยีดของใบสัง่ซือ้ 1. แชมพ ู 50 ขวดๆละ 100 บาท 2. สบู ่ 100 กอ้นๆละ 20 บาท 3. ยาสฟัีน 100 หลอดๆละ 50 บาท
23
ตวัอยา่ง
24
ตวัอยา่ง
ผูป่้วย
รหสั {pk} ชือ่ นามสกลุ เพศ วดปเกดิ
ประวัตอิาการ ครัง้ที ่วันที ่BT BW BP อาการ
ม ี
25
Relationship Types
Relationship type
Set of meaningful associations among entity types.
Relationship occurrence
Uniquely identifiable association, which includes one occurrence from each participating entity type.
26
ตัวอยา่ง
meaningful associations
27
Semantic net of Has relationship type
Relationship occurrence คอื r1 (B003, SG37), r2 และ r3
28
Degree of a relationship
Number of participating entities in relationship.
Relationship of degree :
two is binary
three is ternary
four is quaternary.
29
สญัลักษณท์ีใ่ช ้
หากเป็นการเชือ่มโยงใน entity เดยีวหรอืระหวา่ง 2 entity ใช ้
เสน้ตรง ทีเ่ป็นเสน้ทบึ
หากม ีentity ตัง้แต ่3 ขึน้ไปจะเพิม่สีเ่หลีย่มขนมเปียกปนู
student course enroll
doctor medicine
patient
prescribe
30
1. พจิารณาตามจ านวนของสมาชกิของ entity ทีเ่กีย่วขอ้งกนั
1:1 relationship
1:N และ N:1 relationship
N:N relationship
2. พจิารณาตามจ านวนของ entity ทีเ่กีย่วขอ้ง
Binary relationship
Unary relationship
Ternary relationship
N-ary relationship
Connection Relationship และ การจัดประเภทของ Relationship
31
3. พจิารณาตามเงือ่นไขการมสีว่นรว่มของความสมัพันธ ์
Mandatory participation
Optional participation
4. พจิารณาจากประเภทของ entity ทีน่ ามาเชือ่มโยงกรณีม ีweak entity type มาเกีย่วขอ้ง
Existence dependency
Identifier dependency
32
Binary relationship called POwns
student course enroll
lecturer course teach
33
Ternary relationship called Registers
doctor medicine
patient
prescribe
Doctor prescribes some medicine for his patient
34
Quaternary relationship called “Arranges”
35
Recursive Relationship
Relationship type where same entity type participates more than once in different roles.
Relationships may be given role names to indicate purpose that each participating entity type plays in a relationship.
ความสมัพันธใ์น entity เดยีว
36
Recursive relationship called Supervises with role names
37
Recursive relationship
subject subject is prerequisite of
เชน่ MIT-532 เป็นวชิาบงัคบัเรยีนกอ่นของวชิา MIT-533
subject
is prerequisite of
38
Entities associated through two distinct relationships with role names
39
Attributes
Property of an entity or a relationship type.
เพลง
ชือ่เพลง
ผูแ้ตง่เนือ้เพลง
ผูแ้ตง่ท านอง
ผูร้อ้ง
ความยาวของเพลง
40
Attributes ของ relationship
41
ตวัอยา่ง
teach
term year percent-resps
Lecturer ID {pk} Title Name Phone office
Course ID {pk} Title Credit
42
Entity and attribute
หากสงัเกตใหด้จีะเห็นวา่เมือ่กลา่วถงึ นักศกึษา ยานพาหนะ หรอื สนิคา้ เป็นการกลา่วถงึ นักศกึษาโดยรวม ยานพาหนะโดยรวม และ สนิคา้โดยรวม
มรีายละเอยีดตามทีร่ะบโุดย Attribute
ไมไ่ดเ้จาะจงเป็นนักศกึษาเฉพาะคน สนิคา้เฉพาะรายการ หรอืยานพาหนะเฉพาะคนั
43
entity/attribute ไมใ่ช ่Data value
อาหาร
ชือ่อาหาร (เชน่กว๋ยเตีย๋วหมสูบั ซปุขา้วโพด ซา่หริม่ ขา้วซอย ขนมปังหนา้กุง้)
ประเภท (เชน่ของคาว ของหวาน ของวา่ง)
จ านวนแคลอร ี
ราคาอาหาร
44
Domain ของ Attribute
Set of allowable values for one or more attributes.
ทกุแอทรบิวิทจ์ะตอ้งมโีดเมน
แอทรบิวิทม์ากกวา่ 1 แอทรบิวิท ์สามารถใชโ้ดเมนเดยีวกนั
วนัที ่(คอื dd/mm/yy) เป็นโดเมนของ
วนัเกดิ
วนัเริม่งาน
วนัยืน่ใบสมคัร
วนัทีล่าออก
45
โดเมนสามารถระบเุป็นชว่ง (Range) ได ้เชน่ คา่ของระดบัชัน้ของขา้ราชการหรอื C ตัง้แต ่1 ถงึ 11
โดเมนสามารถก าหนดเป็นคา่จ าเพาะได ้เชน่ คา่ของ gender หรอืเพศ ม ี2 คา่คอื “ช” หรอื “ญ” เทา่นัน้
46
โดเมนอาจประกอบดว้ยโดเมนยอ่ย เชน่ วนัทีป่ระกอบดว้ย วนั เดอืน และ ปี หากพจิารณาแตล่ะสว่น
วนัมคีา่ทีใ่ชไ้ดอ้ยูใ่นชว่ง 1 ถงึ 31 หรอื เดอืนมคีา่ทีใ่ชไ้ดอ้ยู่ระหวา่ง 1 ถงึ 12 เป็นตน้
Date day
month
year
47
ในการก าหนดโดเมนใหก้บัแอทรบิวิทบ์างแอทรบิวิท ์อาจมีเกณฑใ์หเ้ลอืกใชห้ลายแบบ
เชน่ สว่นสงูของคนอาจมหีน่วยเป็นเซนตเิมตร หรอื เป็นฟตุกไ็ด ้
จงึตอ้งเลอืกวา่จะเก็บแบบไหน เพราะคา่ทีเ่ก็บจะตา่งกนั
48
ประโยชนข์องโดเมน
ใชเ้พือ่ก าหนดวา่การท างานใดจะใชก้บัขอ้มลูนัน้ได ้เชน่ถา้ขอ้มลูเป็นตวัเลขจงึจะบวกกนัได ้
ใชเ้พือ่ก าหนดวา่สามารถน าแอทรบิวิทใ์ดมาเปรยีบเทยีบกนัไดบ้า้ง
ใชเ้พือ่ตรวจสอบวา่คา่ทีเ่ป็นไปไดข้องแอทรบิวิทน์ีเ้ป็นคา่ใด
ใชเ้พือ่ก าหนดเนือ้ทีแ่ละรปูแบบทีใ่ชไ้ดก้บัขอ้มลูนัน้ๆเมือ่จะสรา้งตารางจรงิๆ
49
ประเภทของ Attributes
Simple Attribute
เป็นแอทรบิวิทท์ีไ่มส่ามารถแบง่ยอ่ยออกเป็นแอทรบิวิทไ์ด ้อกี
เชน่ เพศ ชือ่ภาควชิา ถนน เงนิเดอืน รหสันักศกึษา รหสัวชิา จ านวนหน่วยกติ ยีห่อ้สนิคา้ จ านวนทีส่ัง่ซือ้
50
Composite Attribute
Attribute composed of multiple components, each with an independent existence.
เกดิจากแอทรบิวิทย์อ่ยๆมารวมกนั หรอืสามารถทีจ่ะแบง่ออกมาเป็นสว่นยอ่ยได ้
โดยทัว่ไปมกัใชใ้นระดบัการออกแบบเพือ่ใหเ้ห็นภาพรวมเสยีกอ่นจากนัน้จงึพจิารณาวา่ตอ้งการเรยีกใชเ้ฉพาะสว่นใดสว่นหนึง่ของขอ้มลูหรอืไม ่
เชน่ทีอ่ยูป่ระกอบดว้ย บา้นเลขที ่ถนน หมูท่ี ่อ าเภอ จังหวดั รหสัไปรษณีย ์
51
Single-valued Attribute
Attribute that holds a single value for each occurrence of an entity type.
โดยทัว่ไปแลว้แอทรบิวิทม์กัเป็นประเภทนี ้
กลุม่เลอืด (เชน่มคีา่เป็น A, B, AB หรอื O เพยีงคา่เดยีว)
จ านวนหน่วยกติของรายวชิา (เชน่มคีา่เป็น 1, 2, 3 หรอื 6 เพยีงคา่เดยีว)
ชือ่นักศกึษา
หมายเลขหอ้งเรยีน
52
Multi-valued Attribute
Attribute that holds multiple values for each occurrence of an entity type.
ในบางกรณีการเก็บขอ้มลูอาจพบวา่คา่ทีต่อ้งการเก็บของแอทริบวิทนั์น้มคีา่มากกวา่ 1 คา่ เชน่ หมายเลขโทรศพัทข์องลกูคา้อาจมมีากกวา่ 1 หมายเลข หรอื e-mail ทีใ่ชต้ดิตอ่อาจมีมากกวา่ 1
กรณีนีใ้นการออกแบบใหใ้ชส้ญัลกัษณ์พเิศษเพือ่เป็นการเตอืนผูอ้อกแบบวา่จะตอ้งด าเนนิการแกไ้ขใหเ้หลอืเพยีงคา่เดยีวในขัน้ตอนตอ่ๆไป
53
Derived Attribute
Attribute that represents a value that is derivable from value of a related attribute, or set of attributes, not necessarily in the same entity type.
คา่ของ attribute สามารถหามา หรอื ค านวณไดจ้าก attribute อืน่โดยไมต่อ้งใสค่า่ลงไปเองโดยผูใ้ช ้
ในการเก็บคา่บางประเภทเชน่ อาย ุจัดไดว้า่เป็นคา่ทีแ่ปรไปตามวนัเวลาทีเ่ปลีย่นไป (เพิม่ขึน้ตามกาลเวลา) ดงันัน้เมือ่เรยีกใชแ้ตล่ะครัง้อายจุงึตอ้งมกีารค านวณเพือ่แสดงอายทุี่ถกูตอ้ง
54
ตวัอยา่งและสญัลักษณท์ีใ่ช ้
55
Keys
Candidate Key
Minimal set of attributes that uniquely identifies each occurrence of an entity type.
Primary Key
Candidate key selected to uniquely identify each occurrence of an entity type.
Composite Key
A candidate key that consists of two or more attributes.
56
Main type of constraint on relationships is called multiplicity.
Multiplicity - number (or range) of possible occurrences of an entity type that may relate to a single occurrence of an associated entity type through a particular relationship.
Represents policies (called business rules) established by user or company.
Structural Constraints
57
The most common degree for relationships is binary.
Binary relationships are generally referred to as being:
one-to-one (1:1)
one-to-many (1:*)
many-to-many (*:*)
58
เงือ่นไขการระบคุวามสมัพันธ ์
เงือ่นไขของการเขยีน (min, max)
student ใช ้(1,*) และ course ใช ้(0,*)
ความหมายคอื นักศกึษาแตล่ะคนสามารถลงทะเบยีนไดห้ลายวชิา และตอ้งเรยีนอยา่งนอ้ย 1 วชิา ในขณะทีว่ชิาหนึง่ๆอาจมนัีกศกึษาเรยีนมากกวา่ 1 คน แตบ่างวชิาอาจไมม่นัีกศกึษาเรยีนเลย
student course enroll (1,*) (0,*)
59
Semantic net of Staff Manages Branch relationship type
60
Multiplicity of Staff Manages Branch (1:1) relationship
61
Semantic net of Staff Oversees PropertyForRent relationship type
62
Multiplicity of Staff Oversees PropertyForRent (1:*) relationship type
63
Semantic net of Newspaper Advertises PropertyForRent relationship type
64
Multiplicity of Newspaper Advertises PropertyForRent (*:*) relationship
65
Structural Constraints
Multiplicity for Complex Relationships
Number (or range) of possible occurrences of an entity type in an n-ary relationship when other (n-1) values are fixed.
66
Semantic net of ternary Registers relationship with values for Staff and Branch entities fixed
67
Multiplicity of ternary Registers relationship
68
Summary of multiplicity constraints
69
Structural Constraints
Multiplicity is made up of two types of restrictions on relationships:
Cardinality
Describes maximum number of possible relationship occurrences for an entity participating in a given relationship type.
Participation
Determines whether all or only some entity occurrences participate in a relationship.
70
Multiplicity as cardinality and participation constraints
71
Problems with ER Models
Problems may arise when designing a conceptual data model called connection traps.
Often due to a misinterpretation of the meaning of certain relationships.
Two main types of connection traps are called fan traps and chasm traps.
72
Problems with ER Models
Fan Trap
Where a model represents a relationship between entity types, but pathway between certain entity occurrences is ambiguous.
Chasm Trap
Where a model suggests the existence of a relationship between entity types, but pathway does not exist between certain entity occurrences.
73
An Example of a Fan Trap
74
Semantic Net of ER Model with Fan Trap
At which branch office does staff number SG37
work?
75
Restructuring ER model to remove Fan Trap
76
Semantic Net of Restructured ER Model with Fan Trap Removed
SG37 works at branch B003.
77
An Example of a Chasm Trap
78
Semantic Net of ER Model with Chasm Trap
At which branch office is property PA14 available?
79
ER Model restructured to remove Chasm Trap
80
Semantic Net of Restructured ER Model with Chasm Trap Removed
81
82
Enhanced entity-relationship modeling
Objectives
Limitations of basic concepts of the ER model and requirements to represent more complex applications using additional data modeling concepts.
Most useful additional data modeling concept of Enhanced ER (EER) model is called specialization/generalization.
A diagrammatic technique for displaying specialization/generalization in an EER diagram using UML.
83
Enhanced Entity-Relationship Model
Since 1980s there has been an increase in emergence of new database applications with more demanding requirements.
Basic concepts of ER modeling are not sufficient to represent requirements of newer, more complex applications.
Response is development of additional ‘semantic’ modeling concepts.
84
The Enhanced Entity-Relationship Model
Semantic concepts are incorporated into the original ER model and called the EER model.
Examples of additional concept of EER model is called specialization / generalization.
85
Specialization / Generalization
Superclass
An entity type that includes one or more distinct subgroupings of its occurrences.
Subclass
A distinct subgrouping of occurrences of an entity type.
86
Specialization / Generalization
Superclass/subclass relationship is one-to-one (1:1).
Superclass may contain overlapping or distinct subclasses.
Not all members of a superclass need be a member of a subclass.
87
Specialization / Generalization
Attribute Inheritance
An entity in a subclass represents same ‘real world’ object as in superclass, and may possess subclass-specific attributes, as well as those associated with the superclass.
88
Specialization / Generalization
Specialization
Process of maximizing differences between members of an entity by identifying their distinguishing characteristics.
Generalization
Process of minimizing differences between entities by identifying their common characteristics.
89
AllStaff relation holding details of all staff
90
Specialization/generalization
91
Specialization/generalization
92
Shared subclass and subclass with its own subclass
93
Constraints on Specialization / Generalization
Two constraints that may apply to a specialization/generalization:
participation constraints
disjoint constraints.
Participation constraint
Determines whether every member in superclass must participate as a member of a subclass.
May be mandatory or optional.
94
Constraints on Specialization / Generalization
Disjoint constraint
Describes relationship between members of the subclasses and indicates whether member of a superclass can be a member of one, or more than one, subclass.
May be disjoint or nondisjoint.
95
Constraints on Specialization / Generalization
There are four categories of constraints of specialization and generalization:
mandatory and disjoint
optional and disjoint
mandatory and nondisjoint
optional and nondisjoint.
96
Staff Superclass with subclasses
97
Owner Superclass with subclasses
98
Person superclass with subclasses
99