ระบบฐำนข้อมูล (database system)mit.wu.ac.th/mit/images/editor/files/dbs...

108
Connolly, Thomas and Begg, Carolyn. 2010. Database Systems: A Practical Approach to Design, Implementation, and Management. 5th Ed. Pearson Education. ดร. สลิล บุญพรหมณ์ สำนักวิชำสำรสนเทศศำสตร์ Normalization ITM-661 ระบบฐำนข ้อมูล (Database system)

Upload: others

Post on 23-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

Connolly, Thomas and Begg, Carolyn. 2010. Database Systems: A

Practical Approach to Design, Implementation, and Management.

5th Ed. Pearson Education. ดร. สลล บญพรหมณ ส ำนกวชำสำรสนเทศศำสตร

Normalization

ITM-661 ระบบฐำนขอมล (Database system)

Page 2: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

2

"...คนเรำถำพอในควำมตองกำรกมควำมโลภนอย เมอมควำมโลภ

นอยกเบยดเบยนคนอนนอย ถำทกประเทศมควำมคด คด-อนน

ไมใชเศรษฐกจ มควำมคดวำท ำอะไรตองพอเพยง หมำยควำม

วำ พอประมำณ ไมสดโตง ไมโลภอยำงมำก คนเรำกอยเปนสข

..."

พระราชด ารสในพระบาทสมเดจพระเจาอยหว

พระราชทานเนองในโอกาสวนเฉลมพระชนมพรรษา

ณ ศาลาดสดาลย สวนจตรลดา พระราชวงดสต

๔ ธนวาคม ๒๕๔๐

Page 3: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

Objectives

The purpose of normalization.

How normalization can be used when designing a relational database.

The potential problems associated with redundant data in base relations.

The concept of functional dependency, which describes the relationship between attributes.

The characteristics of functional dependencies used in normalization.

3

Page 4: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

Objectives

How to identify functional dependencies for a given relation.

How functional dependencies identify the primary key for a relation.

How to undertake the process of normalization.

How normalization uses functional dependencies to group attributes into relations that are in a known normal form.

4

Page 5: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

Objectives

How to identify the most commonly used normal forms, namely First Normal Form (1NF), Second Normal Form (2NF), and Third Normal Form (3NF).

The problems associated with relations that break the rules of 1NF, 2NF, or 3NF.

How to represent attributes shown on a form as 3NF relations using normalization.

5

Page 6: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

Purpose of Normalization

Normalization is a technique for producing a set of suitable relations that support the data requirements of an enterprise.

Characteristics of a suitable set of relations include:

The minimal number of attributes necessary to support the data requirements of the enterprise;

attributes with a close logical relationship are found in the same relation;

minimal redundancy with each attribute represented only once with the important exception of attributes that form all or part of foreign keys.

6

Page 7: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

7

The benefits of using a database that has a suitable set of relations is that the database will be:

easier for the user to access and maintain the data;

take up minimal storage space on the computer.

Page 8: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

How Normalization Supports Database Design

8

Page 9: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

9

Major aim of relational database design is to group attributes

into relations to minimize data redundancy.

Potential benefits for implemented database include:

Updates to the data stored in the database are achieved

with a minimal number of operations thus reducing the

opportunities for data inconsistencies.

Reduction in the file storage space required by the base

relations thus minimizing costs.

Problems associated with data redundancy are illustrated by

comparing the Staff and Branch relations with the StaffBranch

relation

Data Redundancy and Update Anomalies

Page 10: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

10

StaffBranch relation has redundant data; the details of a branch are repeated for every member of staff.

Page 11: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

11

In contrast, the branch information appears only once for each branch in the Branch relation and only the branch number (branchNo) is repeated in the Staff relation, to represent where each member of staff is located.

Page 12: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

12

Relations that contain redundant information may potentially suffer from update anomalies.

Types of update anomalies include

Insertion

Deletion

Modification

Page 13: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

13

