Advertisement

Advertisement

Normalization Denormalization in Sql

 

What is Normalisation Denormalisation in Sql

1. Normalization

Normalization is a process used in database design to minimize redundancy and dependency by organizing fields and tables. The goal is to divide a database into smaller tables and link them using relationships, ensuring data integrity and efficient data retrieval.

2. Denormalization

Denormalization is the reverse process of normalization. It involves combining normalized tables to improve read performance by reducing the number of joins. However, it may introduce redundancy, which can lead to data anomalies.

3. First Normal Form (1NF)

Definition: A table is in 1NF if:

  • It contains only atomic (indivisible) values, meaning no repeating groups or arrays.
  • Each column contains unique values.
  • There is a unique identifier (primary key) for each row.

Example:

IDNamePhone Numbers
1John12345, 67890
2Jane54321, 98765

This table violates 1NF because the "Phone Numbers" column contains multiple values. To normalize:

IDNamePhone Number
1John12345
1John67890
2Jane54321
2Jane98765

4. Second Normal Form (2NF)

Definition: A table is in 2NF if:

  • It is already in 1NF.
  • All non-key attributes are fully functionally dependent on the entire primary key (no partial dependencies).

Example: Consider a table where the primary key consists of two columns (Order ID, Product ID), and the table also includes other information like product description:

Order IDProduct IDProduct DescriptionQuantity
1101Laptop2
1102Mouse3
2101Laptop1

In this case, "Product Description" depends only on "Product ID" and not on the full primary key. To bring this table into 2NF, we must split the table:

Order Table:

Order IDProduct IDQuantity
11012
11023
21011

Product Table:

Product IDProduct Description
101Laptop
102Mouse

5. Third Normal Form (3NF)

Definition: A table is in 3NF if:

  • It is already in 2NF.
  • There are no transitive dependencies (non-key attributes do not depend on other non-key attributes).

Example: Consider a table that includes Customer information along with Order data:

Order IDCustomer IDCustomer NameCustomer City
1201AliceNew York
2202BobLos Angeles
3201AliceNew York

Here, "Customer City" depends on "Customer ID," which is a non-key attribute, so there is a transitive dependency between "Customer City" and "Order ID." To normalize this into 3NF:

Order Table:

Order IDCustomer ID
1201
2202
3201

Customer Table:

Customer IDCustomer NameCustomer City
201AliceNew York
202BobLos Angeles

This removes the transitive dependency.


Summary of Normal Forms:

  • 1NF: Eliminate repeating groups (each cell contains atomic values).
  • 2NF: Remove partial dependencies (every non-key column depends on the whole primary key).
  • 3NF: Remove transitive dependencies (non-key columns depend only on the primary key).

These forms ensure that the database is efficiently organized and prevents redundancy.

Normalization Denormalization in Sql Normalization Denormalization in Sql Reviewed by Rikesh on October 07, 2024 Rating: 5

No comments:

Powered by Blogger.