Database Normalization and Table Structures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    Database Normalization and Table Structures

    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:
    [imgnothumb]http://bytes.com/images/howtos/normalization_t able2.gif[/imgnothumb]
    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).
    [imgnothumb]http://bytes.com/images/howtos/normalization_t able1.gif[/imgnothumb]

    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:
    [imgnothumb]http://bytes.com/images/howtos/normalization_t blDiagram1.gif[/imgnothumb]

    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 quantity 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.
    [imgnothumb]http://bytes.com/images/howtos/normalization_t blDiagram2.gif[/imgnothumb]
    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.
    [imgnothumb]http://bytes.com/images/howtos/normalization_t blDiagram3.gif[/imgnothumb]
    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 SuppliersPerCus tomer table would be {SupplierID, CustomerID}. Now the Suppliers table will have a 1 to many relationship with the SuppliersPerCus tomer table and the Customers table will also have a 1 to many relationship with the SuppliersPerCus tomer 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
    Last edited by MMcCarthy; Jan 16 '13, 09:48 PM.
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Thanx

    Its a really helpful article.

    Comment

    • MMcCarthy
      Recognized Expert MVP
      • Aug 2006
      • 14387

      #3
      Originally posted by debasisdas
      Thanx

      Its a really helpful article.
      You're welcome :)

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Unequivocal thanks for posting this article, Mary.

        Comment

        • srinivasarao yarru
          New Member
          • Mar 2007
          • 4

          #5
          i am a learner so it is very helpful.

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            Originally posted by srinivasarao yarru
            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

            Comment

            • MMcCarthy
              Recognized Expert MVP
              • Aug 2006
              • 14387

              #7
              Originally posted by Motoma
              Unequivocal thanks for posting this article, Mary.
              No problem.

              You're welcome.

              Comment

              • debalina
                New Member
                • Jun 2007
                • 2

                #8
                Thank U Very Much For The Article ... It Is Really Helpful For Me ..

                Comment

                • MMcCarthy
                  Recognized Expert MVP
                  • Aug 2006
                  • 14387

                  #9
                  Originally posted by debalina
                  Thank U Very Much For The Article ... It Is Really Helpful For Me ..
                  Thank you.

                  I'm glad you found it helpful.

                  Mary

                  Comment

                  • KevHill
                    New Member
                    • Jun 2007
                    • 6

                    #10
                    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.

                    Comment

                    • MMcCarthy
                      Recognized Expert MVP
                      • Aug 2006
                      • 14387

                      #11
                      Originally posted by KevHill
                      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. :)

                      Comment

                      • Kumarswamy
                        New Member
                        • Jun 2007
                        • 4

                        #12
                        This is very Useful ..Thanks a lot

                        Comment

                        • MMcCarthy
                          Recognized Expert MVP
                          • Aug 2006
                          • 14387

                          #13
                          Originally posted by Kumarswamy
                          This is very Useful ..Thanks a lot
                          You're welcome!

                          Glad you found it useful.

                          Comment

                          • Scott Price
                            Recognized Expert Top Contributor
                            • Jul 2007
                            • 1384

                            #14
                            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!

                            Comment

                            • MMcCarthy
                              Recognized Expert MVP
                              • Aug 2006
                              • 14387

                              #15
                              Originally posted by Scott Price
                              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

                              Comment

                              Working...