楊立偉教授 台灣大學工管系 2013 fall 1 chapter 5: logical database design and the...

55
楊楊楊楊楊 楊楊楊楊楊楊楊 2013 Fall 2013 Fall 1 Chapter 5: Chapter 5: Logical Database Logical Database Design and the Design and the Relational Model Relational Model : : 11 11 註註 註註 Chapter 4 Chapter 4

Upload: edwin-turner

Post on 01-Jan-2016

236 views

Category:

Documents


1 download

TRANSCRIPT

楊立偉教授台灣大學工管系

2013 Fall2013 Fall 11

Chapter 5:Chapter 5:Logical Database Design Logical Database Design and the Relational Modeland the Relational Model

註 註 :: 於於 1111 版為版為 Chapter 4Chapter 4

Chapter 5 22

RelationRelation Relation is a named, two-dimensional table of data Relation is a named, two-dimensional table of data 二二

維的表格維的表格 Table consists of rows Table consists of rows 列 列 (records (records 紀錄紀錄 ) and columns ) and columns

欄 欄 (attribute (attribute 屬性 屬性 or field or field 欄位欄位 )) Requirements for a table to qualify as a Requirements for a table to qualify as a

relation:relation: It must have a unique nameIt must have a unique name Every attribute value must be atomic (Every attribute value must be atomic (not multivalued, not not multivalued, not

compositecomposite)) Every row must be unique (can’t have two rows with exactly Every row must be unique (can’t have two rows with exactly

the same values for all their fields) the same values for all their fields) 不可以有兩列是完全一樣的不可以有兩列是完全一樣的 Attributes (columns) in tables must have unique namesAttributes (columns) in tables must have unique names The order of the columns must be irrelevant The order of the columns must be irrelevant 欄位順序不重要欄位順序不重要 The order of the rows must be irrelevant The order of the rows must be irrelevant 紀錄順序不重要紀錄順序不重要

Chapter 5 33

Correspondence with E-R Correspondence with E-R ModelModel

Relations (tables) correspond with entity Relations (tables) correspond with entity types and with many-to-many relationship types and with many-to-many relationship typestypes個體、多對多關係、有屬性的關係,應該轉成個體、多對多關係、有屬性的關係,應該轉成 TableTable

Rows correspond with entity instances and Rows correspond with entity instances and with many-to-many relationship instanceswith many-to-many relationship instances

Columns correspond with attributesColumns correspond with attributes

NOTE: The word NOTE: The word relationrelation (in relational database) is NOT (in relational database) is NOT

the same as the word the same as the word relationshiprelationship (in E-R model) (in E-R model)

從 E-R model 來建立表格的對應方法

Chapter 5 44

Key FieldsKey Fields Keys are special fields that serve two main Keys are special fields that serve two main

purposes:purposes: Primary keys Primary keys 主鍵主鍵 are are uniqueunique identifiers of the relation. identifiers of the relation.

Examples include employee numbers, social security Examples include employee numbers, social security numbers, etc. numbers, etc. This is how we can guarantee that all rows This is how we can guarantee that all rows are uniqueare unique

Foreign keys Foreign keys 外鍵 外鍵 are identifiers that enable a are identifiers that enable a dependentdependent relation (on the many side of a relationship) to refer to its relation (on the many side of a relationship) to refer to its parentparent relation (on the one side of the relationship) relation (on the one side of the relationship)

Keys can be Keys can be simplesimple (a single field) or (a single field) or compositecomposite (more than one field)(more than one field)

Note: keys usually are used as indexes Note: keys usually are used as indexes

to speed up the response to user queriesto speed up the response to user queries

Chapter 5 55

Primary Key

Foreign Key (implements 1:N relationship between customer and order)

Combined, these are a composite primary key (uniquely identifies the order line)…individually they are foreign keys (implement M:N relationship between order and product)

Figure 5-3 Schema for four relations (Pine Valley Furniture Company)

E-R model 可參考課本 Figure 3-22

Chapter 5 66

對應下列 對應下列 E-R modelE-R model

Chapter 5 77

Integrity ConstraintsIntegrity Constraints

Domain ConstraintsDomain Constraints Allowable values for an attribute. See Table 5-Allowable values for an attribute. See Table 5-

11 Entity IntegrityEntity Integrity

No primary key attribute may be null. All No primary key attribute may be null. All primary key fields primary key fields MUSTMUST have data have data 主鍵欄位一主鍵欄位一定要有值定要有值

