1.07k likes | 1.68k Views
Unit 5 Entity-Relationship Model. 英文版: Chap 6 “Database Design and the E-R Model” 中文版:第 2 章 “實體關係模型”. E-R Diagram for a Banking Enterprise. Chapter 6: Entity-Relationship Model. 6.1. Design Process 6.2. E-R Model ( 名詞定義 ) 6.3. Constraints ( 對模型提出限制 e.g. 對應數目 ) 6.4. E-R Diagram ( 模型解說 )
E N D
Unit 5Entity-Relationship Model 英文版:Chap 6 “Database Design and the E-R Model” 中文版:第2章 “實體關係模型”
Chapter 6: Entity-Relationship Model • 6.1. Design Process • 6.2. E-R Model (名詞定義) • 6.3. Constraints (對模型提出限制 e.g.對應數目) • 6.4. E-R Diagram (模型解說) • 6.5. E-R Design Issues (其他議題) • 6.6. Weak Entity Sets • 6.7. Extended E-R Features • 6.8. Design for the Banking Enterprise • 6.9. Reduction to Relation Schemas • 6.10. Database Design • 6.11. UML
Design Process • Relational Database • Application Layer: SQL/ Host language • Relational Model: Relational Algebra • Logical Database Design • Normalization • Semantic Modeling, i.e., E-R model • Physical Database Design • Storage and File Structure • Indexing and Hashing • Query Process & Optimization • Transactions • Concurrency Control • Recovery System
Modeling • 與 Relational Database 相比較 • Table entity set (atomic P-key), • relationship set • (2) Attribute, domain 定義互通 • A database can be modeled as: • a collection of entities, • relationship among entities. • An entityis an object that exists and is distinguishable from other objects. • Example: specific person, company, event, plant • Entities have attributes • Example: people have names and addresses • An entity set is a set of entities of the same type that share the same properties. • Example: set of all persons, companies, trees, holidays
Attributes • An entity is represented by a set of attributes, that is descriptive properties possessed by all members of an entity set. • Domain – the set of permitted values for each attribute • Attribute types: • Simple and composite attributes. • Single-valued and multi-valued attributes • Example: multivalued attribute: phone_numbers • Derived attributes • Can be computed from other attributes • Example: age, given date_of_birth Example: customer = (customer_id, customer_name, customer_street, customer_city ) loan = (loan_number, amount )
E-R Diagram With Composite, Multivalued, and Derived Attributes Preview Composite Key Multivalued Derived
Entity Sets customer and loan customer_id customer_ customer_ customer_ loan_ amount name street city number S P
Example (“S, P, SP”) price qty Pname PN Sname SN P SP S color status city weight
Relationship Sets • A relationship is an association among several entities Example:HayesdepositorA-102customerentityrelationship setaccountentity • A relationship set is a mathematical relation among n 2 entities, each taken from entity sets {(e1, e2, … en) | e1 E1, e2 E2, …, en En}where (e1, e2, …, en) is a relationship • Example: (Hayes, A-102) depositor 實例如: Depositor Borrower SP table …
Relationship Sets (Cont.) • An attribute can also be property of a relationship set. • For instance, the depositor relationship set between entity sets customer and account may have the attribute access-date 以 SP為例: price, qty
Degree of a Relationship Set • Refers to number of entity sets that participate in a relationship set. • Relationship sets that involve two entity sets are binary (or degree two). Generally, most relationship sets in a database system are binary. • Relationship sets may involve more than two entity sets. • Relationships between more than two entity sets are rare. Most relationships are binary. (More on this later.) • Example: Suppose employees of a bank may have jobs (responsibilities) at multiple branches, with different jobs at different branches. Then there is a ternary relationship set between entity sets employee, job, and branch
Chapter 6: Entity-Relationship Model • 6.1. Design Process • 6.2. E-R Model • 6.3. Constraints • 6.4. E-R Diagram • 6.5. E-R Design Issues • 6.6. Weak Entity Sets • 6.7. Extended E-R Features • 6.8. Design for the Banking Enterprise • 6.9. Reduction to Relation Schemas • 6.10. Database Design • 6.11. UML
Mapping Cardinality Constraints • Express the number of entities to which another entity can be associated via a relationship set. • Most useful in describing binary relationship sets. • For a binary relationship set the mapping cardinality must be one of the following types: • One to one • One to many • Many to one • Many to many 實際轉化為 Table 時 One-to-One: 可省去代表Relationship Set 的表格 ,三者同在一表中。 One-to-Many: 此二情形之Relationship Set Many-to-One: 必須作表,其Candidate Key 與 Many端相同,甚至可合併 到 Many 端去(非完全參與用null)。 Many-to-Many: 就同前例SP表,須使用 Composite key
Mapping Cardinalities One to one One to many Note: Some elements in A and B may not be mapped to any elements in the other set
Mapping Cardinalities Many to one Many to many Note: Some elements in A and B may not be mapped to any elements in the other set
Keys • A super key of an entity set is a set of one or more attributes whose values uniquely determine each entity. • A candidate key of an entity set is a minimal super key • Customer_id is candidate key of customer • account_number is candidate key of account • Although several candidate keys may exist, one of the candidate keys is selected to be the primary key.
Keys for Relationship Sets • The combination of primary keys of the participating entity sets forms a super key of a relationship set. • (customer_id, account_number) is the super key of depositor • NOTE: this means a pair of entity sets can have at most one relationship in a particular relationship set. (可使用 multivalued, 如必要時亦可另開 entity set 再用 many to one 連接這兩個 entity sets) • Example 1: if we wish to track all access_dates to each account by each customer, we cannot assume a relationship for each access. We can use a multivalued attribute though • Example 2: Team – match ((game#)) – Team • Must consider the mapping cardinality of the relationship set when deciding what are the candidate keys (many-to-one 有合併機會) • Need to consider semantics of relationship set in selecting the primary key in case of more than one candidate key (例如外加“配對編號”)
Chapter 6: Entity-Relationship Model • 6.1. Design Process • 6.2. E-R Model • 6.3. Constraints • 6.4. E-R Diagram • 6.5. E-R Design Issues • 6.6. Weak Entity Sets • 6.7. Extended E-R Features • 6.8. Design for the Banking Enterprise • 6.9. Reduction to Relation Schemas • 6.10. Database Design • 6.11. UML
E-R Diagrams • Rectangles represent entity sets. • Diamonds represent relationship sets. • Lines link attributes to entity sets and entity sets to relationship sets. • Ellipses represent attributes • Double ellipses represent multivalued attributes. • Dashed ellipses denote derived attributes. • Underline indicates primary key attributes (will study later)
E-R Diagram With Composite, Multivalued, and Derived Attributes Composite Key Multivalued Derived
Relationship Sets with Attributes 如前例: SP. price/ qty
Roles • Entity sets of a relationship need not be distinct • The labels “manager” and “worker” are called roles; they specify how employee entities interact via the works_for relationship set. • Roles are indicated in E-R diagrams by labeling the lines that connect diamonds to rectangles. • Role labels are optional, and are used to clarify semantics of the relationship 經理也是員工,但他只有一個。
Cardinality Constraints • We express cardinality constraints by drawing either a directed line (), signifying “one,” or an undirected line (—), signifying “many,” between the relationship set and the entity set. • One-to-one relationship: • A customer is associated with at most one loan via the relationship borrower • A loan is associated with at most one customer via borrower
One-To-Many Relationship • In the one-to-many relationship a loan is associated with at most one customer via borrower, a customer is associated with several (including 0) loans via borrower 另以 S SP ─ P 說明之。
Many-To-One Relationships • In a many-to-one relationship a loan is associated with several (including 0) customers via borrower, a customer is associated with at most one loan via borrower 另以 S ─SP P 說明之。
Many-To-Many Relationship • A customer is associated with several (possibly 0) loans via borrower • A loan is associated with several (possibly 0) customers via borrower 另以 S ─SP ─ P 說明之。
Participation of an Entity Set in a Relationship Set • Total participation (indicated by double line): every entity in the entity set participates in at least one relationship in the relationship set • E.g. participation of loan in borrower is total • every loan must have a customer associated to it via borrower • Partial participation: some entities may not participate in any relationship in the relationship set • Example: participation of customer in borrower is partial 此圖可提醒 Programmer 設計警示 See also "SP == P" case.
Alternative Notation for Cardinality Limits • Cardinality limits can also express participation constraints 1..* 與上頁之圖同義。
One-to-Many v.s. Cardinality Limits price qty Pname 1..* PN Sname SN P SP S P SP S (1) one-to-many: PN 決定 SN, i.e., 每種物料最多只能由一家專賣 (2) SN 出現在 “SP”至少一次, i.e., 每個供貨商至少賣1種以上物料 (無箭號之 1..* 可用雙實線圖示) color weight status city 兩者效果相同,但上圖意義較明確 price qty 2..5 Pname PN Sname SN P SP S 0..1 (1) one-to-many: PN 即決定 SN (2) 每個供貨商供貨 2~5 種物料 P SP S color weight status city
E-R Diagram with a Ternary Relationship C C P P SP SP S S (SN,PN, ….., cid) (SN,PN,Cid)
Cardinality Constraints on Ternary Relationship • We allow at most one arrow out of a ternary (or greater degree) relationship to indicate a cardinality constraint • E.g. an arrow from works_on to job indicates each employee works on at most one job at any branch. • If there is more than one arrow, there are two ways of defining the meaning. • E.g a ternary relationship R between A, B and C with arrows to B and C could mean 1. each A entity is associated with a unique entity from B and C or 2. each pair of entities from (A, B) is associated with a unique C entity, and each pair (A, C) is associated with a unique B • Each alternative has been used in different formalisms • To avoid confusion we outlaw more than one arrow Discuss: 如何做表?(e.g. job) Ans: 仍要用 3E+1R, R不能合併, 雖其 P-Key (E, B, J) 只剩下(E,B)有作用。
Chapter 6: Entity-Relationship Model • 6.1. Design Process • 6.2. E-R Model • 6.3. Constraints • 6.4. E-R Diagram • 6.5. E-R Design Issues • 6.6. Weak Entity Sets • 6.7. Extended E-R Features • 6.8. Design for the Banking Enterprise • 6.9. Reduction to Relation Schemas • 6.10. Database Design • 6.11. UML
Design Issues • Use of entity sets vs. attributes (and, multivalued attributes)Choice mainly depends on the structure of the enterprise being modeled, and on the semantics associated with the attribute in question. (e.g. 電話號碼用weak relationship set 來連接) • Use of entity sets vs. relationship setsPossible guideline is to designate a relationship set to describe an action that occurs between entities (e.g. 獨立出每場比賽資料,而非以主隊客隊配對出戰組合) • Binary versus n-ary relationship setsAlthough it is possible to replace any nonbinary (n-ary, for n > 2) relationship set by a number of distinct binary relationship sets, a n-ary relationship set shows more clearly that several entities participate in a single relationship. • Placement of relationship attributes (e.g. Many-to-one 的時候 price要放在 E or R ?)
Converting Non-Binary Relationships to Binary Form • In general, any non-binary relationship can be represented using binary relationships by creating an artificial entity set. • Replace R between entity sets A, B and Cby an entity set E, and three relationship sets: 1. RA, relating E and A 2.RB, relating E and B 3. RC, relating E and C • Create a special identifying attribute for E • Add any attributes of R to E • For each relationship (ai , bi , ci) in R, create 1. a new entity eiin the entity set E 2. add (ei , ai ) to RA 3. add (ei , bi) to RB 4. add (ei , ci ) to RC RA, RB, RC變成 沒有描述屬性的 Relationship Sets
Weak Entity Sets • We depict a weak entity set by double rectangles. • We underline the discriminator of a weak entity set with a dashed line. • payment_number – discriminator of the payment entity set • Primary key for payment – (loan_number, payment_number) (1..*) (0..1) Identifying Relationship 不具有描述屬性 Example 2: 對嫌疑犯的監控 Record – (ID, timestamp) + dress, description… 弱實體集合解決了「單獨實體卻必須擁有 composite key」的關係圖問題 想像:你找不到另一個「基本資料表」來構築想要的「配對資料表」
E-R Diagram With Aggregation 將「配對資料」(R) 升格為「基本資料」(E) Note: Entity Set – Entity Set 之間不可連線,一定要 經過 E-R-E Relationship Set 之間 若要連線,其中一個 R 必須是 Aggregation Discuss: What’s the Super-Key? (E,B,J) or, given (N) (E,B,J,M) or (N,M)
Specialization Example 實作 ISA: 可分別實作 employee/ customer 亦可只作上層實體 person 基本資料表 多一欄記錄 ep/cm (1) 可空白 … partial (2) 不可空白 … total (||) 表中尚有其他欄 配合儲存其下屬性 “disjoint” 則用在實作 下層實體,其承襲2個以上 的上層實體中的某一個 可以反畫左例,讓 ep/cm 變成雙親代。 雙親代正常不加 disjoint 表示承襲兩者所有特質 父定義/ 高階型別
ISA implementation … … type num balance cid cname customer depositor account overdraw phone i_rate addr 須為兩者之一 ISA saving checking saving checking ISA ISA i_rate cid cname overdraw num addr customer account depositor balance 做為親代考慮 其中之一 (||) or 全無 (全有不可能) phone disjoint ? 做為子代考慮: 其中之一(disjoint) or 全有 (全無不可能)
E-R Diagram for a Banking Enterprise 實作 ISA: account 基本資料表 多一欄記錄 sv/ch (1) 可空白 … partial (2) 不可空白 … total (||) 表中尚有其他欄 配合註記其下屬性 “disjoint” 則用在實作 下層實體,其承襲2個以上 的上層實體中的某一個 可以反畫左例,讓 sv/ch 變成雙親代。 雙親代正常不加 disjoint 表示承襲兩者所有特質
範例一、圖書分館藏書資料 3個基本資料表、1個配對資料表 Qty Byear BN Btitle Lid Lname Pid Publisher Pub Lib Collection Book Publish Laddr Pweb Author Pyear Paddr
範例一、圖書分館藏書資料 (cont.) Qty Byear Btitle Author Lid Lname Pid Publisher BN Pub Lib Collection Book Publish Laddr Pweb Paddr Revise 4個基本資料表、 1個配對資料表 多了 Version 基本資料 其 P-key =(BN, Edition) Book 左、右兩側 原來的連線,如果 改接到 Version 上 分別是什麼意義? Edition Version Pyear 這個基本資料表的 P-Key 是複合鍵
範例二、Fat-together 餐餐團購網 開團之後列出目標店家 之所有菜色供團友訂購 4個基本資料表、1個配對資料表(ternary)
範例二、Fat-together 餐餐團購網 (conti.) 錢要隨單繳清 而非隨菜繳交 我想吃 數量