Functional Dependencies (FD)

Important concept associated with normalization.

Functional dependency describes relationship between attributes.

For example, if A and B are attributes of relation R, B is functionally dependent on A

(denoted A B),

if each value of A in R is associated with exactly one value of B in R.

Page 14: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

14

คณสมบตของ FD

Property of the meaning or semantics of the attributes in a relation.

Diagrammatic representation.

The determinant of a functional dependency refers to the attribute or group of attributes on the left-hand side of the arrow.

Page 15: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

ตวอยาง

15

Page 16: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

16

Consider the values shown in staffNo and sName attributes of

the Staff relation

Based on sample data, the following functional dependencies

appear to hold.

staffNo → sName

sName → staffNo

Example FD that holds for all time

Page 17: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

17

However, the only functional dependency that remains true for all possible values for the staffNo and sName attributes of the Staff relation is:

staffNo → sName

Page 18: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

18

ตวอยาง

กำรเปนทปรกษำ (รหส, ชอ, นำมสกล, GPAX, หลกสตร, รหสอำจำรย, ชออำจำรย, หองท ำงำน, โทรศพท)

Page 19: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

19

การเขยน FD

SSN ENAME

PNUMBER PNAME, PLOCATION

SSN + PNUMBER HOURS

SSN PNUMBER HOURS ENAME PNAME PLOCATION

EMP_PROJ (SSN, PNUMBER, HOURS, ENAME, PNAME, PLOCATION)

แบบท 1

แบบท 2

Page 20: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

20

Determinants should have the minimal number of attributes necessary to maintain the functional dependency with the attribute(s) on the right hand-side.

This requirement is called full functional dependency.

Page 21: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

21

Full functional dependency indicates that

if A and B are attributes of a relation,

B is fully functionally dependent on A, if B is functionally dependent on A, but not on any proper subset of A.

Page 22: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

22

หรอ

A functional dependency x y is partial dependency if

there is some attribute A X that can be removed from X and dependency will still hold.

That is A, A X , (X - {A}) Y

Page 23: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

23

ตวอยาง Full FD

Exists in the Staff relation

staffNo, sName → branchNo

True - each value of (staffNo, sName) is associated with a single value of branchNo.

However, branchNo is also functionally dependent on a subset of (staffNo, sName), namely staffNo.

Example above is a partial dependency.

Page 24: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

24

Main characteristics of FD used in normalization:

There is a one-to-one relationship between the attribute(s) on the left-hand side (determinant) and those on the right-hand side of a functional dependency.

Holds for all time.

The determinant has the minimal number of attributes necessary to maintain the dependency with the attribute(s) on the right hand-side.

Page 25: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

25

ค ำแนะน ำ

ในตำรำงทม attribute จ ำนวนมำก กำรหำ FD จะยำกขน รวมทงกำรเขยน FD จะดยำกจงใหใชกำรเขยนแบบท 2 เพรำะพจำรณำงำยกวำ ไมสบสน

Page 26: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

26

Identifying Functional Dependencies

Identifying all FD between a set of attributes is relatively simple if the meaning of each attribute and the relationships between the attributes are well understood.

This information should be provided by the enterprise in the form of discussions with users and/or documentation such as the users’ requirements specification.

Page 27: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

27

Identifying Functional Dependencies (ตอ)

if

the users are unavailable for consultation and/or

the documentation is incomplete

then

depending on the database application it may be necessary for the database designer to use their common sense and/or experience to provide the missing information.

Page 28: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

28

สรปการเขยน FD

ขอมลจำกเอกสำรทรวบรวมมำเพอออกแบบฐำนขอมลวำประกอบไปดวยขอมลอะไร และขอมลสมพนธกนหรอไม อยำงไร

ผใช(user)บอกใหทรำบ

ประสบกำรณหรอวจำรณญำณของผออกแบบ

