Första normalformen
By: K. I've been asked to be the data modeler for a new application and I've not done this before. I understand the idea of getting the entities and how they connect together from customer interviews, but how do I apply the normalization rules to my data model? Also, why are they important? Check out this tip to learn more. Solution When we apply normalization rules to our database models, we're looking to preserve data integrity.
Normalization does this in two ways:.
Normalformer och normalisering
There are several normal forms, but for this tip we'll focus on first normal form. Subsequent tips will expand with the other typically used normal forms. First normal form has two requirements:. The idea here is that data is stored in a way where there aren't groups of data in a column for instance, multiple items in an order in a single column or there aren't multiple columns of the same thing in a table.
Here we've created a table for Orders and one of the columns contains everything that was part of that order. Visually, we'd see it like so:. One of the problems here is what happens if we have to change the order? What if the person only wanted a single t-shirt?
Understanding First Normal Form in SQL Server
Or what if we wanted to search for every purchase of medium-sized t-shirts? Both types of operations are more difficult than they should be because of the way the data is stored. We're not just contending with t-shirts, but also pants or shorts. This is an example where we see groups of data in a single column. If this is the way our data is modeled, it's not in first normal form.
Perhaps we've learned our lesson and we don't want groups of data in a single column. Therefore, we break the order items up into multiple columns, say 2, because we've never had an order consisting of more than 2 different items.
First Normal Form: An introduction to SQL table normalization
The code that would create such a construct is this:. Hopefully you are already spotting some issues. For instance, what happens if I want 3 items for a particular order? Now I'm stuck, because the table only supports 2. Or what if I wanted to search for all khaki pants that were ordered?
Data Modeling: Understanding First Normal Form in SQL Server
I have to search both sets of columns to get this information. If I expanded the table to have even more columns to support the ability to have more items on a particular order, then I would have to increase all my searches accordingly. It's a mess! Another issue with either table is how do you identify a particular order? For instance, for the two rows of data we have, I might be able to identify a particular order by looking at the time the order came in and who the order was for, but as can be seen here, the orders look like they came in at the same time.
So I can't exactly identify which order is which. We could go and include every column, but while that happens on occasion, it's not how things tend to work in the real world. That can be built into the data model. We know we need to identify each row uniquely.