database normalization 1nf, 2nf, 3nf, bcnf, 4nf, 5nf

26
Normalization Oum Saokosal Master's Degree in Information Systems, South Korea 012-252-752 / 010-878-992 [email protected] 1

Upload: oum-saokosal

Post on 06-Jan-2017

12.255 views

Category:

Education


9 download

TRANSCRIPT

Page 1: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Normalization

Oum SaokosalMaster's Degree in Information Systems, South

Korea012-252-752 / 010-878-992

[email protected]

1

Page 2: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Normalization The biggest problem needed to be solved in

database is data redundancy. Why data redundancy is the problem? Because it

causes: Insert Anomaly Update Anomaly Delete Anomaly

2

Teacher Subject Teacher Degree

Tel

Sok San Database Master's 012666777Van Sokhen Database Bachelor's 017678678Sok San E-

CommerceMaster's 012666777

Page 3: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Normalization (Cont.) Normalization is the process of removing

redundant data from your tables to improve storage efficiency, data integrity, and scalability.

Normalization generally involves splitting existing tables into multiple ones, which must be re-joined or linked each time a query is issued.

Why normalization? The relation derived from the user view or data store will

most likely be unnormalized. The problem usually happens when an existing system

uses unstructured file, e.g. in MS Excel.

3

Page 4: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Steps of Normalization 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)

In practice, 1NF, 2NF, and 3NF are enough for database.

4

Page 5: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

First Normal Form (1NF)The official qualifications for 1NF are:

1. Each attribute name must be unique.2. Each attribute value must be single.3. Each row must be unique.4. There is no repeating groups.

Additional: Choose a primary key.

Reminder: A primary key is unique, not null, unchanged. A

primary key can be either an attribute or combined attributes.

5

Page 6: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

First Normal Form (1NF) (Cont.) Example of a table not in 1NF :

It violates the 1NF because: Attribute values are not single. Repeating groups exists.

6

Group Topic Student ScoreGroup A Intro MongoDB Sok San 18 marks

Sao Ry 17 marksGroup B Intro MySQL Chan Tina 19 marks

Tith Sophea 16 marks

Page 7: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

First Normal Form (1NF) (Cont.) After eliminating:

Now it is in 1NF. However, it might still violate 2NF and so on.

7

Group

Topic Family Name

Given Name

Score

A Intro MongoDB Sok San 18A Intro MongoDB Sao Ry 17B Intro MySQL Chan Tina 19B Intro MySQL Tith Sophea 16

Page 8: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Functional Dependencies

8

We say an attribute, B, has a functional dependency on another attribute, A, if for any two records, which havethe same value for A, then the values for B in these two records must be the same. We illustrate this as:

A B (read as: A determines B or B depends on A)

employee name email address

employee name

project email address

Sok San POS Mart Sys [email protected] Ry Univ Mgt Sys [email protected] San Web Redesign [email protected] Sokna POS Mart Sys [email protected] Ry DB Design [email protected]

Page 9: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Functional Dependencies (cont.)

9

EmpNum EmpEmail EmpFname EmpLname123 [email protected] John Doe456 [email protected] Peter Smith555 [email protected] Alan Lee633 [email protected] Peter Doe787 [email protected] Alan Lee

If EmpNum is the PK then the FDs: EmpNum EmpEmail, EmpFname, EmpLname must exist.

Page 10: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Functional Dependencies (cont.)

10

EmpNum EmpEmail, EmpFname, EmpLname

EmpNumEmpEmail

EmpFnameEmpLname

EmpNum EmpEmail EmpFname EmpLname

3 different ways you might see FDs depicted

Page 11: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Determinant

11

Functional Dependency

EmpNum EmpEmail

Attribute on the left hand side is known as the determinant

• EmpNum is a determinant of EmpEmail

Page 12: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Second Normal Form (2NF)

The official qualifications for 2NF are:

1.A table is already in 1NF.2.All nonkey attributes are fully dependent on the

primary key.All partial dependencies are removed to place in another table.

12

Page 13: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

CourseID SemesterID

Num Student

Course Name

IT101 201301 25 DatabaseIT101 201302 25 DatabaseIT102 201301 30 Web ProgIT102 201302 35 Web ProgIT103 201401 20 Networking

Example of a table not in 2NF:

Primary Key

The Course Name depends on only CourseID, a part of the primary keynot the whole primary {CourseID, SemesterID}.It’s called partial dependency.Solution:

Remove CourseID and Course Name together to create a new table.13

Page 14: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

CourseID Course NameIT101 DatabaseIT101 DatabaseIT102 Web ProgIT102 Web ProgIT103 Networking

SemesterDone? Oh no, it is still not in 1NF yet. Remove the repeating groups too.Finally, connect the relationship.

CourseID Course NameIT101 DatabaseIT102 Web ProgIT103 Networking

CourseID SemesterID

Num Student

IT101 201301 25IT101 201302 25IT102 201301 30IT102 201302 35IT103 201401 20

14

Page 15: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Third Normal Form (3NF)The official qualifications for 3NF are:

