Database - First Normal Form (1NF)

Advertisements


First normal form (1NF) sets the very basic rules for an organized database:

  • Define the data items required, because they become the columns in a table. Place related data items in a table.

  • Ensure that there are no repeating groups of data.

  • Ensure that there is a primary key.

First Rule of 1NF:

You must define the data items. This means looking at the data to be stored, organizing the data into columns, defining what type of data each column contains, and finally putting related columns into their own table.

For example, you put all the columns relating to locations of meetings in the Location table, those relating to members in the MemberDetails table, and so on.

Second Rule of 1NF:

The next step is ensuring that there are no repeating groups of data. Consider we have the following table:

CREATE TABLE CUSTOMERS(
       ID   INT              NOT NULL,
       NAME VARCHAR (20)     NOT NULL,
       AGE  INT              NOT NULL,
       ADDRESS  CHAR (25),
       ORDERS   VARCHAR(155)
);

So if we populate this table for a single customer having multiple orders, then it would be something as follows:

IDNAMEAGEADDRESSORDERS
100Sachin36Lower West SideCannon XL-200
100Sachin36Lower West SideBattery XL-200
100Sachin36Lower West SideTripod Large

But as per 1NF, we need to ensure that there are no repeating groups of data. So let us break above table into two parts and join them using a key as follows:

CUSTOMERS table:

CREATE TABLE CUSTOMERS(
       ID   INT              NOT NULL,
       NAME VARCHAR (20)     NOT NULL,
       AGE  INT              NOT NULL,
       ADDRESS  CHAR (25),
       PRIMARY KEY (ID)
);

This table would have the following record:

IDNAMEAGEADDRESS
100Sachin36Lower West Side

ORDERS table:

CREATE TABLE ORDERS(
       ID   INT              NOT NULL,
       CUSTOMER_ID INT       NOT NULL,
       ORDERS   VARCHAR(155),
       PRIMARY KEY (ID)
);

This table would have the following records:

IDCUSTOMER_IDORDERS
10100Cannon XL-200
11100Battery XL-200
12100Tripod Large

Third Rule of 1NF:

The final rule of the first normal form, create a primary key for each table which we have already created.



Advertisements
Advertisements