Normalization and Databases Asfia Rahman
What is? process of structuring data within a database. The process starts with a flat file and finishes with a set of related tables. UNF / 0NF 1NF 2NF 3NF
0NF Unnormalised form a flat file. contain non-atomic data, repeating groups of data and possibly redundant data. Non-atomic data is where a field contains more than one item of data. Repeating groups of data is when fields are repeated for each record or a record appears to have more than one set of data for a group of fields. Redundant data exists when data is repeated unnecessarily. This can be spotted when data can be identified by knowing it is dependent upon another field.
Examples of redundant data and non-atomic data
Examples of repeating and more than 1 set of data (also non-atomic)
1 NF all fields must contain atomic data (1 item only) there must be no repeating groups of data there must be a unique key.
Changing 0nf to 1nf Non-atomic Data (one field has more than 1 item) Order ID Order Date Customer ID Customer Name Customer Address Product Code Product name Quantity Price 3847 12/9/16 1224 Baker Jones Somewhere, city 1212 1232 Marzipan Flour 1 2 $1.50 $0.75 2320 15/10/16 2342 Emma W Someplace,, cityW 2343 Sugar Eggs 3 6 $0.83 $0.15 Non-atomic Data (one field has more than 1 item) Redundant data ( price is repeated every time the product comes)
How to do it? Split any repeating group of data into a separate table Add the key of the original table to act as a foreign key. Order ID Order Date Customer ID Customer Name Customer Address Product Code Product name Quantity Price 3847 12/9/16 1224 Baker Jones Somewhere, city 1212 1232 Marzipan Flour 1 2 $1.50 $0.75 2320 15/10/16 2342 Emma W Someplace,, cityW 2343 Sugar Eggs 3 6 $0.83 $0.15 Compound key Order ID Product Code Product name Quantity Price 3847 1212 Marzipan 1 $1.50 1232 Flour 2 $0.75 2320 3 2342 Sugar $0.83 2343 Eggs 6 $0.15 Order ID Order Date Customer ID Customer Name Customer Address 3847 12/9/16 1224 Baker Jones Somewhere, city 2320 15/10/16 2342 Emma W Someplace,, cityW
2 NF No partial key dependencies Means no non-key fields can be dependent upon part of a primary key. only applies to tables with compound Keys These fields and that part of the compound key will form a new table.
1nf to 2nf 1 Product Code Product name Price 1212 Marzipan $1.50 1232 Flour $0.75 2342 Sugar $0.83 2343 Eggs $0.15 Order ID Product Code Product name Quantity Price 3847 1212 Marzipan 1 $1.50 1232 Flour 2 $0.75 2320 3 2342 Sugar $0.83 2343 Eggs 6 $0.15 2 Order ID Product Code Quantity 3847 1212 1 1232 2 2320 3 2342 2343 6 Split out data dependent on only part of a composite key Add the key of the original table to act as a foreign key. The PK for the table made will have a CK made up from the original PK along with the main identifier for that group.
3NF no non-key dependencies. No fields that are dependent upon another field that is not a primary key
2nf to 3nf Split out data dependent on a non-key field Foreign key will be the non-key field left behind. Order ID Order Date Customer ID 3847 12/9/16 1224 2320 15/10/16 2342 Order ID Order Date Customer ID Customer Name Customer Address 3847 12/9/16 1224 Baker Jones Somewhere, city 2320 15/10/16 2342 Emma W Someplace,, cityW 2 1 Customer ID Customer Name Customer Address 1224 Baker Jones Somewhere, city 2342 Emma W Someplace,, cityW
Final Tables Product Code Product name Price 1212 Marzipan $1.50 1232 Flour $0.75 2342 Sugar $0.83 2343 Eggs $0.15 Order ID Product Code Quantity 3847 1212 1 1232 2 2320 3 2342 2343 6 Customer ID Customer Name Customer Address 1224 Baker Jones Somewhere, city 2342 Emma W Someplace,, cityW Order ID Order Date Customer ID 3847 12/9/16 1224 2320 15/10/16 2342