normalization of database tables

Post on 01-Jan-2016

75 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Normalization of Database Tables. การ Normalize คือหลักการออกแบบฐานข้อมูล คิดค้นโดย ดร.คอร์ด การทำ Normalize ในแต่ละระดับจะช่วยแยก Attribute ที่ซ้ำซ้อนกันออกไป. วัตถุประสงค์การเรียนรู้. สามารถอธิบายขั้นตอนการทำ Normalization ในแต่ละรูปแบบได้ - PowerPoint PPT Presentation

TRANSCRIPT

Normalization of Database Tables

การ Normalize คื�อหลักการออกแบบฐานข้�อมู�ลั คื�ดคื�นโดย

ดร.คือร�ด การทำ�า Normalize ในแต่�ลัะระดบจะช่�วยแยก Attribute

ทำ!"ซ้ำ�$าซ้ำ�อนกนออกไป

วั�ตถุ�ประสงค์ การเร�ยนร��

1 .สามูารถอธิ�บายข้$นต่อนการทำ�า Normalization ในแต่�ลัะร�ปแบบได�

2. มู!คืวามูเข้�าใจในการทำ�า Normalizationในแต่�ลัะร�ปแบบ

วั�ตถุ�ประสงค์ ของการทำ�า Normalization

1 .เพื่�"อลัดเน�$อทำ!"ในการจดเก,บข้�อมู�ลั2. เพื่�"อลัดป-ญหาทำ!"ข้�อมู�ลัไมู�ถ�ก

ต่�อง(Inconsistency)3. เป0นการลัดป-ญหาทำ!"เก�ดจากการเพื่�"มู

ปรบปร1งแลัะลับข้�อมู�ลั(Insert, Update and Delete Anomalies)

Series of stages called normal forms:

(First Normal Form : 1NF) (Second Normal Form : 2NF) (Third Normal Form : 3NF) (Boyce/Codd Normal Form :

BCNF) (Fourth Normal Form : 4NF) (Fifth Normal Form : 5NF)

The term first normal form (1NF) All the key attribute are defined

There are no repeating groups in the table

All attributes are dependent on the primary key

First Normal Form : 1NFRelation หน2"งๆ จะอย��ในร�ปแบบ

1 NF ก,ต่�อเมู�"อ “คื�าข้อง Attribute ต่�างๆ ในแต่�ลัะ

Tuple จะมู!คื�าข้องข้�อมู�ลัเพื่!ยงคื�าเด!ยว น"นคื�อไมู�มู! Repeating Group แลัะ

Multi-valued”

First Normal Form : 1NF

S# SNAME CITY P# QTY

S1 Henry London P1 300

P2 200

P3 400

P4 200

P5 100

P6 100

S2 Vivian Paris P1 300

P2 400

S3 Harry Paris P2 200

S4 James London P2 200

P4 300

P5 400

S# SNAME CITY P# QTY

S1 Henry London P1 300

S1 Henry London P2 200

S1 Henry London P3 400

S1 Henry London P4 200

S1 Henry London P5 100

S1 Henry London P6 100

S2 Vivian Paris P1 300

S2 Vivian Paris P2 400

S3 Harry Paris P2 200

S4 James London P2 200

S4 James London P4 300

S4 James London P5 400

คืวามูผิ�ดพื่ลัาดบางอย�างทำ!"อาจเก�ดข้2$นกบข้�อมู�ลัใน Relation ก,ยงมู!อย��

เช่�น1. Insert Anomaly การทำ!"จะเพื่�"มู

ข้�อมู�ลัข้องผิ��ผิลั�ต่จะทำ�าได�ต่�อเมู�"อผิ��ผิลั�ต่รายน$นมู!การส�งส�นคื�าไปให�ผิ��ซ้ำ�$อ ร!เลัช่"น

จะไมู�แสดงรายลัะเอ!ยดข้องผิ��ผิลั�ต่ S5 ทำ!"อย��ทำ!" Athens ถ�ายงไมู�มู!การส�งส�นคื�า ดงน$นหากมู!เพื่!ยงข้�อมู�ลัข้องผิ��ผิลั�ต่แต่�ยงไมู�เคืยได�รบการส"งส�นคื�าก,จะเพื่�"มู

