363,927 Members | 2741 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Database Normalization and Table Structures

MMcCarthy
Expert Mod 5K+
P: 8,068
Normalisation is the term used to describe how you break a file down into tables to create a database. There are 3 or 4 major steps involved known as 1NF (First Normal Form), 2NF (Second Normal Form), 3NF (Third Normal Form) and BCNF (Boyce-Codd Normal Form). There are others but they are rarely if ever used. A database is said to be Normalised if it is in 3NF (or ideally in BCNF). These steps are descibed as follows:


Note: When attribute is used we are speaking of a field in the table

1NF

To put a database in 1N
  • ensure that all attributes (columns) are atomic (which means that any single field should only have a value for ONE thing).
Examples:

In a database a table on Customers would have an address attribute. The address is made up of Company Name, Address Line1, Address Line2, Address Line3, City, Postcode. There are 6 values to this address and as such each should have it's own field (column).


If your company sold furniture a table on products could have a description attribute. If for example that attribute was 'Beech Desk 120w x 75h x 50d'. Ideally this would be broken down into a number attributes like 'Colour', 'Type', 'Width', 'Height' and 'Depth'. The reason for this is it would allow you to seach the database for all Desks, for all pieces of Beech furniture, for all desks with a width of 120 etc.
  • Create a separate table for each set of related data and Identify each set of related data with a primary key
Example:


In a general Invoicing database you would have a separate table for Customers, Orders, Products, Invoices and you would probably need tables for OrderDetails and InvoiceDetails as well. Each of these tables must have their own primary key. Each of these tables except for customers would have a foreign key reference to the primary key of another table. (See Relationships below)
  • Do not use multiple fields in a single table to store similar data
Example:
(Underlined fields are Primary Keys and Italicised fields are Foreign Keys)

In a customer order you could have more than one product. That is the customer has ordered more than one item. If you tried to put all of this in one table as {OrderID, CustomerID, OrderDate, Product1, Product2, Product3} what would happen if the customer ordered more than 3 products. There would also be implications for querying the kind or quantiy of products ordered by a customer. Therefore these product fields don't belong in the order table which is why we would have an OrderDetails table which would have a foreign key reference to the Orders table {OrderDetailsID, OrderID, ProductID, Quantity}. Using productID as a foreign key to the product table means you don't have to identify the product attributes here. This also allows you to enter a quantity figure for the product ordered.

Relationships:

All tables should have a 1 to 1 or 1 to many relationship. This means for example that 1 customer can have 1 or many orders and 1 order can have 1 or many details.

Therefore Orders table would have a foreign key reference to the Customer table primary key {OrderID, CustomerID, OrderDate} and the OrderDetails table would have a foreign key reference to the Order table primary key {OrderDetailsID, OrderID, ProductID, Quantity}. This table also contains a foreign key reference to the Products table. As a product is likely to be ordered more than once there is a many to 1 relationship between the OrderDetails and the Products table.

If any tables have a many to many relationship this must be broken out using a JOIN table. For example, Customers can have many Suppliers and Suppliers can supply to many Customers. This is known as a many to many relationship. You would need to create a JOIN table that would have a primary key made up of a foreign key reference to the Customers table and a foreign key reference to the suppliers table. Therefore the SuppliersPerCustomer table would be {SupplierID, CustomerID}. Now the Suppliers table will have a 1 to many relationship with the SuppliersPerCustomer table and the Customers table will also have a 1 to many relationship with the SuppliersPerCustomer table.

2NF

The database must meet all the requirements of the 1NF.

In addition, records should not depend on anything other than a table's primary key (a primary key can be made up of more than one field, only if absolutely necessary like in a JOIN table).

Example:

A customers address is needed by the Customers table, but also by the Orders, and Invoices tables. Instead of storing the customer's address as a separate entry in each of these tables, store it in one place, either in the Customers table or in a separate Addresses table.

3NF

The database must meet all the requirements of the 1NF and 2NF.

The third normal form requires that all columns in a relational table are dependent only upon the primary key. A more formal definition is:
  • A relational table is in third normal form (3NF) if it is already in 2NF and every non-key column is non transitively dependent upon its primary key.
