Skip to main content

Database Normalization Basics

There are 3 primary reasons why a database should be normalized:
  1. It brings data integrity to the tables (entities). This facilitates proper inserting, updating, and deleting of data thereby preventing data anomalies. If a table is not normalized it runs the risk of inconsistent data, data redundancies, and inconsistent result sets.  
  2. To create entity relationships thereby providing referential integrity. Referential integrity ensures that relationships between tables remain consistent when entities are altered.
  3. To avoid having one set of users with a biased view of the data. For example a marketing department may view the data in line with their revenue needs rather than having an objective view of the data as it applies to the entire organization.
We begin by analyzing the current relationships that exist between the attributes within each entity.
Apparent in the Orders entity is the repeating data in the ITEM_NAME, ITEM_DESCRIPT, QUANTITY, and PRICE attributes. We break the entity out to have no repeating data in any attribute while leaving the structure of the entity the same.


















Each attribute now has a single value and there are now no repeating groups, or multiple data entries in any attribute.

Convert the un-normalized Orders entity to the First Normal Form.

We will need to have a combination of ORDER_ID and CUST_ID to uniquely identify the row. These will be our primary keys. We break down the Orders entity into a new Order entity, by extracting the information related to the product that was ordered. We now have 2 entities; Order, and Orders. We are now in 1st normal form. (1NF) where all key attributes are identified and there are no repeating groups. Our goal it to heave every entity act as a Noun, with the Noun's attributes only.



















Convert  to the Second Normal Form and provided the entity name, column names, and appropriate key types.


We further break out the Orders entity by creating an Items entity. Here we effectively remove the products being sold from the actual sales order information.
















Obvious at this point the Items entity can now be updated at any time in the future without disturbing the entity relationships. For example, we could add a SKU Number attribute to the Items entity, or an End Of Life product designation.

Orders entity is now cleaned up once the product attributes were removed and moved to the Items entity.













Converted the entities to the Third Normal Form and provided the entity name, column names, and appropriate key type. 

The primary advantage should be apparent; if a need arises at some point in the future to:
  • Add additional attributes to the items entity
  • Alter the DATE attribute's time stamp to MM-DD-YYYY in the ORDER entity.
This can be accomplished without disturbing the other entities and their relationships.
Here we now have our original Orders entity broken out into 3NF. Where each entity represents a single subject or noun and the primary keys are clearly defined. Attributes are now of a single value and simple (atomic data). However, this comes with a price as the additional entities will cause additional IO operations and processing logic to join them.


There are numerous articles on the advantages and disadvantages of normalization. Certainly the price of hard drives over time has changed and IO may not be the issue that it was in the past. Yet with the advent of tablet and mobile devices, the need for speed is at a premium once again.

Comments

Popular posts from this blog

:nth-child structural pseudo-class selectors

There are 4 pseudo-class expressions that are part of the :nth-child pseudo-class. Structural pseudo-class selectors target HTML elements based on the DOM tree. Basically, elements that cannot easily be targeted by simple selectors or combinations of selectors. What makes pseudo-classes so handy is the ability style elements dynamically based on its position in the DOM. :nth-of-type(N) :nth-last-child(N) :nth-child(N) :nth-last-of-type(N) :nth-of-type(N) selector My favorite of the 4 is the :nth-of-type(N) selector. The nth-of-type selector allows you to select child elements of a parent based on the particular type of the element, for example every 5th "li" element in a list. You can select even or odd elements, or the nth (order number) child in the group of elements. The class accepts the argument "n" which can can be a keyword, a number, or strings "odd", "even", or an expression "-n+3". Let's look at a simple but ef

The ICMP protocol

Let's look into the ICMP protocol. Specifically, ping and traceroute. ICMP is the Internet Control Message Protocol and is a component of the IP Layer. Basically, used by hosts to communicate diagnostic network layer information that is carried in the IP payload. It communicates error messages which are acted on by the IP layer or the UDP or TCP protocols. All of the exercises were carried out using the open source network protocol analyzer "Wireshark". www.wireshark.org Describe in detail the protocols ARP and ICMP. ARP is the Address Resolution Protocol is similar to that of DNS. Where DNS resolves IP addresses to domain names, ARP resolves network layer IP addresses to link layer MAC addresses. In order to send a datagram the source must give the adaptor the IP address and the MAC address. For example, host A wants to send a packet to host B. Host A uses a cached ARP table to look up the IP address for any existing records of host B's MAC address. If the MA

Creating triggers

Triggers are SQL statements which are stored with the intention of being activated or fired when an event associated with a database table occurs. This could be any event including an INSERT, UPDATE and DELETE. Lets begin by creating a few simple insert triggers CREATE a trigger on the ORDERLINE table for the insert event. The trigger will update the corresponding PRODUCT record QTY_ORDERED by adding the value of QTY to QTY_ORDERED. CREATE TRIGGER tr_qty_ordered_value_insert ON Orderline FOR INSERT AS BEGIN UPDATE product SET QTY_ORDERED = QTY_ORDERED + ((SELECT qty from INSERTED) * (SELECT unitprice from INSERTED)) WHERE product.ProductID = (SELECT ProductID from INSERTED); END; Command(s) completed successfully. CREATE a trigger on the ORDERLINE table for the delete event. The trigger will update the corresponding PRODUCT record QTY_ORDERED by subtracting the value of QTY FROM QTY_ORDERED. CREATE TRIGGER tr_qty_ordered_value_delete ON Orde