ข้�อมู�ลัข้องผิ��ผิลั�ต่ไมู�ได�

First Normal Form : 1NF

2. Delete Anomaly ในการลับข้�อมู�ลับาง Tuple จะลับทำ$งข้�อมู�ลัผิ��ผิลั�ต่

แลัะข้�อมู�ลัการส�งส�นคื�า ป-ญหาข้อง Relation น!$คื�อ Relation น!$ประกอบด�วย Attribute มูากเก�นไปโดยทำ!"ข้�อมู�ลั

บาง Attribute อาจไมู�จ�าเป0นต่�องใช่�งานแต่�มูาผิ�กต่�ดกบ Attribute ทำ!"ต่�อง

ใช่�งาน

First Normal Form : 1NF

3. Update Anomaly การปรบปร1งจะทำ�าให�ย1�งยากแลัะเส!ยเวลัา รวมูทำ$งอาจ

ก�อให�เก�ดคืวามูผิ�ดพื่ลัาดทำ!"ข้�อมู�ลัไมู�เหมู�อนกน เช่�น เปลั!"ยนช่�"อเมู�องข้อง S1

เป0น New York

First Normal Form : 1NF

ป-ญหาทำ!"เก�ดข้2$น สามูารถแก�ไข้ได�โดยแต่ก Relation

(Decomposition) เป0น 2Relation คื�อ Relation SUPPLIER