In other words, all nonkey attributes are functionally dependent only upon the primary key. All 3NF really means is that all fields (attributes) should be dependent on the tables primary key. If they are not they should be put in their own table. This means that every attribute unless it is a primary or foreign key must be DIRECTLY dependent on the Primary Key of this table and not on some other column.

Example:

The Customer table contains information such as address, city, postcode imagine it also contained a column called shipping cost. The value of shipping cost changes in relation to which city the products are being delivered to, and therefore is not directly dependent on the customer even though the cost might not change per customer, but it is dependent on the city that the customer is in. Therefore we would need to create another separate table to hold the information about cities and shipping costs.

BCNF

A relation is in Boyce-Codd Normal Form (BCNF) if every determinant is a candidate key. BCNF is very similar to 3NF but deals with dependencies within the primary keys. BCNF in it's simplist terms just says don't have a primary key made up of more than one field unless it is a join table to disperse a many to many relationship and only contains the two primary keys of the tables it is joining.

Most relations that are in 3NF are also in BCNF. It only happens that a relation which is in 3NF is not in BCNF when the primary key in a table is made up of more than one field and the other columns are not dependent on both fields but only on one or the other.

A database is said to be normalised if it is in 3NF and/or BCNF

Notes:
Someone asked why normalisation is important. One of our experts Scott Price posted a very useful reply to this in post #15
Jan 8 '07 #1
Share this Article
Share on Google+
76 Comments


debasisdas
Expert Mod 5K+
P: 6,614
Thanx

Its a really helpful article.
May 4 '07 #2

MMcCarthy
Expert Mod 5K+
P: 8,068
Thanx

Its a really helpful article.
You're welcome :)
May 4 '07 #3

Motoma
Expert 2.5K+
P: 2,649
Unequivocal thanks for posting this article, Mary.
May 15 '07 #4

srinivasarao yarru
P: 4
i am a learner so it is very helpful.
Jun 1 '07 #5

MMcCarthy
Expert Mod 5K+
P: 8,068
i am a learner so it is very helpful.
I'm glad to hear it.

If you have any questions post them in the Access forum. We will do what we can to help.

Mary
Jun 1 '07 #6

MMcCarthy
Expert Mod 5K+
P: 8,068
Unequivocal thanks for posting this article, Mary.
No problem.

You're welcome.
Jun 1 '07 #7

debalina
P: 2
Thank U Very Much For The Article ... It Is Really Helpful For Me ..
Jun 16 '07 #8

MMcCarthy
Expert Mod 5K+
P: 8,068
Thank U Very Much For The Article ... It Is Really Helpful For Me ..
Thank you.

I'm glad you found it helpful.

Mary
Jun 16 '07 #9

KevHill
P: 6
Very nice, it would be good to add something about the rationale of why you would want something normalized. I assume it ease of upkeep and efficiency of searches, etc.
Jun 25 '07 #10

MMcCarthy
Expert Mod 5K+
P: 8,068
Very nice, it would be good to add something about the rationale of why you would want something normalized. I assume it ease of upkeep and efficiency of searches, etc.
It is!

I'll try to put something together when I get the time. It's a little difficult to explain the reasons why relational database management works in simplified terms but I'll see what I can do. :)
Jun 25 '07 #11

Kumarswamy
P: 4
This is very Useful ..Thanks a lot
Jun 28 '07 #12

MMcCarthy
Expert Mod 5K+
P: 8,068
This is very Useful ..Thanks a lot
You're welcome!

Glad you found it useful.
Jun 28 '07 #13

Scott Price
Expert 100+
P: 1,261
Great post!

Suggested reading for anyone interested in DB normalisation/design is: Database Design for Mere Mortals Second Edition by Mike Hernandez.

Mike writes in language that the rest of us non-geniuses can actually understand!
Jul 4 '07 #14

MMcCarthy
Expert Mod 5K+
P: 8,068
Great post!

Suggested reading for anyone interested in DB normalisation/design is: Database Design for Mere Mortals Second Edition by Mike Hernandez.

Mike writes in language that the rest of us non-geniuses can actually understand!
Thanks Scott

Glad you liked it
Jul 5 '07 #15