Chapter 5 88

Domain definitions enforce domain integrity constraints

Chapter 5 99

Integrity ConstraintsIntegrity Constraints Referential Integrity Referential Integrity 參照完整性 參照完整性 – – any foreign key value any foreign key value

(on the relation of the many side) MUST match a primary (on the relation of the many side) MUST match a primary key value in the relation of the one side. (Or the foreign key value in the relation of the one side. (Or the foreign key can be null) key can be null) 外鍵一定是指向某筆紀錄外鍵一定是指向某筆紀錄 , , 或者為空或者為空 For example: Delete RulesFor example: Delete Rules

在刪除資料時,為了保持參照完整性的三種做法在刪除資料時,為了保持參照完整性的三種做法 Restrict – don’t allow delete of “parent” side if related rows Restrict – don’t allow delete of “parent” side if related rows

exist in “dependent” sideexist in “dependent” side Cascade – automatically delete “dependent” side rows that Cascade – automatically delete “dependent” side rows that

correspond with the “parent” side row to be deletedcorrespond with the “parent” side row to be deleted Set-to-Null – set the foreign key in the dependent side to null if Set-to-Null – set the foreign key in the dependent side to null if

deleting from the parent side deleting from the parent side not allowed for weak entities not allowed for weak entities

Chapter 5 1010

Figure 5-5 Referential integrity constraints (Pine Valley Furniture)

Referential integrity

constraints are drawn via arrows from dependent to

parent table( 箭號指向 parent)

Chapter 5 1111

Figure 5-6 SQL table definitions

Referential integrity

constraints are implemented with

foreign key to primary key references

SQL 四大指令之一 ( 語法下次會教 )

Chapter 5 1212

Transforming E-R DiagramsTransforming E-R Diagramsinto Relations (1)into Relations (1)

Mapping Regular Entities to Relations Mapping Regular Entities to Relations 1.1. Simple attributes: E-R attributes map Simple attributes: E-R attributes map

directly onto the relationdirectly onto the relation

2.2. Composite attributes: Use only their Composite attributes: Use only their simple, component attributes simple, component attributes 複合屬性則複合屬性則個別建立對應欄位個別建立對應欄位

3.3. Multivalued Attribute: Becomes a Multivalued Attribute: Becomes a separate relation with a foreign key separate relation with a foreign key taken from the superior entity taken from the superior entity 多值欄位多值欄位應獨立成應獨立成 TableTable

Chapter 5 1313

(a) CUSTOMER entity type with simple attributes

Figure 5-8 Mapping a regular entity

(b) CUSTOMER relation

Chapter 5 1414

(a) CUSTOMER entity type with composite attribute

Figure 5-9 Mapping a composite attribute

(b) CUSTOMER relation with address detail

Chapter 5 1515

Figure 5-10 Mapping an entity with a multivalued attribute

One–to–many relationship between original entity and new relation

(a)

Multivalued attribute becomes a separate relation with foreign key

(b)

Chapter 5 1616

Transforming E-R DiagramsTransforming E-R Diagramsinto Relations (2)into Relations (2)

Mapping Weak EntitiesMapping Weak Entities Becomes a separate relation with a Becomes a separate relation with a

foreign key taken from the superior foreign key taken from the superior entityentity

Primary key composed of:Primary key composed of: Partial identifier of weak entity Partial identifier of weak entity 部份自己的部份自己的 IDID 欄欄

位位 Primary key of identifying relation (strong Primary key of identifying relation (strong

entity) entity) 加上加上 ParentParent 的主鍵欄位的主鍵欄位

Chapter 5 1717

Figure 5-11 Example of mapping a weak entity

a) Weak entity DEPENDENT

扶養親屬

Chapter 5 1818

NOTE: the domain constraint for the foreign key should NOT allow null value if DEPENDENT is a weak entity

Foreign key

Composite primary key

Figure 5-11 Example of mapping a weak entity (cont.)

b) Relations resulting from weak entity

Chapter 5 1919

Transforming E-R DiagramsTransforming E-R Diagramsinto Relations (3)into Relations (3)

Mapping Binary RelationshipsMapping Binary Relationships One-to-Many – Primary key on the one side One-to-Many – Primary key on the one side

becomes a foreign key on the many sidebecomes a foreign key on the many side Many-to-Many – Create a Many-to-Many – Create a new relationnew relation

with the primary keys of the two entities as with the primary keys of the two entities as its primary keyits primary key

