When designing a database, one of the priorities is ensuring that data is stored efficiently while remaining easy to retrieve. Poor database design can lead to duplicate information, inconsistent records, wasted storage space and slow queries.
The two techniques used by database designers to solve these challenges are:
- Normalization
- Denormalization
NORMALIZATION
It's the process of organizing data into smaller, related tables to reduce redundancy, improve consistency and maintain data integrity.
It stores each piece of information only once and connects related tables using keys.
Example:
Instead of having product details, customer details and order details in one table, you can separate them as below:
Customers Table
- Customer_ID
- Customer_Name
- Customer_Email
Products Table
- Product_ID
- Product_Name
- Product_Price
Orders Table
- Order_ID
- Customer_ID
- Product_ID
Database Anomalies
Database anomalies are inconsistencies or errors that occur during data operations (insertions, updates or deletions) due to a poorly designed database
Normalization prevents three kinds of anomalies:
i).Insert Anomaly - Occurs when you can't add one piece of information without adding another unrelated piece of information. Example: A new product can't be added until someone purchases it because product information is stored inside the order table.
ii).Update Anomaly - Occurs when changing one piece of information requires updating multiple rows. Example: If John wants to change his phone number and he had placed 200 orders and his phone number appears in 200 records.Changing his phone number means updating all 200 rows. Missing even one creates inconsistent data
iii).Delete Anomaly - Occurs when deleting one record accidentally removes important information. Example: If a customer has placed only one order, deleting that order also removes the only record containing the customer's information therefore disappearing from the database
Normal Forms
A normal form is a set of rules used to organize data in a relational database.
First Normal Form (1NF)
Ensures each column stores only one value and each row represents a single record.
A table is in the First Normal Form(1NF) if:
Every column contains single values. - Each cell should contain only one value.
There are no repeating groups. - Similar information should not be stored in multiple columns like Phone1, Phone2, Phone3.
Each row is unique. - Every record can be uniquely identified using a primary key.
Instead of:
| Student ID | Name | Courses |
|---|---|---|
| 101 | Khal | SQL, Python, Excel |
Every cell should contain only one value:
| Student ID | Name | Courses |
|---|---|---|
| 101 | Khal | SQL |
| 101 | Khal | Python |
| 101 | Khal | Excel |
Second Normal Form (2NF)
A table is in Second Normal Form (2NF) if:
- It is already in 1NF.
- Every non-key column depends on the entire primary key, not just part of it. This applies to tables with composite (combined) primary keys.
Example:
| Student ID | Course ID | Student Name | Grade |
|---|---|---|---|
| 101 | DB101 | Tywin | A |
| 101 | AC201 | Joffrey | B |
Student ID and Course ID are both primary keys which form a composite key. Student Name depends only on Student ID thus creating partial dependency. To fix this, the table is split into;
Students
|Student ID | Student Name |
Enrollment
|Student ID | Course ID |Grade|
Now every non-key column depends on the whole primary key
Third Normal Form (3NF)
A table is in Third Normal Form (3NF) if:
- It is already in 2NF.
- There are no transitive dependencies.
Transitive dependency - A non-key column should not depend on another non-key column. Every non-key column should depend directly on the primary key
Example:
| Employee ID | Department ID | Department Name |
|---|---|---|
| 101 | 10 | IT |
| 201 | 20 | Finance |
Department Name depends directly on Department ID and indirectly on Employee ID which is the primary key. That's a transitive dependency. To fix this, the table is split into;
Employees
|Employee ID | Department ID|
Departments
|Department ID | Department Name |
Now Department Name depends directly on Department ID, which is the primary key of the Departments table
Boyce-Codd Normal Form (BCNF)
It's used when some redundancy still exists despite meeting the requirements of 3NF.
For every dependency: The column determining another column (the determinant) must be a candidate key.
A candidate key is any column or combination of columns, that can uniquely identify a row.
Example: If a university has a rule that a lecturer teaches only one course and each course can have several lecturers. Each course always uses one room.
| Lecturer | Course | Room |
|---|---|---|
| John | Database | 802 |
| Snow | Database | 802 |
| Lannister | Networks | 810 |
Although Lecturer is the candidate key, Course also determines Room and because Course is not a candidate key, the table violates Boyce-Codd Normal Form (BCNF). To fix this, the table is split into;
Lecturers
|Lecturer|Course
Course
|Course|Room|
Now every determinant is a candidate key in its own table.
Advantages of Normalization
1.Reduced data duplication - Information is stored only once, avoiding unnecessary repetition and saving storage space
2.Improves consistency - Since data exists in a single location, updates are less likely to create conflicting information.
3.Prevents anomalies - Proper table organization avoids problems when inserting, updating or deleting records.
4.Improves data integrity - The database ensures that the related information remains accurate, complete and reliable.
3.Easier maintenance - Changes are made in one place, making the database simpler to update and extend over time.
4.Better storage efficiency - Less repeated information means less storage usage.
Limitations of Normalization
1.More joins required - Related information spread across several tables, require JOIN operations to retrieve complete data.
2.Slower read performance - Complex queries with many joins can take longer to execute, especially on very large databases.
3.Complex Queries - Developers must write advanced SQL queries to combine data from multiple tables.
4.Not Ideal for Analytics - Large reports involving millions of records become expensive
5.More tables to manage - A normalized database contains many smaller tables, increasing design and management complexity.
DENORMALIZATION
It's the process of combining tables or duplicating data to improve query performance and improve speed of data retrieval.
This approach is commonly used in:
- Data warehouses
- OLAP systems
- Business Intelligence
- Reporting databases
- Large-scale analytics
Example:
Sales_Report
- Order_ID
- Customer_Name
- Product_Name
- Order_Date
- Revenue
Advantages of Denormalization
1.Faster queries - Since related data is stored together, the database retrieves information with fewer joins.
2.Better reporting performance - Dashboards and analytical reports run more efficiently because much of the required data is already combined.
3.Simpler SQL queries - Fewer tables mean simpler queries that are easier to write and understand.
4.Better for read-heavy systems - Applications that mostly retrieve data benefit from improved response times.
Limitations of Denormalization
1.Data redundancy - The same information is stored in multiple places, increasing storage requirements.
2.Difficult updates - When duplicated information changes, every copy must be updated to maintain consistency.
3.Inconsistent data - If some copies are updated while others are not, conflicting information can appear in the database
4.Higher maintenance effort - Developers need additional checks to keep redundant data synchronized.
Normalization vs Denormalization
| Feature | Normalization | Denormalization |
|---|---|---|
| Objective | Eliminate redundancy and improve data integrity | Improve read performance by reducing joins. |
| Data redundancy | Very low because duplicate data is removed. | Higher because some duplication is intentionally introduced. |
| Storage requirements | Uses storage efficiently by avoiding repeated values | Requires more storage due to duplicated information |
| Query performance | Read queries may be slower because they often require multiple JOIN operations. | Read queries are faster because much of the required information is already stored together. |
| Update performance | Updates are easier since changes are made in one location. | Updates are more complex because duplicated data must be synchronized across multiple records. |
| Data consistency | Easier to maintain | Harder to maintain because redundant data increases inconsistencies if updates are missed |
| Complexity of queries | SQL queries are often more complex due to multiple joins. | SQL queries are usually simpler since fewer joins are required. |
| Maintenance | Easier to maintain because redundancy is minimized | Requires additional maintenance to ensure duplicated data remains consistent. |
| Best suited for | OLTP systems such as banking where accurate transactions are critical. | OLAP systems, data warehouses, dashboards, reporting platforms where fast data retrieval is the priority. |
When To Use Normalization
- Building OLTP systems where data accuracy and consistency are essential.
- Frequent inserts, updates, and deletes are expected.
- Preventing duplicate data is a priority.
- Storage efficiency is important.
- Maintaining a single source for each data item is required
Examples: Systems such as banking, hospital management, student information, payroll, inventory management and airline reservation.
When To Use Denormalization
- Building OLAP systems or data warehouses.
- Queries involve large datasets and complex aggregations.
- Read operations outnumber write operations.
- Fast reporting and dashboard performance are critical.
- Some data redundancy is acceptable in exchange for improved query speed.
Examples: Business intelligence platforms, executive dashboards, sales reporting systems, customer analytics and financial reporting.
Final Thoughts
Normalization and denormalization are both valuable database design techniques and the best choice depends on the problem you're trying to solve. Many modern systems use a combination of both normalized databases for handling day-to-day transactions and denormalized structures for faster reporting and analytics. By understanding when to apply each technique, you can design databases that balance accuracy, efficiency and performance while meeting the needs of your application.
Top comments (0)