สำมญส ำนกของผออกแบบ บำงครงเรำสำมำรถระบไดเพรำะเปนควำมสมพนธท “ใครๆกร” เชน หมำยเลขโทรศพททใช

ตดตอลกคำยอมซ ำได

Page 29: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

29

ตวอยาง

Examine semantics of attributes in StaffBranch relation.

Assume that position held and branch determine a member of staff’s salary.

Page 30: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

30

FD ของ StaffBranch relation คอ

staffNo → sName, position, salary, branchNo, bAddress

branchNo → bAddress

bAddress → branchNo

branchNo, position → salary

bAddress, position → salary

Page 31: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

31

Identifying the PK for a Relation using FD

Main purpose of identifying a set of FD for a relation is to specify the set of integrity constraints that must hold on a relation.

An important integrity constraint to consider first is the identification of candidate keys, one of which is selected to be the primary key for the relation.

Page 32: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

32

StaffBranch relation has 5 FD

staffNo → sName, position, salary, branchNo, bAddress

branchNo → bAddress

bAddress → branchNo

branchNo, position → salary

bAddress, position → salary

The determinants are staffNo, branchNo, bAddress, (branchNo, position), and (bAddress, position).

Page 33: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

33

To identify all candidate key(s), identify the attribute (or group of attributes) that uniquely identifies each tuple in this relation.

All attributes that are not part of a candidate key should be functionally dependent on the key.

The only candidate key and therefore primary key for StaffBranch relation, is staffNo, as all other attributes of the relation are functionally dependent on staffNo.

Page 34: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

34

ค าแนะน า

FD ใชในกำรท ำ normalization ตงแตระดบท 2 เปนตนไป และในกำรท ำ normalization แตละระดบจะมประเดนทตองตรวจควำมเกยวของของ attribute ตำงกน

ดงนนจงไมควรรบเขยน FD ในทกประเดนเพรำะจ ำนวน attribute ยงมจ ำนวนมำกอย ใหแสดงเฉพำะประเดนทใชในกำรท ำ normalization นนๆ

Page 35: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

35

ค าแนะน า

ตรวจสอบจ ำนวน attribute ใน FD วำตองไมตกหลนจำกตำรำงทตรวจสอบอย ยงตรวจสอบเรำจะไดตำรำงเพมขน และขนำดตำรำงจะเลกลง

ในกำรเขยน FD ไมควรเขยน non-key attribute ซ ำกบ FD อนโดยไมเกดประโยชน เพรำะจะท ำใหสบสนโดยไมจ ำเปน

Page 36: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

36

3. Normalization

Normalization is a technique for producing a set of suitable

relations that support the data requirements of an

enterprise.

As normalization proceeds, the relations become

progressively more restricted (stronger) in format and also

less vulnerable to update anomalies.

Page 37: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

37

The Process of Normalization

Formal technique for analyzing a relation based on its primary key and the functional dependencies between the attributes of that relation.

Often executed as a series of steps. Each step corresponds to a specific normal form, which has known properties.

Page 38: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

38

How Normalization Supports DB Design

Page 39: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

39

The Process of Normalization

Page 40: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

40

Page 41: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

41

Unnormalized Form (UNF)

A table that contains one or more repeating groups.

To create an unnormalized table

Transform the data from the information source (e.g. form) into table format with columns and rows.

Page 42: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

42

Page 43: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

43

4. First Normal Form (1NF)

A relation is said to be in the first normal form if it contains no repeating group.

A table in which the intersection of every column and record contains only one value.

Page 44: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

44

ถำตำรำงนนไมมกำรเกบขอมลมำกกวำ 1 ชด (repeating group) หรอขอมลทเกบมคำเพยงคำเดยว (single value) หรอ

ถำในทกชอง (intersection คอจดทแถวกบคอลมนตดกน อำจเรยกวำ cell หรอชอง) เกบขอมลเพยงคำเดยว

Page 45: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

45

Page 46: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

46

UNF to 1NF