One-to-One – Primary key on the mandatory One-to-One – Primary key on the mandatory side becomes a foreign key on the optional side becomes a foreign key on the optional sideside

Chapter 5 2020

Figure 5-12 Example of mapping a 1:M relationship

a) Relationship between customers and orders

Note the mandatory one

b) Mapping the relationship

Again, no null value in the foreign key…this is because of the mandatory minimum cardinality

Foreign key

Chapter 5 2121

Figure 5-13 Example of mapping an M:N relationship

a) Completes relationship (M:N)

The Completes relationship will need to become a separate relation需要新開一個 Table ( 假設取名為 Certificate 結業證書 )

Chapter 5 2222

New intersection

relation

Foreign key

Foreign key

Composite primary key

Figure 5-13 Example of mapping an M:N relationship (cont.)

b) Three resulting relations

Chapter 5 2323

Figure 5-14 Example of mapping a binary 1:1 relationship

a) In_charge relationship (1:1)

Often in 1:1 relationships, one direction is optional

Chapter 5 2424

b) Resulting relations

Figure 5-14 Example of mapping a binary 1:1 relationship (cont.)

Foreign key goes in the relation on the optional side,matching the primary key on the mandatory side

通常 1:1 關係 , 在其中一張 Table 加上外鍵欄位即可→ 挑哪一張 Table 較佳 ?

為什麼 ?

Chapter 5 2525

Transforming E-R DiagramsTransforming E-R Diagramsinto Relations (4)into Relations (4)

Mapping Associative EntitiesMapping Associative Entities Identifier Not Assigned Identifier Not Assigned 未特別給定未特別給定 IDID

者者 Default primary key for the association Default primary key for the association

relation is composed of the primary keys relation is composed of the primary keys of the two entities (as in M:N relationship)of the two entities (as in M:N relationship)

Identifier Assigned Identifier Assigned 另給定另給定 IDID 者者 It is natural and familiar to end-usersIt is natural and familiar to end-users Default identifier may not be uniqueDefault identifier may not be unique

Chapter 5 2626

Figure 5-15 Example of mapping an associative entity

a) An associative entity

Chapter 5 2727

Figure 5-15 Example of mapping an associative entity (cont.)

b) Three resulting relations

Composite primary key formed from the two foreign keys

Chapter 5 2828

Figure 5-16 Example of mapping an associative entity with an identifier

a) SHIPMENT associative entity

Chapter 5 2929

Figure 5-16 Example of mapping an associative entity with an identifier (cont.)

b) Three resulting relations

Primary key differs from foreign keys

Chapter 5 3030

Transforming E-R DiagramsTransforming E-R Diagramsinto Relations (5)into Relations (5)

Mapping Unary RelationshipsMapping Unary Relationships One-to-Many – Recursive foreign key in the One-to-Many – Recursive foreign key in the

same relation same relation 直接加外鍵欄位即可 Many-to-Many – Two relations: Many-to-Many – Two relations: 要多開一張

Table One for the entity typeOne for the entity type One for an associative relation in which One for an associative relation in which

the primary key has two attributes, both the primary key has two attributes, both taken from the primary key of the entitytaken from the primary key of the entity

Chapter 5 3131

Figure 5-17 Mapping a unary 1:N relationship

(a) EMPLOYEE entity with unary relationship

(b) EMPLOYEE relation with recursive foreign key

Chapter 5 3232

Figure 5-18 Mapping a unary M:N relationship

(a) Bill-of-materials relationships (M:N)

(b) ITEM and COMPONENT relations

Chapter 5 3333

Transforming E-R Diagrams into Transforming E-R Diagrams into Relations (6)Relations (6)

Mapping Ternary (and n-ary) Mapping Ternary (and n-ary) RelationshipsRelationships One relation for each entity and one for One relation for each entity and one for

the associative entitythe associative entity Associative entity has foreign keys to Associative entity has foreign keys to

each entity in the relationshipeach entity in the relationship

Chapter 5 3434

Figure 5-19 Mapping a ternary relationship

a) PATIENT TREATMENT Ternary relationship with associative entity

內科醫師

病患 處方看病紀錄

Chapter 5 3535

b) Mapping the ternary relationship PATIENT TREATMENT

Remember that the

primary key MUST be

unique

Figure 5-19 Mapping a ternary relationship (cont.)

This is why treatment date and time are

included in the composite

primary key

But this makes a very

cumbersome key…

It would be better to create a

surrogate key like Treatment #

