Normalization of a Database
Published: 2020-09-27 02:43:53

Welcome back!

In this blog, I will be going over the normalization I implemented into my COVID-19 database to reduce redundancy and clean up the database in general.

So, the best place to start is to answer the overarching question: what *is* normalization? Also, what are these forms of normalization?

First, normalization is the process of taking tables within a database and breaking them down to reduce the redundancy or repetition of a specific quality. In the example below, in our database, we have a column in the table 'GeographicLocation' named 'Country'. However, since the table also implements the use of zip codes, the 'Country' column becomes irrelevant. Zipcodes lock the app into being US-only.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nqtawtkwegy9v8cnfqig.png)

Normalization has different forms to it. The two I will be exploring in this blog post are 1NF and 2NF -- 1st and 2nd Normal Form.

1NF is the "base" level of all normal forms. Normal forms build off of each other. Each subsequent form must carry all of the qualities of the previous forms. So, 3NF has to have all of the properties in 1NF, 2NF, and the new ones in 3NF.

In the example below, I have my 'Behavior' table. 1NF has three properties that need to be true:

1. Data is stored within tables and rows; can be uniquely identified by a primary key
2. Said data is in its most reduced form
3. There are no repeating/redundant columns

'Behavior' passes these criteria because all of the data in its table is properly stored and has the overarching 'BehaviorID' primary key, all of the data cannot be reduced any further, and there are no redundant columns present.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4fk0hkcaas3bbrejciow.png)

Moving on, we have the 2nd Normal Form. Looking at the example below, we see that we have a column named 'WorkplacePolicy' in the 'Occupation' table. While the column is not inherently wrong in terms of formatting, it is wrong in terms of placement. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/445d3zy32w7gt4qz4zo6.png)

Looking at all of the other attributes on the table, they all directly relate to the primary key. In this case, it is 'CompanyID'. 'WorkplacePolicy' doesn't directly correlate to an individual's ID; rather, it correlates to an entire company. If the policy changes, the change may not sync up to the other values in other columns, which is known as an "anomaly".

To fix this, I created a separate table called 'WorkplacePolicy' with different Boolean values to make it more fleshed out and able to operate independently. I also created a new column named 'CompanyName' to act as a new foreign key in 'Occupation'.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k874432yvv9j46s5heyj.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x69eebead9qg0v3p3egi.png)

So, I hope this gave a better overview for normalization, 1NF, and 2NF than my video did -- the purpose of my video was mainly to show off the changes I made to my database, while this post was to really go into the technical component of it all.

If you haven't watched my video, you should! I go over the changes above and also how to properly use the *alter* command.

{% youtube QKBV7exHzzY %}

With that said, thank you for reading -- I hope you have a great day!

~ Incerah