Nominate an attribute or group of attributes to act as the key for the unnormalized table.

Identify the repeating group(s) in the unnormalized table which repeats for the key attribute(s).

Page 47: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

47

การแปลง UNF เปน 1NF

Remove the repeating group by

Entering appropriate data into the empty columns of rows containing the repeating data (‘flattening’ the table).

Or by

Placing the repeating data along with a copy of the original key attribute(s) into a separate relation.

Page 48: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

48

ตวอยาง

ลองพจารณาตารางนวามคณสมบต First normal form หรอไม

Page 49: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

49

แยกคาทซ าออกเปนแถวหรอ tuple ใหมซงผลทไดคอ

Page 50: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

50

จำกกำรแยกแถวใหมนนจะท ำใหในบำงเซลยงไมมขอมล จงตองเตมขอมลใหครบถวน กำรท ำแบบนเรยกวำกำรท ำ table flattening

Page 51: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

51

แตจะพบวำ Primary key เดมนนไมสำมำรถใชไดอกตอไป เพรำะไมมคณสมบต Uniqueness หรอคำทไมซ ำ จงตองพจำรณำ Key ใหม ในทนจะใช SID มำเปน key

Page 52: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

52

จงไดตารางสดทายทมคณสมบตของ 1NF ทเขยนไดเปน

Page 53: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

53

ตวอยำง จงตรวจสอบและแกไขตำรำง Enrolment ใหมคณสมบต 1NF

Page 54: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

54

ตาราง Enrollment นไมมคณสมบตของ 1NF

Page 55: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

55

กำรแกไขเพอท ำให relation นมคณสมบต 1NF ท ำไดโดยกำรแยกคำทมมำกกวำ 1 คำนนออกมำเปนแตละ tuple หรอแยกออกมำเปนคำในแถวยอยๆ และตองเตมคำในแถวใหครบ

Page 56: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

56

เมอแกไขแลวจะเหนวำ Course_ID ซงเปน Key เดมเนองจำกมคณสมบตทใหคำไมซ ำ (Uniqueness) จะใชไมไดอกตอไป จงตองพจำรณำ Primary key ทเหมำะสมใหมดวยในกรณนจงเพม Course_ID เปน key อก 1 attribute โดยจะได relation ทมลกษณะดงน

Page 57: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

57

ตวอยาง จงตรวจสอบและแกไขตำรำง Project ใหมคณสมบต 1NF

Page 58: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

58

พบวำ pno ซงเดมใชเปน primary key ไมสำมำรถใชเปน key ได อกตอไปเพรำะมหลำยบรรทดทใชคำของ pno เหมอนกน

Page 59: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

59

Page 60: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

60

การแกไขตารางประกอบดวยขนตอนดงน

1) แยกคำทใสมำกกวำ 1 คำออกมำใสในบรรทดใหม โดยจดให ใสเซลละ 1 คำเทำนน

2) เตมคำในสวนทเกยวของในเตมบรรทด ส ำหรบในกรณน เมอเพมบรรทดใหมใหใสคำของ Emp_num และ Emp_name ลงไปในบรรทดใหมดวย

3) ก ำหนด primary key ในตำรำงทแกไขแลว

Page 61: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

61

สรป

ขนตอนกำรแกไขใหตำรำงมคณสมบต 1NF มดงน

1) ตรวจสอบวำ repeating group หรอไม หำกไมมถอวำเปน 1NF โดยใช key เดมได แตถำม repeating group ให ด ำเนนกำรตอไป

2) ท ำให attribute ทเกบคำมำกกวำ 1 คำเกบไดเพยงคำเดยวโดยเกบแยกบรรทดละ 1 คำ ในกำรท ำเชนนจะตองเตมคำเดมลงในบรรทดใหมใหครบถวนดวย กำรท ำเชนนมวำ table flattening

3) ก ำหนด primary key ของตำรำงใหม

Page 62: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

62

Page 63: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

63

5. Second Normal Form (2NF)