1.A table is already in 2NF.2.Nonprimary key attributes do not depend on

other nonprimary key attributes (i.e. no transitive dependencies)All transitive dependencies are removed to place in another table.

15

Page 16: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

StudyID Course Name Teacher Name Teacher Tel1 Database Sok Piseth 012 123 4562 Database Sao Kanha 0977 322 1113 Web Prog Chan Veasna 012 412 3334 Web Prog Chan Veasna 012 412 3335 Networking Pou Sambath 077 545 221

Example of a Table not in 3NF:

Primary Key The Teacher Tel is a nonkey attribute, andthe Teacher Name is also a nonkey atttribute.But Teacher Tel depends on Teacher Name.It is called transitive dependency.

Solution: Remove Teacher Name and Teacher Tel together to create a new table.

16

Page 17: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Teacher Name Teacher TelSok Piseth 012 123 456Sao Kanha 0977 322 111Chan Veasna 012 412 333Chan Veasna 012 412 333Pou Sambath 077 545 221

Done? Oh no, it is still not in 1NF yet. Remove Repeating row.

Teacher Name

Teacher Tel

Sok Piseth 012 123 456Sao Kanha 0977 322 111Chan Veasna 012 412 333Pou Sambath 077 545 221

Note about primary key:-In theory, you can choose Teacher Name to be a primary key.-But in practice, you should add Teacher ID as the primary key.

ID Teacher Name

Teacher Tel

T1 Sok Piseth 012 123 456T2 Sao Kanha 0977 322

111T3 Chan Veasna 012 412 333T4 Pou Sambath 077 545 221

StudyID

Course Name T.ID

1 Database T12 Database T23 Web Prog T34 Web Prog T35 Networking T4

17

Page 18: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Boyce Codd Normal Form (BCNF) – 3.5NFThe official qualifications for BCNF are:

1.A table is already in 3NF.2.All determinants must be superkeys.All determinants that are not superkeys are

removed to place in another table.

18

Page 19: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Boyce Codd Normal Form (BCNF) (Cont.) Example of a table not in BCNF:

Key: {Student, Course} Functional Dependency:

{Student, Course} Teacher Teacher Course

Problem: Teacher is not a superkey but determines Course.

19

Student Course TeacherSok DB JohnSao DB WilliamChan E-Commerce ToddSok E-Commerce ToddChan DB William

Page 20: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

20

Student CourseSok DBSao DBChan E-CommerceSok E-CommerceChan DB

Course TeacherDB JohnDB WilliamE-Commerce Todd

CourseDBE-Commerce

Solution: Decouple a table contains Teacher and Course from from original table (Student, Course). Finally, connect the new and old table to third table contains Course.

Page 21: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Forth Normal Form (4NF)The official qualifications for 4NF are:

1.A table is already in BCNF.2.A table contains no multi-valued dependencies.

Multi-valued dependency: MVDs occur when two or more independent multi valued facts about the same attribute occur within the same table.A B (B multi-valued depends on A)

21

Page 22: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Forth Normal Form (4NF) (Cont.) Example of a table not in 4NF:

Key: {Student, Major, Hobby} MVD: Student Major, Hobby

22

Student Major HobbySok IT FootballSok IT VolleyballSao IT FootballSao Med FootballChan IT NULLPuth NULL FootballTith NULL NULL

Page 23: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

23

Student MajorSok ITSao ITSao MedChan ITPuth NULLTith NULL

Student HobbySok FootballSok VolleyballSao FootballChan NULLPuth FootballTith NULL

StudentSokSaoChanPuthTith

Solution: Decouple to each table contains MVD. Finally, connect each to a third table contains Student.

Page 24: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Fifth Normal Form (5NF)The official qualifications for 5NF are:1.A table is already in 4NF.2.The attributes of multi-valued dependencies

are related.

24

Page 25: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

Fifth Normal Form (5NF) (Cont.) Example of a table not in 5NF:

Key: {Seller, Company, Product} MVD: Seller Company, Product Product is related to Company.

25

Seller Company ProductSok MIAF Trading ZenyaSao Coca-Cola Corp CokeSao Coca-Cola Corp FantaSao Coca-Cola Corp SpriteChan Angkor Brewery Angkor BeerChan Cambodia

BreweryCambodia Beer

Page 26: Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF

26

Seller CompanySok MIAF TradingSao Coca-Cola CorpChan Angkor BreweryChan Cambodia

Brewery

Seller

Product

Sok ZenyaSao CokeSao FantaSao SpriteChan Angkor

BeerChan Cambodia

Beer

Company ProductMIAF Trading ZenyaCoca-Cola Corp

Coke

Coca-Cola Corp

Fanta

Coca-Cola Corp

Sprite

Angkor Brewery

Angkor Beer

Cambodia Brewery

Cambodia Beer

SellerSokSaoChan

CompanyMIAF TradingCoca-Cola CorpAngkor BreweryCambodia Brewery

ProductZenyaCokeFantaSpriteAngkor BeerCambodia Beer

1

1

1

1

1

1

M

M

M

M

M