主鍵是複合欄位而且又很長時考慮直接給一個編號當主鍵

Chapter 5 3636

Data Normalization Data Normalization 正規化正規化 To validate and improve a logical To validate and improve a logical

design so that it satisfies certain design so that it satisfies certain constraints that constraints that avoid avoid unnecessary duplication of unnecessary duplication of data data 避免資料重複與各種異象

The process of decomposing The process of decomposing relations with anomalies to produce relations with anomalies to produce smaller, smaller, well-structuredwell-structured relations relations切成較小 , 結構較好的表

Chapter 5 3737

Well-Structured RelationsWell-Structured Relations A relation that contains A relation that contains minimal data minimal data

redundancyredundancy and allows users to insert, delete, and allows users to insert, delete, and update rows and update rows without causing data without causing data inconsistenciesinconsistencies

Goal is to avoid anomalies Goal is to avoid anomalies 為了避免異象為了避免異象 Insertion Anomaly Insertion Anomaly – adding new rows forces user – adding new rows forces user

to create duplicate datato create duplicate data Deletion Anomaly Deletion Anomaly – deleting rows may cause a loss – deleting rows may cause a loss

of data that would be needed for other future rowsof data that would be needed for other future rows Modification Anomaly Modification Anomaly – changing data in a row – changing data in a row

forces changes to other rows because of duplicationforces changes to other rows because of duplication

General rule of thumb: A table should not pertain to more than one entity type 一張表不該表示超過一個 Entity

Chapter 5 3838

Example–Figure 5-2bExample–Figure 5-2b

Question–Is this a relation?

Answer – Yes: Unique rows and no multivalued attributes (by definition)

Question–What’s the primary key? Answer – Composite: Emp_ID, Course_Title

Chapter 5 3939

Anomalies in this TableAnomalies in this Table Insertion Insertion – can’t enter a new employee without – can’t enter a new employee without

having the employee take a classhaving the employee take a class Deletion Deletion – if we remove employee 140, we lose – if we remove employee 140, we lose

information about the existence of a Tax Acc information about the existence of a Tax Acc classclass

Modification Modification – giving a salary increase to – giving a salary increase to employee 100 forces us to update multiple employee 100 forces us to update multiple recordsrecordsWhy do these anomalies exist?

Because there are two entity types in this one relation. This results in data duplication and an unnecessary dependency between the entities

「一張大表」造成了不必要的資料重複與相依性

問題出在這「一張大表」 企圖同時表示多個 Entity

Chapter 5 4040

Functional Dependencies and Functional Dependencies and KeysKeys

Functional Dependency: The value of one Functional Dependency: The value of one attribute (the attribute (the determinantdeterminant) determines ) determines the value of another attribute the value of another attribute 相依 : 一個欄位是受另一個欄位所決定

Candidate Key:Candidate Key: A unique identifier. One of the candidate A unique identifier. One of the candidate

keys will become the primary keykeys will become the primary key E.g. perhaps there is both credit card number and E.g. perhaps there is both credit card number and

SSN in a table…in this case both are candidate SSN in a table…in this case both are candidate keyskeys

Each non-key field is functionally dependent Each non-key field is functionally dependent on every candidate keyon every candidate key

Chapter 5 4141

Figure 5.22 Steps in normalization

一般要滿足第三正規化即可

Chapter 5 4242

First Normal Form First Normal Form 第一正規化第一正規化 No multivalued attributes No multivalued attributes 不可有多值的不可有多值的

欄位欄位 Every attribute value is atomicEvery attribute value is atomic Fig. 5-25 Fig. 5-25 is notis not in 1 in 1stst Normal Form Normal Form

(multivalued attributes) (multivalued attributes) it is not a it is not a relationrelation

Fig. 5-26 Fig. 5-26 isis in 1 in 1stst Normal form Normal form All relationsAll relations are in 1 are in 1stst Normal Normal

FormForm

Chapter 5 4343

Table with multivalued attributes, not in 1st normal form

Note: this is NOT a relation

多值放在同一筆發票紀錄中

Chapter 5 4444

Table with no multivalued attributes (in 1st normal form)

Note: this is a relation (but not a well-structured one yet)

Chapter 5 4545

Anomalies in this TableAnomalies in this Table Insertion Insertion – if new product is ordered for order – if new product is ordered for order

1007 of existing customer, 1007 of existing customer, customer data must be re-entered, causing duplication, causing duplication