Based on the concept of full functional dependency.

Full functional dependency indicates that if

A and B are attributes of a relation,

B is fully dependent on A if B is functionally dependent on A but not on any proper subset of A.

Page 64: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

64

A relation is in 2NF if it has been placed in 1NF, and if every data item in the entity is fully functionally dependent on the primary key.

A functional dependency x y is partial dependency if

there is some attribute A X that can be removed from X and dependency will still hold.

That is for some A X , (X - {A}) Y

Page 65: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

65

ตวอยาง

ถา X = {รหสวชา, รหสผสอน} และ

Y = {หนวยกต, ชอวชา}

X Y

รหสวชา, รหสผสอน หนวยกต, ชอวชา

รหสวชา หนวยกต, ชอวชา

ดงนน สรปวา

รหสวชา, รหสผสอน หนวยกต, ชอวชา

มความสมพนธแบบ partial dependency

Page 66: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

66

การแปลง 1NF ใหเปน 2NF

1) Identify the PK for the 1NF relation.

2) Identify the FD in the relation.

3) If partial dependencies exist on the primary key remove them by placing them in a new relation along with a copy of their determinant.

4) สรปตำรำงทมคณสมบต 2NF

Page 67: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

จงพจารณาตารางตอไปน

67

Business rules: 1) แตละวชำมผสอนไดเพยงคนเดยว 2) ผสอนแตละคนสอนไดมำกกวำ 1 วชำ 3) รหสวชำจะซ ำกนไมได แตชอวชำอำจซ ำกนได

Page 68: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

68

Page 69: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

69

Page 70: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

70

ขนตอนการท าใหเปน 2NF ประกอบดวย

ข นตอนท 1

เปนกำรขจดกลมของขอมลทซ ำ ซงจะไดเปนตำรำงท

ประกอบดวย Attribute ทมคำๆเดยวในทกcellของตำรำง (ท ำ

ใหเปน 1NF) และระบ primary key

Page 71: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

71

ข นตอนท 2

เปนกำรตรวจสอบควำมสมพนธระหวำง Key attribute ซงอำจมำจำก attribute มำกกวำ 1 attribute (หรอทเรยกวำ Compound key) กบ attribute ทเหลอในตำรำงนนวำตองเปนกำรขนอยแกกนแบบครบถวน (Fully dependency) ไมใชแบบกำรขนแกกนเพยงบำงสวน (Partially dependency)

เขยน FD1

Page 72: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

72

ข นตอนท 3

ส ำหรบ FD ทแสดงใหเหนวำเกด partial participation ใหแปลงเปนตำรำง

ใหม

FD ของ primary key แรกนนจะตองเหลอ non-key attribute เฉพำะท

เปน full-functionally dependence เทำนน

Page 73: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

73

ขนตอนท 4 สรปตำรำง

Page 74: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

74

ตวอยาง

ตำรำง assignment วำมคณสมบต 2 NF หรอไม หำกไมม จงแกไข

Page 75: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

75

Business rules

1) พนกงำนแตละคนสำมำรถท ำงำนในโครงกำรไดมำกกวำ 1 โครงกำร

2) ระยะเวลำทพนกงำนแตละคนท ำงำนในโครงกำรไมเทำนน แลวแตปรมำณงำน

3) แตละโครงกำรเปนงำนของลกคำบรษทเดยว และมพนกงำนทท ำงำนให โครงกำรนนมำกกวำ 1 คน

4) ลกคำแตละบรษทอำจมโครงกำรทจำงใหด ำเนนกำรมำกกวำ 1 โครงกำร

5) ชอของโครงกำรอำจซ ำกนได

Page 76: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

76

การพจารณาประกอบดวยขนตอนดงน

1. ตรวจสอบคณสมบต 1NF

ตำรำงมคณสมบต 1NF และโครงสรำงดงน

Page 77: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

77

2. เขยน FD หลก (FD1)