Scott Price
Expert 100+
P: 1,261
This reply was given when a member asked why normalisation is important.
My 2 cents worth :-)

Database normalization is a rather arcane topic that you will not be able to really understand until you work your way a little further into the database design process.

Simply put the rules of db design were developed years ago by some mathematicians working in things like 3rd order predicate logic, etc. Access and other db programs are built with these rules in mind. Yes, you can fudge your way around the way these rules/programs are built, but you do so at considerable cost to your brainpower, frustration levels, etc... Some things also not only become much more difficult to do, they can become downright impossible! A much simpler (and far more productive and safe) method is to simply accept that the rules are there for a reason.

As you work on db's more, you will begin to learn WHY the rules are there.

One simple example using your scenario: Suppose you store everything in one big table. Then next year, Quebec decides it doesn't want to be part of Canada anymore (yeah, sure... never happen...). How are you going to change the values in your 'one big table' that refer to Quebec? You will have to write code to search the fields to find each instance of the word Quebec and change it to whatever it gets changed to.

If, instead, you have a normalized db, the change is quite simple: you go to the table named something like tblProvinces, and make one change to the entry Quebec which then affects every entry that it is related to through table relationships. This concept is called data integrity, and is far more important than this simple example illustrates...

Change the example to involve accounting systems and $'s and you will understand perhaps more of the potential errors involved.

For example if you are storing employee salaries in one big table that has all information in it. How are you going to find salaried employees of a certain level of salary? Write code to search the fields to find the instances between a certain $ figure... How are you then going to change each employees salary when they get a raise? How are you going to generate a report to make sure that someone isn't (horrors) making more money than their experience/skill level?

It's only possible to dumb this subject down to a certain point! If you weren't willing to learn how to do it right, you wouldn't be here at this site asking for pointers, so I hope the above information is some help!

Scott.
Jul 15 '07 #16

arunmen
P: 4
Thanks dude It was an wonderful tutorial.
Aug 4 '07 #17

MMcCarthy
Expert Mod 5K+
P: 8,068
Thanks dude It was an wonderful tutorial.
You're welcome. Glad it helped.
Aug 5 '07 #18

ammoos
100+
P: 100
Really great article. Congratulations and also thanks a lot for providing us this detailed information
Sep 12 '07 #19

MMcCarthy
Expert Mod 5K+
P: 8,068
Really great article. Congratulations and also thanks a lot for providing us this detailed information
You're welcome, thank you.
Sep 12 '07 #20

Despina
P: 4
thank you! it really helps
Sep 17 '07 #21

MMcCarthy
Expert Mod 5K+
P: 8,068
thank you! it really helps
Glad to hear it.
Sep 17 '07 #22

prk
P: 2
prk
thanks a lot..

Its really helpful.
Nov 13 '07 #23

MMcCarthy
Expert Mod 5K+
P: 8,068
thanks a lot..

Its really helpful.
I'm happy you found it useful.
Nov 13 '07 #24

sierra7
Expert 100+
P: 445
Thanks for your excellent tutorial but I would like to ask whether there is a difference in having a compound key (OrderID + LineNo in the OrderDetails table) compared to single field unique key (OrderDetailsID).
Does the DBMS function more efficiently?
Is this a pre-requisite for up-sizing to SQL Server?

I have a stock control system where OrderID is over 125,000 and it is not uncommon to have 10 or more Line items on an order, so my field equivalent to OrderDetailID is very large, and does not do anything for me or the users. All access to the Order Details is either by OrderID, ProductID, Due Date etc.

I have a subordinate table which tracks Batch Number, Quantity Picked so I can calculate profit, print certificates etc., but this is linked (joined) via OrderID + LineNo which is more meaningful (I used to work with dBase and suffered file corruption so often had to recover data. I have only had one instance of this with Access in the last 8 years, but I still like to 'read' the tables!)

I am considering removing OrderDetailsID and SOBatchDetailsID as a step to improve performance (less data should mean less band-width over the network, 'cos Access is sending all data as it is not true Client/Server) Is this a move in the wrong direction because in the longer term I guess will have to upsize to SQL ?
Nov 15 '07 #25

76 Comments

Post your comment

Sign in to post your comment or Sign up for a free account.