嘉義大學 管理資訊系 — 李延憲 教授 | EER 模型轉換為關聯式資料庫
✦ 每個 regular (strong) entity type E 建立一個 relation R
✦ E 的所有 single-valued simple attributes 成為 R 的 attributes
✦ E 的 primary key 成為 R 的 primary key
✦ Composite attributes 只取其 simple components
Student(SSN, FName, LName, Address)
SSN 為 primary key(底線標示),其餘為 simple attributes。
Q1: Regular entity type 轉換時,composite attribute 如何處理?
Q2: Entity type 的 PK 在轉換後的 Relation 中扮演什麼角色?
✦ 為 weak entity type W 建立 relation R
✦ 包含 W 的所有 simple attributes
✦ 複製 identifying (owner) entity 的 PK 作為 R 的 foreign key
✦ R 的 PK = owner entity 的 PK + W 的 partial key
Dependent(SSN, Name, Gender, Relation)
SSN 是來自 Employee 的 FK,Name 是 partial key。PK = {SSN, Name}。
Q1: Weak entity 的 PK 由什麼組成?
Q2: Identifying entity 的 PK 在 weak entity relation 中是什麼?
✦ 選擇其中一邊(通常是 total participation 那邊)
✦ 把另一邊的 PK 作為 FK 加入被選的 relation
✦ Relationship 本身的 attributes 也加入該 relation
✦ 也可以用 merge 或 cross-reference 方式
Department(DNo, DName, MgrSSN, MgrStartDate)
MgrSSN 是 FK 參照 Employee(SSN),MgrStartDate 是 relationship attribute。
Q1: 1:1 relationship 轉換時,FK 通常加在哪一邊?
Q2: 1:1 relationship 的 attributes 放在哪裡?
✦ 把 "one-side" entity 的 PK 加入 "many-side" entity 的 relation 作為 FK
✦ Relationship 的 attributes 也加入 many-side 的 relation
✦ 不需要建立新的 relation
Employee(SSN, FName, LName, DNo)
DNo 是 FK 參照 Department(DNo)。FK 加在 many-side (Employee)。
Q1: 1:M relationship 中,FK 加在哪一邊?
Q2: Department(1) — WorkFor — Employee(M),轉換後 DNo 出現在哪?
✦ 建立新的 relation R 代表此 relationship
✦ 兩邊 entity 的 PK 都作為 R 的 FK
✦ R 的 PK = 兩個 FK 的組合
✦ Relationship 的 attributes 也加入 R
Works_On(SSN, PNumber, Hours)
SSN 是 FK→Employee,PNumber 是 FK→Project。PK = {SSN, PNumber}。Hours 是 relationship attribute。
Q1: M:N relationship 轉換時需要?
Q2: 新建 relation 的 PK 是什麼?
✦ 建立新的 relation R
✦ 所有參與 entity 的 PK 都作為 R 的 FK
✦ PK 通常是所有 FK 的組合
✦ 例外:若某 entity 的 max cardinality 是 1,其 FK 不加入 PK
Work_on(S_SSN, A_SSN, Title)
三個 FK 分別參照 Student、Advisor、Thesis。PK = 三者組合。
Q1: N-ary relationship 中,若某 entity 的 max cardinality 是 1,其 FK 是否加入 PK?
Q2: Ternary relationship 轉換後,新 relation 至少有幾個 FK?
有 4 種轉換方式(Options A, B, C-1, C-2):
每個 subclass relation 包含 superclass 的 PK 作為 FK(也是自己的 PK),加上 subclass 特有的 attributes。
Person(SSN, Name, BDate)
Employee(SSN, Salary) — SSN 是 FK→Person
Student(SSN, GPA) — SSN 是 FK→Person
每個 subclass relation 包含 superclass 的所有 attributes(複製)。適用於 total, disjoint specialization。
Employee(SSN, Name, BDate, Salary)
Student(SSN, Name, BDate, GPA)
加一個 type attribute 區分 subclass,所有 subclass attributes 都放在 superclass relation。
Person(SSN, Name, BDate, Type, Salary, GPA)
Type ∈ {'Employee', 'Student'}。不屬於該 type 的欄位為 NULL。
加 boolean flags(每個 subclass 一個 flag),所有 subclass attributes 都放在 superclass relation。
Person(SSN, Name, BDate, IsEmployee, IsStudent, Salary, GPA)
一個人可以同時是 Employee 和 Student。
Q1: 哪個 Option 適合 total, disjoint specialization 且不想保留 superclass relation?
Q2: Overlapping specialization 適合用哪個 Option?
Q3: Option A 中,subclass relation 的 PK 是什麼?
✦ 為每個 multi-valued attribute A 建立新的 relation R
✦ R 包含 A 本身 + owner entity 的 PK 作為 FK
✦ R 的 PK = FK + A
Phones(SSN, Phone)
SSN 是 FK→Employee。PK = {SSN, Phone}。
Q1: Multi-valued attribute 為什麼不能直接放在原 relation?
Q2: Multi-valued attribute relation 的 PK 是什麼?
✦ 為 category 建立新的 relation,加入 surrogate key
✦ 每個 superclass 的 relation 加入 FK 指向 category relation 的 surrogate key
✦ Category relation 包含 category 共有的 attributes
Owner(OwnerId, ...category attributes...)
Person(SSN, Name, OwnerId)
Company(CName, Address, OwnerId)
OwnerId 是 surrogate key。Person 和 Company 各加 FK 指向 Owner。
Q1: Category (Union Type) 為什麼需要 surrogate key?
Q2: Category 轉換後,FK 加在哪裡?
• Employee(strong entity):eId(PK), pId, name, birthday, department, balance, phones(多值)
• Store(strong entity):sName(PK), address, phone, description
• FoodItem(weak entity, owner=Store):fName(partial key), description, unitPrice, discount
• Order(weak entity, owner=Employee):date(partial key), description, salePrice
• Deposit(weak entity, owner=Employee):dTime(partial key), amount
• Contains(M:N between Order & FoodItem):num
1. Employee(eId, pId, name, birthday, department, balance)
2. Store(sName, address, phone, description)
3. FoodItem(sName, fName, description, unitPrice, discount)
→ Rule 2: sName FK→Store, PK={sName, fName}
4. Order(eId, date, description, salePrice)
→ Rule 2: eId FK→Employee, PK={eId, date}
5. Deposit(eId, dTime, amount)
→ Rule 2: eId FK→Employee, PK={eId, dTime}
6. Contains(eId, date, sName, fName, num)
→ Rule 5: M:N between Order & FoodItem
7. Phones(eId, phone)
→ Rule 8: multi-valued attribute
Q1: FoodItem 是 weak entity,它的 PK 是什麼?
Q2: Contains relation 的 PK 包含幾個 attributes?
Q3: 這個系統總共產生幾個 Relations?
• Course:cNo(PK), cName, cDesc
• Teacher:tNo(PK), tName, title, departments(多值)
• Student:sId(PK), sName, gender, bDate, email
• Item(weak entity, owner=Course):iName(partial key), dueDate
• Teaches(M:N, Teacher-Course)
• Takes(M:N, Student-Course):finalScore
• Evaluated(ternary: Student-Course-Item):score
1. Course(cNo, cName, cDesc)
2. Teacher(tNo, tName, title)
3. Student(sId, sName, gender, bDate, email)
4. Item(cNo, iName, dueDate)
→ Rule 2: cNo FK→Course, PK={cNo, iName}
5. Teaches(tNo, cNo)
→ Rule 5: M:N
6. Takes(sId, cNo, finalScore)
→ Rule 5: M:N
7. Evaluated(sId, cNo, iName, score)
→ Rule 6: ternary relationship
8. Departments(tNo, department)
→ Rule 8: multi-valued attribute
Q1: Evaluated 是什麼類型的 relationship?
Q2: Teacher 的 departments 用哪條規則轉換?
• Member:mId(PK), name, email, startDate
• Merchandise(weak entity, owner=Member):seqNo(partial key), name, description, expired, bottomPrice
• Category:cId(PK), description, parentCId(FK, self-referencing)
• Bid:bId(PK), price, dateTime, mId(FK→bidder), ownerMId+seqNo(FK→Merchandise)
• Purchases(M:N, Member-Merchandise):price
• Categorized(M:N, Merchandise-Category)
1. Member(mId, name, email, startDate)
2. Merchandise(mId, seqNo, name, description, expired, bottomPrice)
→ Rule 2: mId FK→Member (owner), PK={mId, seqNo}
3. Category(cId, description, parentCId)
→ Rule 1 + self-referencing FK
4. Bid(bId, price, dateTime, mId, ownerMId, seqNo)
→ Rule 1: mId FK→Member(bidder), {ownerMId,seqNo} FK→Merchandise
5. Purchases(mId, ownerMId, seqNo, price)
→ Rule 5: M:N
6. Categorized(ownerMId, seqNo, cId)
→ Rule 5: M:N
Q1: Category 的 parentCId 是什麼類型的 FK?
Q2: Bid relation 中有幾個 FK?
• Department:dNo(PK), dName, dCat, startDate
• Doctor:dId(PK), pId, name, birthday, position, dNo(FK→Department), chiefDNo(FK→Department)
• DiagnosisTime(weak entity, owner=Doctor):startDateTime(partial key), capacity
• Patient:pNo(PK), pId, name, sex, birthday
• Registers(M:N, Patient-DiagnosisTime):no, estimatedTime
1. Department(dNo, dName, dCat, startDate)
2. Doctor(dId, pId, name, birthday, position, dNo, chiefDNo)
→ Rule 1 + Rule 4: dNo FK→Department(所屬), chiefDNo FK→Department(主管)
3. DiagnosisTime(dId, startDateTime, capacity)
→ Rule 2: dId FK→Doctor, PK={dId, startDateTime}
4. Patient(pNo, pId, name, sex, birthday)
5. Registers(pNo, dId, startDateTime, no, estimatedTime)
→ Rule 5: M:N between Patient & DiagnosisTime
Q1: Doctor relation 中有幾個 FK?
Q2: Registers 的 PK 包含哪些 attributes?
• 繼承 #4 的所有 entities
• Doctor specialization(disjoint, Option A):
- Resident:residenceDate, mentorDId(FK→Doctor)
- AttendingDoc:position
- ChiefDoc:chargeDate, position
• Specialties(multi-valued attribute of Doctor)
• Inpatient(subtype of Patient):startDate, treatingDId(FK), assistingDId(FK)
• Room:rNo(PK), loc
• Occupies(N:1, Inpatient-Room)
(保留 #4 的 5 個 Relations,以下為新增)
6. Resident(dId, residenceDate, mentorDId)
→ Rule 7 Option A: dId FK→Doctor, mentorDId FK→Doctor
7. AttendingDoc(dId, position)
→ Rule 7 Option A: dId FK→Doctor
8. ChiefDoc(dId, chargeDate, position)
→ Rule 7 Option A: dId FK→Doctor
9. Specialties(dId, specialty)
→ Rule 8: multi-valued attribute
10. Inpatient(pNo, startDate, treatingDId, assistingDId, rNo)
→ Rule 7 Option A + Rule 4: rNo FK→Room (Occupies N:1)
11. Room(rNo, loc)
Q1: Doctor 的 specialization 使用哪個 Option?
Q2: Inpatient 的 Occupies (N:1) 關係如何轉換?
• EntityType:eName(PK), color, location
• WeakEntityType:wName(PK), color, location
• RelationshipType:rName(PK), color, location, degree
• Attribute:aId(PK), aName, domain, isSingleValued
• Has(1:N, EntityType-Attribute):isKey
• Involves(M:N, EntityType-RelationshipType):min, max
• R_Has(1:N, RelationshipType-Attribute)
• Identifies(1:1, WeakEntityType-RelationshipType)
• W_Has(1:N, WeakEntityType-Attribute):isPartialKey
• W_Involves(M:N, WeakEntityType-RelationshipType):min, max
• Composes(1:N self-referencing on Attribute)
1. EntityType(eName, color, location)
2. WeakEntityType(wName, color, location)
3. RelationshipType(rName, color, location, degree)
4. Attribute(aId, aName, domain, isSingleValued, compositeAId)
→ Rule 1 + Composes(1:N self): compositeAId FK→Attribute(aId)
5. Has(aId, eName, isKey)
→ Rule 4: 1:N, eName FK→EntityType。因 Attribute 可被多個 Entity 擁有,用 aId 作 PK 或加入 eName 作為 FK
6. Involves(eName, rName, min, max)
→ Rule 5: M:N
7. R_Has(aId, rName)
→ Rule 4: 1:N, rName FK→RelationshipType
8. Identifies(wName, rName)
→ Rule 3: 1:1
9. W_Has(aId, wName, isPartialKey)
→ Rule 4: 1:N
10. W_Involves(wName, rName, min, max)
→ Rule 5: M:N
11. Composes — 已合併到 Attribute 的 compositeAId
→ Rule 4: 1:N self-referencing
Q1: Composes 是什麼類型的 relationship?
Q2: 這個系統總共產生幾個 Relations?
完成學習任務解鎖成就!