assignment (empid, empname, pno, pname, pcompany, day)

Page 78: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

78

3. เขยน FD รอง

เนองจำกกำรท ำ second normal form เปนกำรตรวจสอบกำรขนอย

กบคย ทตองเปนกำรขนกบคยอยำงสมบรณ (Fully functionally

dependent) ไมใชกำรขนกบสวนใดสวนหนงของคย (Partially

dependent) ดงนนจงตองหำวำสวนใดสวนหนงของคยนน

สำมำรถบอกอะไรไดบำง

Page 79: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

79

การเขยน FD รองของตาราง Assignment

assignment (empid, empname, pno, pname, pcompany, day)

Page 80: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

80

4. สรปตารางทมคณสมบต 2NF

โดยพจำรณำจำก FD ทงหมด

Page 81: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

81

Page 82: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

82

ขอสงเกต

กำรตรวจสอบ 2NF เปนกำรตรวจเพอพจำรณำวำ primary key ทเลอกมำนนใหญเกนจ ำเปนหรอไม โดยกำรตงค ำถำมวำจ ำเปนตองใช attribute มำกๆมำเปน key หรอเปลำ ม attribute ใดทเมอน ำออกไปแลว key ยงคงควำมเปน key

หำก Attribute ทเลอกเปน key นนมเพยง 1 attribute ซงไมสำมำรถลดขนำดของ key ไดแลว กรณเชนนจงมคณสมบต 2NF อยแลว

Page 83: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

83

ตวอยาง

Business rules 1) ชอนกศกษำไมซ ำกน 2) รหสวชำไมซ ำกน แตชอวชำอำจซ ำกนได 3) นกศกษำแตละคนลงทะเบยนเรยนไดหลำยวชำ 4) แตละวชำอำจมนกศกษำเรยนมำกกวำ 1 คน 5) แตละวชำมผสอนรำยเดยว 6) ชออำจำรยไมซ ำกน 7) อำจำรยแตละคนสอนไดมำกกวำ 1 วชำ

Page 84: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

84

กำรพจำรณำประกอบดวยขนตอนดงน

1. ท ำใหตำรำง Enrolment มคณสมบต 1NF

2. เขยน FD1

ในกรณนคยคอซงจดไดวำเปน compound key หรอ concatenated key คอ (SName, CourseID)

3. เขยน FD รอง

จงตองพจำรณำในลกษณะทกชดในคยทเปนไปไดในทนมคยเปนไปได 3 ชดคอ

SName,

CourseID,

(Sname, CourseID)

4. สรปตำรำงทมคณสมบต 2NF

Page 85: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

85

Business rules 1) นกศกษำแตละคนมอำจำรยทปรกษำไดเพยงคนเดยว 2) นกศกษำสมครท ำงำนไดไมเกน 3 บรษท 3) แตละบรษทมเจำหนำทตดตอเพยงรำยเดยว เปนไปไดทช อจนท.อำจเหมอนกน 4) แตละบรษทอำจเปดรบสมครมำกกวำ 1 ต ำแหนง 5) จ ำนวนรบในแตละต ำแหนงทบรษทเปดรบไมจ ำเปนตองเทำกน 6) อำจำรยทปรกษำแตละคนมโทรศพทหมำยเลขเดยว และก ำหนดหมำยเลขให

อำจำรยแตละคนไมซ ำกน 7) อำจำรยแตละคนสำมำรถมนกศกษำทตองดแลไดมำกกวำ 1 คน

Page 86: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

86

กำรพจำรณำประกอบดวยขนตอนดงน

1. ท ำใหตำรำงใหมคณสมบต 1NF

2. เขยน FD1

3. เขยน FD รอง

4. สรปตำรำงทมคณสมบต 2NF

Page 87: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

87

6. Third Normal Form (3NF)

Based on the concept of transitive dependency.

Transitive Dependency is a condition where

A, B and C are attributes of a relation such that if A B and B C,

