Wednesday, 12 August 2009

Database Normalization

Normalization is a process of certain rules to be applied while a database is being designed. The goal of this process is to prevent unnecesary data repeats and ensuring data dependencies and efficent use of data.

So what are these rules? There are five known normalization forms. Lets explain these one by one;

First Normal Form (1NF):
One row can contain only one information for a specific data.

Example;
WRONG: We have a Cars table and this table has a column called FuelType as nvarchar(255) and we are storing the data as
Cars
CarIDFuelType
1"Petrol, Diesel"
2"Diesel"
3"Electric, Diesel"




CORRECT: We need to create a new table called FuelTypes which will store all the fuel types and instead of using FuelType nvarchar(255) column, FuelTypeID int should be used and needs to be related to FuelTypes table.
FuelTypes
FuelTypeIDName
1"Petrol"
2"Diesel"
3"Electric"

Cars
CarIDFuelTypeID
11
12
22
33
32

Second Normal Form (2NF)

A table that has a lookup column (which is primary key in other table) can not contain any information from that lookup table.

Example;
WRONG:
Services
ServiceIDWorkerIDWorkerName
12Serkant
23Catherine

Workers
WorkerIDWorkerName
2Serkant
3Catherine


CORRECT:
Services
ServiceIDWorkerID
12
23



Workers
WorkerIDWorkerName
2Serkant
3Catherine

Third Normal Form (3NF)

A table whose non-primary key fields are dependent only on the primary key and therefore have no dependence any other non-primary key field in the table.

Example;
WRONG:
Services
ServiceIDWorkerNameWorkerPhone
1Serkant0777 777 7777
2Daniel0777 888 8888


CORRECT:
Services
ServiceIDWorkerID
12
23

Workers
WorkerIDWorkerNameWorkerPhone
2Serkant0777 777 7777
3Daniel0777 888 8888


Fourth Normal Form (4NF)


A table can not contain two or more 1:n or n:m relationships that are not directly related.

Example;
WRONG:
Book Deliveries
BookstoreBookArea
Bookstore ABook AArea A
Bookstore ABook AArea B
Bookstore BBook BArea B
Bookstore CBook BArea C


CORRECT:
Bookstore Books
BookstoreBook
Bookstore ABook A
Bookstore BBook A
Bookstore BBook B


Bookstore Delivery Areas
BookstoreArea
Bookstore AArea A
Bookstore BArea A
Bookstore BArea B
Bookstore BArea C


Fifth Normal Form (5NF)

This form deals with cases where information can be reconstructed from smaller pieces of information that can be maintained with less redundancy.

Example;
WRONG:
SalesmanCompanyProduct
Salesman ACompany AProduct A
Salesman ACompany AProduct B
Salesman BCompany BProduct A
Salesman BCompany BProduct B


CORRECT:

Company Salesmen
CompanySalesman
Company ASalesman A
Company BSalesman B


Company Products
CompanyProduct
Company AProduct A
Company AProduct B
Company BProduct A
Company BProduct B

Salesman Products
SalesmanProduct
Salesman AProduct A
Salesman AProduct B
Salesman BProduct A
Salesman BProduct B

No comments:

Post a Comment