ποΈ Normalization is the process of organizing data in a relational database to reduce redundancy, eliminate data anomalies, and improve data integrity. It divides data into multiple related tables using keys and relationships so that each piece of information is stored only once whenever possible.
π What is Normalization?
In an unnormalized database, the same information may appear in multiple places, leading to duplicate data and inconsistencies. Normalization applies a series of rules called Normal Forms (NF) to structure data efficiently while maintaining relationships between tables.
Information
π― Why Use Normalization?
A normalized database is easier to maintain and helps ensure the accuracy of stored data.
- π Eliminate duplicate data.
- π Improve data consistency.
- π Prevent update, insert, and delete anomalies.
- π Reduce storage requirements.
- π Improve database maintainability.
- π Enforce relationships using keys.
π Problems with an Unnormalized Table
Consider a table that stores customer and order information together.
| OrderID | CustomerName | CustomerPhone | Product | Quantity |
|---|---|---|---|---|
| 101 | Alice | 9876543210 | Laptop | 1 |
| 102 | Alice | 9876543210 | Mouse | 2 |
| 103 | Bob | 9123456789 | Keyboard | 1 |
Alice's contact information is stored multiple times. If her phone number changes, every matching row must be updated.
β οΈ Data Anomalies
| Anomaly | Description |
|---|---|
| Update Anomaly | Updating the same information in multiple rows can lead to inconsistent data. |
| Insert Anomaly | Some data cannot be inserted without inserting unrelated data. |
| Delete Anomaly | Deleting a row may unintentionally remove valuable information. |
Warning
π The Normal Forms
| Normal Form | Main Goal |
|---|---|
| 1NF | Remove repeating groups and ensure atomic values. |
| 2NF | Remove partial dependencies. |
| 3NF | Remove transitive dependencies. |
| BCNF | Strengthen dependency rules beyond 3NF. |
| 4NF | Remove multi-valued dependencies. |
| 5NF | Eliminate join dependencies. |
π₯ First Normal Form (1NF)
A table is in 1NF if:
- β Each column contains only atomic (indivisible) values.
- β There are no repeating groups or arrays.
- β Every row is uniquely identifiable.
β Before 1NF
| Student | Courses |
|---|---|
| Alice | Math, Science |
β After 1NF
| Student | Course |
|---|---|
| Alice | Math |
| Alice | Science |
π₯ Second Normal Form (2NF)
A table is in 2NF if:
- β It already satisfies 1NF.
- β Every non-key column depends on the entire primary key.
- β Partial dependencies are removed.
This mainly applies to tables with composite primary keys.
π₯ Third Normal Form (3NF)
A table is in 3NF if:
- β It satisfies 2NF.
- β Non-key columns depend only on the primary key.
- β Transitive dependencies are removed.
Example
Instead of storing department information with every employee, create a separate department table and reference it using a foreign key.
| Employees | |
|---|---|
| EmployeeID | DepartmentID |
| Departments | |
|---|---|
| DepartmentID | DepartmentName |
π Boyce-Codd Normal Form (BCNF)
BCNF is a stricter version of 3NF. Every functional dependency must have a candidate key as its determinant.
Important
ποΈ Example of a Normalized Database
| Customers | Orders | Products |
|---|---|---|
| CustomerID | OrderID | ProductID |
| Name | CustomerID | ProductName |
| Phone | OrderDate | Price |
Relationships are maintained using primary keys and foreign keys instead of repeating customer and product details in every order record.
βοΈ Normalization vs Denormalization
| Normalization | Denormalization |
|---|---|
| Reduces redundancy. | Introduces controlled redundancy. |
| Requires more joins. | Requires fewer joins. |
| Improves consistency. | Improves read performance. |
| Better for OLTP systems. | Often used in reporting and data warehouses. |
πΌ Real-World Applications
- π¦ Banking systems.
- π E-commerce databases.
- π₯ Hospital management systems.
- π Student information systems.
- π¦ Inventory management.
- π’ Enterprise resource planning (ERP) systems.
ποΈ Database Compatibility
Normalization is a database design principle, not a SQL command. It applies to all relational database management systems.
| Database System | Supports Normalized Design |
|---|---|
| MySQL | β Yes |
| PostgreSQL | β Yes |
| SQL Server | β Yes |
| Oracle | β Yes |
| SQLite | β Yes |
β οΈ Common Mistakes
- β Over-normalizing small databases unnecessarily.
- β Ignoring primary and foreign key relationships.
- β Storing duplicate information across multiple tables.
- β Confusing normalization with performance optimization.
Warning
β οΈ Best Practices
Best Practice
π Key Points to Remember
- π Normalization organizes data efficiently.
- π It reduces redundancy and improves consistency.
- π It prevents update, insert, and delete anomalies.
- π The most commonly used normal forms are 1NF, 2NF, and 3NF.
- π PARTITION BY, joins, and foreign keys work effectively with normalized schemas.
- π Most OLTP databases are designed up to 3NF or BCNF.