then C is transitively dependent on A through B. (Provided that A is not functionally dependent on B or C).

Page 88: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

88

A relation is in 3NF

iff

it is in 2 NF and no nonkey attribute is “transitively dependent” on the primary key.

An entity is said to be in the 3NF

if it has been placed in 2NF

and

if all fields which are not a part of PK are mutually independent;

that is, there are no transitive dependencies.

Page 89: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

89

Transitive dependency คออะไร

x y is transitive dependency

iff

there is a set of attributes Z that is not a subset of any key of R,

and both X Z and Z Y hold.

KEYS R X

Y

Z

Page 90: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

90

Transitive dependency

Emp_Dept (ENAME, SSN, BDATE, ADDRESS, DNUMBER, DNAME,

DMGRSSN)

Business rules 1) หมำยเลขประกนสงคมของพนกงำนแตละคนไมซ ำกน ถงแมชอพนกงำนอำจ

เหมอนกน 2) พนกงำนแตละคนท ำงำนอยในแผนกใดแผนกหนงเพยงแผนกเดยว 3) แตละแผนกมหวหนำแผนกคนเดยว

Page 91: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

91

กำรแปลง 2NF ใหเปน 3NF

Identify the primary key in the 2NF relation.

Identify FD in the relation.

If transitive dependencies exist on the primary key remove them by placing them in a new relation along with a copy of their dominant.

Page 92: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

92

ขนตอนกำรตรวจสอบและแกไข

1. พจำรณำวำตำรำงคณสมบต 2 NFหรอไม ท ำใหตำรำงใหมคณสมบต 1NF

เขยน FD1

เขยน FD รอง

สรปตำรำงทมคณสมบต 2NF

2. พจำรณำวำตำรำงคณสมบต 3 NFหรอไม พจำรณำตำรำงทตองตรวจสอบ

ระบ FD ทท ำใหเกด Transitive dependency และปรบแก FD ของตำรำงนน

เขยนโครงสรำงตำรำงจำก FD ใหมทงสวนทเกด Transitive dependency และ FD ของตำรำงทแกไข

น ำมำสรปรวมกบตำรำงทงหมด

Page 93: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

93

1. พจำรณำวำตำรำงคณสมบต 2 NF หรอไม ท ำใหตำรำงใหมคณสมบต 1NF

เขยน FD1

เขยน FD รอง

สรปตำรำงทมคณสมบต 2NF

Page 94: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

94

2. พจำรณำวำตำรำงคณสมบต 3 NF หรอไม พจำรณำตำรำงทตองตรวจสอบ

ระบ FD ทท ำใหเกด Transitive dependency และปรบแก FD ของตำรำงนน

เขยนโครงสรำงตำรำงจำก FD ใหมทงสวนทเกด Transitive dependency และ FD ของตำรำงทแกไข

น ำมำสรปรวมกบตำรำงทงหมด

Page 95: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

สรป

95

ตำรำง 1NF

ตำรำง 2NF

ตำรำง 3NF

Page 96: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

96

ตวอยำง จงตรวจสอบตำรำง staffBranch และท ำใหมคณสมบต3NF

Business rules 1) พนกงำนแตละคนจะท ำงำนในสำขำใดสำขำหนงเพยงสำขำเดยว 2) แตละสำขำอำจมพนกงำนประจ ำอยมำกกวำ 1 คน 3) แตละสำขำมหมำยเลขโทรศพทหมำยเลขเดยว 4) ถงแมพนกงำนท ำงำนในต ำแหนงเดยวกน เงนเดอนอำจแตกตำง

กนได

Page 97: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

วธท า

97

1. พจำรณำวำตำรำงคณสมบต 2 NFหรอไม ท ำใหตำรำงใหมคณสมบต 1NF

เขยน FD1

เขยน FD รอง

สรปตำรำงทมคณสมบต 2NF

Page 98: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

98

