Sunday, December 4, 2011

Understanding normal forms

Normal forms or normalization was very hard for me understand until I came across a webpage and everything became quite clear for me. The plus point about this webpage was the way it explained the normal forms. The explanation does not use relational algebra or functional dependencies to understand normal forms.
1st Normal Form:
1st normal Form deals with the shape of a Record Type. 
Under first normal form, all occurrences of 
record type must contain the same number of fields.

First normal form excludes variable repeating fields and groups.

2nd Normal Form:

Second normal form is violated when a non-key field is a fact about a subset of a key.
It is only relevant when the key is composite, i.e., consists of several fields.


Consider the following inventory record:
R(Part, Wherehouse,Qty,Wherehouse-Address)
Here the key is Part-Wherehouse fields together but Wherehouse-Address is fact about Wherehouse only. So it violates the 2nd Normal form.
The decomposition should be:
R1(Part,Wherehouse,Qty)    and      R2( Wherehouse,Wherehouse-Address)
The problems here were:
The warehouse address is repeated in every record that refers to a part stored in that warehouse.
If the address of the warehouse changes, every record referring to a part stored in that warehouse must be updated.
Because of the redundancy, the data might become inconsistent, with different records showing different addresses for the same warehouse.
If at some point in time there are no parts stored in the warehouse, there may be no record in which to keep the warehouse's address.


3rd Normal Form:

Third normal form is violated when a non-key field is a fact about another non-key field.
Example:
R(Employee,Department,Location)
Here the key is Employee. But Location is for the Department(Another non key). So it violates 3rd Normal Form.
Decomposition:
R1(Employee,Department)     and R2(Employee,Location)


The problems here were:
The department's location is repeated in the record of every employee assigned to that department if department location changes, every such record must be updated.
Because of the redundancy, the data might become inconsistent, with different records showing different locations for the same department.
If a department has no employees, there may be no record in which to keep the department's location.


To summarize, a record is in second and third normal forms if every field is either part of the key or provides a (single-valued) fact about exactly the whole key and nothing else.

No comments:

Post a Comment