DeletionDeletion–if we delete the Dining Table from –if we delete the Dining Table from Order 1006, Order 1006, we lose information concerning this item's finish and price

Update Update – changing the price of product ID 4 – changing the price of product ID 4 requires update in several records

Why do these anomalies exist? Because there are multiple entity types in one relation. This results in duplication and an unnecessary dependency between the entities

Chapter 5 4646

Second Normal Form Second Normal Form 第二正第二正規化規化

1NF PLUS 1NF PLUS every non-key attribute is every non-key attribute is fully functionally dependent on fully functionally dependent on the the ENTIREENTIRE primary key primary key 每個非鍵值欄位每個非鍵值欄位 , , 只能與整個主鍵相依只能與整個主鍵相依 Every non-key attribute must be defined Every non-key attribute must be defined

by the entire key, not by only part of the by the entire key, not by only part of the keykey

No partial functional dependenciesNo partial functional dependencies 不能只與不能只與部份主鍵相依部份主鍵相依

Chapter 5

2NF PLUS 2NF PLUS no no transitivetransitive dependenciesdependencies (functional dependencies (functional dependencies on non-primary-key attributes)on non-primary-key attributes)

每個非鍵值欄位每個非鍵值欄位 , , 不能與任何非鍵值欄位相依 不能與任何非鍵值欄位相依 (( 反面再講一次反面再講一次 , , 語氣更強語氣更強 )) Note: This is called transitive, because the Note: This is called transitive, because the

primary key is a determinant for another primary key is a determinant for another attribute, which in turn is a determinant for a attribute, which in turn is a determinant for a thirdthird

Solution: Solution: Non-key determinant with transitive Non-key determinant with transitive dependencies go into a new tabledependencies go into a new table; non-key ; non-key determinant becomes primary key in the new determinant becomes primary key in the new table and stays as foreign key in the old table table and stays as foreign key in the old table

4747

Third Normal Form Third Normal Form 第三正規化第三正規化

Chapter 5 4848

Order_ID Order_Date, Customer_ID, Customer_Name, Customer_Address

Therefore, NOT in 2nd Normal Form

Customer_ID Customer_Name, Customer_Address

Product_ID Product_Description, Product_Finish, Unit_Price

Order_ID, Product_ID Order_Quantity

Figure 5-27 Functional dependency diagram for INVOICE

Chapter 54949

Partial dependencies are removed, but there are still transitive dependencies

Getting it into Getting it into Second Normal Second Normal FormForm

Figure 5-28 Removing partial dependencies

Chapter 5 5050

Transitive dependencies are removed

Figure 5-29 Removing partial dependencies

Getting it into Getting it into Third Normal Third Normal FormForm

Chapter 5

實務技巧

該不該切表格 一張大表的唯一優點

查詢快速 , 不需組合 多張小表優點

更新方便 , 不會有不一致的怪現象 何時該切表格

「有些欄位是跟隨某個欄位的」

Chapter 5

練習範例 大潤發的銷售紀錄表

有哪些欄位彼此相依? 只要留哪些必要欄位?

Chapter 5 5353

Merging RelationsMerging Relations Combining entities from multiple E-R models into Combining entities from multiple E-R models into

common relations common relations 合併多個合併多個 E-R modelsE-R models 成一組表格成一組表格

when merging entities from different ER models when merging entities from different ER models 合併要注意的事 合併要注意的事 :: Synonyms Synonyms 異名同義詞問題異名同義詞問題 ?? – two or more attributes – two or more attributes

with different names but same meaning with different names but same meaning Ex. Author CreatorEx. Author Creator Homonyms Homonyms 同名異義詞問題同名異義詞問題 ?? –attributes with same –attributes with same

name but different meanings name but different meanings Ex. Price (means unit or total?)Ex. Price (means unit or total?)

Transitive dependencies – even if relations are in 3NF Transitive dependencies – even if relations are in 3NF prior to merging, they may not be after merging prior to merging, they may not be after merging 合併合併之之後要再檢查第三正規化後要再檢查第三正規化

Chapter 5 5454

Enterprise KeysEnterprise Keys

Primary keys that are unique in the whole Primary keys that are unique in the whole database, not just within a single relation database, not just within a single relation 整個整個資料庫內都可用的主鍵資料庫內都可用的主鍵

Corresponds with the concept of an object ID Corresponds with the concept of an object ID in object-oriented systemsin object-oriented systems

Chapter 5 5555

Figure 5-31 Enterprise keys

a) Relations with enterprise key

b) Sample data with enterprise key