2. พจำรณำวำตำรำงคณสมบต 3 NF หรอไม พจำรณำตำรำงทตองตรวจสอบ

ระบ FD ทท ำใหเกด Transitive dependency และปรบแก FD ของตำรำงนน

เขยนโครงสรำงตำรำงจำก FD ใหมทงสวนทเกด Transitive dependency และ FD ของตำรำงทแกไข

น ำมำสรปรวมกบตำรำงทงหมด

Page 99: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

99

สรป

ตำรำง 1NF

ตำรำง 2NF

ตำรำง 3NF

Page 100: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

100

ตวอยำง จงตรวจสอบตำรำง Assignmentและท ำใหมคณสมบต3NF

Business rules

1) แตละโครงกำรมพนกงำนรบผดชอบมำกกวำ 1 คน

2) ระยะเวลำทพนกงำนแตละคนท ำงำนในแตละโครงกำรอำจตำงกนตำมปรมำณงำน

3) คำตอบแทนทพนกงำนไดรบขนอยกบควำมรบผดชอบในโครงกำร

4) พนกงำนแตละคนมต ำแหนงหรอบทบำทเดยว แตต ำแหนงหนงๆมพนกงำนได มำกกวำ 1 คน

Page 101: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

วธท ำ

101

1. พจำรณำวำตำรำงคณสมบต 2 NFหรอไม ท ำใหตำรำงใหมคณสมบต 1NF

เขยน FD1

เขยน FD รอง

สรปตำรำงทมคณสมบต 2NF

Page 102: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

102

2. พจำรณำวำตำรำงคณสมบต 3 NF หรอไม พจำรณำตำรำงทตองตรวจสอบ

ระบ FD ทท ำใหเกด Transitive dependency และปรบแก FD ของตำรำงนน

เขยนโครงสรำงตำรำงจำก FD ใหมทงสวนทเกด Transitive dependency และ FD ของตำรำงทแกไข

น ำมำสรปรวมกบตำรำงทงหมด

Page 103: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

103

สรป

ตำรำง 1NF

ตำรำง 2NF

ตำรำง 3NF

Page 104: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

ตวอยำง จงตรวจสอบและท ำใหมคณสมบต3NF

104

Page 105: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

105

Business rules 1) ในกำรซอสนคำของลกคำแตละครงกระท ำโดยพนกงำนขำยเพยงรำยเดยว 2) ในแตละวนสำมำรถมพนกงำนท ำงำนมำกกวำ 1 คน 3) ในวนหนงๆลกคำทซ อสนคำแลวจะไมกลบมำซอซ ำ 4) ลกคำรำยเดมสำมำรถซอสนคำซ ำไดโดยไมจ ำเปนวำปรมำณกำรซอจะตอง

เทำกนทกครงทซ อคำนน 5) ลกคำไมจ ำเปนตองใชบรกำรผขำยรำยเดมเพอกลบมำซอสนคำใหม

Page 106: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

วธท ำ

106

1. พจำรณำวำตำรำงคณสมบต 2 NFหรอไม ท ำใหตำรำงใหมคณสมบต 1NF

เขยน FD1

เขยน FD รอง

สรปตำรำงทมคณสมบต 2NF

Page 107: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

107

2. พจำรณำวำตำรำงคณสมบต 3 NF หรอไม พจำรณำตำรำงทตองตรวจสอบ

ระบ FD ทท ำใหเกด Transitive dependency และปรบแก FD ของตำรำงนน

เขยนโครงสรำงตำรำงจำก FD ใหมทงสวนทเกด Transitive dependency และ FD ของตำรำงทแกไข

น ำมำสรปรวมกบตำรำงทงหมด

Page 108: ระบบฐำนข้อมูล (Database system)mit.wu.ac.th/mit/images/editor/files/DBS 2556 3--Normalization.pdf · The purpose of normalization. How normalization can

108

สรป

ตำรำง 1NF

ตำรำง 2NF

ตำรำง 3NF