แลัะ Relation ORDERSUPPLIER(S#, SNAME, CITY)

ORDER(S#, P#, QTY)

First Normal Form : 1NF

A table is in second normal form(2NF) if:

It is in 1NF It includes no partial dependencies;

that is, no attribute is dependent on only a portion of the primary key.

Second Normal Form : 2NFRelation หน2"งๆ จะอย��ในร�ปแบบ 2NF ก,ต่�อเมู�"อ“1. Relation น$นๆ ต่�องอย��ในร�ปแบบ 1 NF

2 Attribute ทำ1กต่วทำ!"ไมู�ได�เป0นคื!ย�หลัก จะต่�องมู!คืวามูสมูพื่นธิ�ระหว�างคื�าข้อง Attribute แบบฟั-งก�ช่นกบคื!ย�หลัก(Fully Functional Dependency ) หร�อกลั�าวง�ายๆ ว�า ไมู�มู! Partial Dependency เก�ดข้2$น”

Relation SUPPLIER แลัะ ORDER อย��ใน 2 NF แลั�ว แต่�หากสมูมูต่�ให� Relation ORDER1 ถ�กออกแบบเป0นดงน!$

S# P# PNAME

QTY

S1 P1 PAPER 200S1 P2 RULER 150S1 P3 PENCIL 300S2 P1 PAPER 50S2 P3 PENCIL 100S3 P4 PEN 100

ORDER1

S# P# PNAME QTY

Second Normal Form : 2NF

เพื่ราะฉะน$น Relation ORDER1 ไมู�ได�อย��ในร�ป 2 NF ต่�องทำ�าการแต่ก Relation เพื่�"อลัดคืวามูซ้ำ�$าซ้ำ�อนข้องข้�อมู�ลั ดงน!$ORDER1(S#, P#, QTY)PRODUCT(P#, PNAME)

Second Normal Form : 2NF

Step 1: Write Each Key Component on a Separate Line

Write each key component on separate line, then write original (composite) key on

last line Each component will become key in new

table

Second Normal Form

Step 2: Assign Corresponding Dependent Attributes

Determine those attributes that are dependent on other attributes

At this point, most anomalies have been eliminated

Second Normal Form

A table is in third normal form(3NF) if:

It is in 2NF It contains no transitive

dependencies transitive dependency; that is,

one or more attribute may be functionally dependent on nonkey attributes

Third Normal Form : 3NFRelation หน2"งๆ จะอย��ในร�ปแบบ 3NF ก,ต่�อเมู�"อ“1. Relation น$นๆ ต่�องอย��ในร�ปแบบ 2NF

2. Attribute ทำ1กต่วทำ!"ไมู�ได�เป0นคื!ย�หลัก ไมู�มู!คื1ณสมูบต่�ในการก�าหนดคื�าข้อง Attribute อ�"นทำ!"ไมู�ใช่�คื!ย�หลัก หร�อกลั�าวง�ายๆ ว�า ไมู�มู! Transitive Dependency เก�ดข้2$น”

Third normal form(3NF)Data anomalies created are easily

eliminated by completing three stepsStep 1: Identify Each New Determinant For every transitive dependency, write

its determinant as PK for new table Determinant

Any attribute whose value determines other values within a row

Third normal form(3NF)Step 2: Identify the Dependent

Attributes Identify attributes dependent on

each determinant identified in Step 1 and identify dependency

Name table to reflect its contents and function

S# SNAME

CITY RATING

S1 Henry

London

1

S2 Vivian

Paris 2

S3 Harry Paris 2S4 Jame

sLondo

n1

S5 Pete Athens

3

SUPPLIER1Third normal form(3NF)

SUPPLIER1 อย��ใน 2 NF แลั�วแต่�ยงมู! Transitive Dependency เก�ดข้2$นอย�� ทำ�าให�มู!คืวามูผิ�ดพื่ลัาดบางอย�างทำ!"อาจเก�ดข้2$นกบข้�อมู�ลัใน Relation ก,ยงมู!อย�� เช่�น

1. Update Anomaly หากมู!การแก�ไข้การจดอนดบข้องจงหวดข้องผิ��ผิลั�ต่ จะต่�องทำ�าการแก�ไข้ข้�อมู�ลัหลัาย Tuple

Third normal form(3NF)

2. Delete Anomaly หากมู!การลับข้�อมู�ลัการจดอนดบข้องจงหวดข้องผิ��ผิลั�ต่ จะทำ�าให�ข้�อมู�ลัการจดการจดอนดบหายไปจากฐานข้�อมู�ลั

Third normal form(3NF)

ป-ญหาทำ!"เก�ดข้2$น สามูารถแก�ไข้ได�โดยแต่ก Relation (Decomposition)

SUPPLIER1 เป0น 2 Relation โดยแยก Attribute ทำ!"ถ�กก�าหนดคื�ากบ

Attribute ทำ!"เป0นต่วก�าหนดคื�า(Determinant ) ออกเป0น

Relation ใหมู�แลัะ ก�าหนดให� Attribute ทำ!"

เป0นDeterminant เป0นคื!ย�หลักข้อง Relation ใหมู�

Third normal form(3NF)

จะได�SUPPLIER(S#, SNAME, CITY)CITY(CITY, RATING)

Third normal form(3NF)

ในบางคืร$งผิ��ออกแบบพื่ยายามูแต่ก Relation ทำ!"มู! attribute มูากๆ ออกเป0นหลัายๆ Relation ซ้ำ2"งจะทำ�าให�เก�ดป-ญหาการแต่ก Relation ทำ!"ไมู�

เหมูาะสมูได�(Bad Decomposition) เช่�น

หากแต่ก Relation SUPPLIER1 เป0นดงน!$SUPPLIER(S#, SNAME,

CITY)SUPPLIER2(S#, RATING)

S# SNAME

CITY

S1 Henry

London

S2 Vivian

Paris

S3 Harry

Paris

S4 James

London

S5 Pete Athens

Normal FormSUPPLIER

Normal FormS# RATI

NGS1 1S2 2S3 2S4 1S5 3

SUPPLIER2

จากต่วอย�าง การแต่ก Relation น!$ด�เหมู�อนจะแก�ป-ญหาคืวามูผิ�ดพื่ลัาดทำ!"

อาจจะเก�ดจากการเพื่�"มู ลับ ปรบปร1งข้�อมู�ลัได� เพื่ราะได�แต่กออกเป0น 2

Relation แลัะอย��ในร�ป 3NF แลั�ว แต่�การแต่ก Relation อาจก�อให�เก�ดคืวามู

ผิ�ดพื่ลัาดข้2$นได�อ!กเช่�นกน จะเห,นว�า การเพื่�"มูช่�"อจงหวดแลัะ

การจดอนดบข้องจงหวดใหมู�จะทำ�าไมู�ได� จนกว�าจะมู!ผิ��ผิลั�ต่รายใดอย��ทำ!"จงหวดทำ!"จะ

เพื่�"มูช่�"อแลัะจดอนดบน$น

Third normal form(3NF)

Relation SUPPLIER แลัะ SUPPLIER2 เป0น Relation ทำ!"ไมู�เป0นอ�สระต่�อกน เพื่ราะจงหวดเป0นต่วก�าหนดการจดอนดบได�S# CITY

RATING

S# CITY

RATING

Third normal form(3NF)

The Boyce-Codd Normal Form (BCNF) Every determinant in table is a candidate

key Has same characteristics as primary key, but for some reason, not chosen to

be primary key When table contains only one candidate key, the 3NF and the BCNF are equivalent BCNF can be violated only when table

contains more than one candidate key

The Boyce-Codd Normal Form (BCNF) Table is in 3NF when it is in 2NF and

there are no transitive dependencies Table can be in 3NF and fails to meet

BCNF No partial dependencies, nor does

it contain transitive dependencies A nonkey attribute is the determinant of a key attribute

Boyce/Codd Normal Form : BCNFRelation หน2"งๆ จะอย��ในร�ปแบบ BCNF ก,ต่�อเมู�"อ“1. Relation น$นๆ ต่�องอย��ในร�ปแบบ 3NF

2. ไมู�มู! Attribute อ�"นใน Relation ทำ!"สามูารถระบ1คื�าข้อง Attribute ทำ!"เป0นคื!ย�หลักหร�อส�วนหน2"งส�วนใดข้องคื!ย�หลักในกรณ!ทำ!"คื!ย�หลักเป0นคื!ย�ผิสมู(Composite Key)”

BCNF จะอย��ในร�ปแบบ 3 NF แต่�ไมู�จ�าเป0นเสมูอไปว�าร�ปแบบ 3 NF จะอย��

ในร�ปแบบข้อง BCNF เน�"องจากร�ปแบบน!$เป0นร�ปแบบทำ!"ข้ยาย

ข้อบเข้ต่ข้องร�ปแบบ 3 NF ให�เหมูาะสมูย�"งข้2$น

โดยร�ปแบบข้อง Relation ทำ!"จะต่�องผิ�านการทำ�าให�เป0น BCNF มูกจะมู!

คื1ณสมูบต่� ดงน!$

The Boyce-Codd Normal Form (BCNF)

1 .เป0น Relation ทำ!"มู! CK หลัายคื!ย�(Multiple Candidate Key)

2. CK เป0นคื!ย�ผิสมู(Composite Key)3. CK น$นมู!บางส�วนซ้ำ�$าซ้ำ�อน

กน(Overlapped)

The Boyce-Codd Normal Form (BCNF)

จะใช่� Relation SUPPLIER3 โดยสมูมูต่�ว�า SNAME เป0นคื�าไมู�ซ้ำ�$ากนแลัะมู!คื1ณสมูบต่�เป0นคื!ย�หลักได�เช่�นกน

The Boyce-Codd Normal Form (BCNF)

S# SNAME P# QTY

S1 Henry P1 300

S1 Henry P2 200

S1 Henry P3 400

S1 Henry P4 200

S1 Henry P5 100

S1 Henry P6 100

S2 Vivian P1 300

S2 Vivian P2 400

S3 Harry P2 200

S4 James P2 200

S4 James P4 300

S4 James P5 400

SUPPLIER3

จากต่วอย�างจะเป0น Relation ทำ!"มู!ป-ญหาเพื่ราะมู! CK เป0นคื!ย�ผิสมูแลัะมู!คืวามูซ้ำ�$าซ้ำ�อนกน CK ทำ!"มู!คื1ณสมูบต่�เป0นคื!ย�หลักข้องร!เลัช่น อาจเป0น S# แลัะ P# หร�อ SNAME แลัะ P#

Relation น!$ไมู�ได�อย��ในร�ปแบบ BCNF เพื่ราะเมู�"อเลั�อก CK ใดเป0นคื!ย�หลักแลั�ว CK ทำ!"ไมู�ถ�กเลั�อกจะยงปรากฎซ้ำ�$าซ้ำ�อนอย��ใน Relation น!$แลัะมู!คื1ณสมูบต่�ในการระบ1คื�าข้อง Attribute ทำ!"เป0นคื!ย�หลัก

The Boyce-Codd Normal Form (BCNF)

เช่�น หากเลั�อก S# แลัะ P# เป0นคื!ย�หลักแลั�วจะเก�ดป-ญหาใน Relation น!$คื�อ SNAME จะมู!คื1ณสมูบต่�ในการระบ1คื�าข้อง Attribute S# ดงน!$

S# SNAME P# QTY

The Boyce-Codd Normal Form (BCNF)

ดงน$น Relation SUPPLIER3 ต่�องทำ�าการแต่กร!เลัช่นออก โดยแยก attribute ทำ!"สามูารถระบ1คื�าข้อง PK แยกเป0นอ!กร!เลัช่นหน2"ง ซ้ำ2"งทำ�าได� 2 กรณ!คื�อ

1 . SUPPLIER4(S#, SNAME) ORDER2(S#, P#, QTY)

หร�อ 2. SUPPLIER4(S#, SNAME) ORDER2(SNAME, P#, QTY)

The Boyce-Codd Normal Form (BCNF)

A

B

C

D

E

The Boyce-Codd Normal Form (BCNF)

Fourth Normal Form (4NF) Table is in fourth normal form (4NF) when

both of the following are true: It is in 3NF Has no multiple sets of multivalued

dependencies 4NF is largely academic if tables conform

to following two rules: All attributes must be dependent on primary

key, but independent of each other No row contains two or more multivalued

facts about an entity

Fourth Normal Form : 4NFRelation หน2"งๆ จะอย��ในร�ปแบบ 4NF ก,ต่�อเมู�"อ“1. Relation น$นๆ ต่�องอย��ในร�ปแบบ BCNF

2. เป0น Relation ทำ!"ไมู�มู!คืวามูสมูพื่นธิ�ในการระบ1คื�าข้อง Attribute แบบหลัายคื�าโดยทำ!" Attribute ทำ!"ถ�กระบ1คื�าเหลั�าน!$ไมู�มู!คืวามูสมูพื่นธิ�กน(Independently Multivalued Dependency) ”

Fourth Normal Form (4NF)

เช่�น ผิ��ผิลั�ต่หน2"งผิลั�ต่ได�หลัายโคืรงการแลัะผิ��ผิลั�ต่หน2"งๆ มู!โรงงานต่$ง

อย��หลัายจงหวดทำ�าให�อย��ในร�ป BCNF โดยให�ทำ1ก

Attribute ประกอบกนเป0นคื!ย�หลัก

Fourth Normal Form (4NF)

S# PJ# CITY

S1 PJ1 Khonkaen

S1 PJ1 Bangkok

S1 PJ2 Khonkaen

S1 PJ2 Bangkok

S2 PJ3 Nontaburi

S2 PJ3 Chiangmai

S2 PJ4 Nontaburi

S2 PJ4 Chiangmai

SPJC

จะได�S# ->-> PJ#S# ->-> CITY

การทำ!" attribute ทำ$งสองไมู�มู!คืวามูสมูพื่นธิ�กนแต่�มูาอย��ใน Relation เด!ยวกนจะก�อให�เก�ดคืวามูซ้ำ�$าซ้ำ�อนข้องข้�อมู�ลั ถ2งแมู�ว�า Relation SPJC จะอย��ในร�ป BCNF โดยมู!ทำ1ก Attribute ประกอบเป0นคื!ย�หลักก,ต่ามูแต่�ยงมู!ป-ญหาอย��

Fourth Normal Form (4NF)

ดงน$น Relation SPJC ต่�องทำ�าการแต่กร!เลัช่นออกเป0น 2Relation ดงน!$

SPJ(S#, PJ#) SC(S#, CITY)

Fourth Normal Form (4NF)

S# PJ#

S1 PJ1

S1 PJ2

S2 PJ3

S2 PJ4

SPJ

S# CITY

S1 Khonkaen

S1 Bangkok

S2 Nontaburi

S2 Chiangmai

SC

Fifth Normal Form : 5NFFifth Normal Form : 5NF

Relation หน2"งๆ จะอย��ในร�ปแบบ 5NF ก,ต่�อเมู�"อ“1. Relation น$นๆ ต่�องอย��ในร�ปแบบ 4NF

2. ไมู�มู! Symmetric Constraint กลั�าวคื�อ หากมู!การแต่กRelation ออกเป0น Relation ย�อย(Projection) แลัะเมู�"อทำ�าการเช่�"อมูโยง Relation ย�อยทำ$งหมูด(Join ) จะไมู�ก�อให�เก�ดข้�อมู�ลัใหมู�ทำ!"ไมู�เหมู�อน Relation เด�มู(Spurious Tuple)”

เช่�น Relation SPP ประกอบด�วย SNAME, PNAME, PJNAME โดยมู! Attribute ทำ$ง 3 เป0นคื!ย�หลักSNA

MEPNAME

PJNAME

Henry

PEN PJ2

Henry

TABLE

PJ1

James

PEN PJ1

Henry

PEN PJ1

Fifth Normal Form : 5NF

Normal FormRelation SPP อย��ใน 4 NF

ด�วยการให� Attribute ทำ$ง 3 ประกอบกนเป0นคื!ย�หลัก แต่�ยงมู!ป-ญหาในการปรบปร1งข้�อมู�ลัต่�างๆ

เพื่�"อหลั!กเลั!"ยงป-ญหา จ2งทำ�าการแต่ก Relation SPP เป0น 3Relation ย�อย(Projection) โดยแต่�ลัะ Relation ย�อยจะประกอบด�วย Attribute ต่�อลัะคื��เป0นคื!ย�หลัก

Normal Form

SNAME

PNAME

Henry

PEN

Henry

TABLE

James

PEN

PNAME

PJNAME

PEN PJ2TABL

EPJ1

PEN PJ1

PJNAME

SNAME

PJ2 Henry

PJ1 Henry

PJ1 James

SP PPJ PJS

ดงน$น หากมู!การแต่กร!เลัช่"นออกมูาแลั�วทำ�าการเช่�"อมูโยงร!เลัช่"นย�อยน$นใหมู� หากไมู�มู!ข้�อมู�ลัทำ!"แต่กต่�างไปจากร!

เลัช่"นเด�มู ก,สามูารถจะแต่กร!เลัช่"นน$นได� แต่�ถ�าหากแต่กเป0นร!เลัช่"นย�อยแลั�วเก�ดข้�อมู�ลัไมู�เหมู�อนกบร!เลัช่"นเด�มูก,ไมู�คืวรแต่กร!เลัช่นให�ถ�อว�าร!เลัช่นเด�มูอย��ใน

5 NF แลั�ว

Fifth Normal Form : 5NF

S# CITY PJ#S1 New

YorkPJ1

S1 Paris PJ2S3 Paris PJ1

SCPJ

Fifth Normal Form : 5NF

S# CITYS1 New

YorkS1 ParisS3 Paris

CITY PJ#New York

PJ1

Paris PJ2Paris PJ1

S# PJ#S1 PJ1S1 PJ2S3 PJ1

Fifth Normal Form : 5NF

S# CITY PJ#S1 New

YorkPJ1

S1 Paris PJ2S1 Paris PJ1S3 Paris PJ1

Spurious Tuple

Fifth Normal Form : 5NF

แบบฝึ�กหั�ดทำ�ายบทำ1. When is a table in 1NF?2. When is a table in 2NF? 3. When is a table in 3NF? 4. When is a table in BCNF?5. Define and discuss the concept of transitive dependency.

แหัล่!งอ�างอ"งข�อมู�ล่Database Systems:

Design, Implementation, and Management, Seventh Edition,

Rob and Colonel

top related