114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
66% Percent Complete
By Kevin McAleer, 6 Minutes
In this lesson, we’ll discuss database normalization, a process that organizes data to reduce redundancy and improve data integrity. You’ll learn about the first three normal forms (1NF, 2NF, and 3NF), which are essential for creating efficient and well-structured databases.
Normalization is a technique used in database design to structure tables and their relationships, minimizing duplicate data and ensuring data integrity. The goal is to create a database that:
Normalization organizes data into a series of tables and establishes relationships based on defined rules, known as normal forms.
A table is in First Normal Form (1NF) if:
Consider the following table that stores a customer’s order and items:
| order_id | customer_name | items | |----------|---------------|----------------| | 1 | Alice | Laptop, Tablet | | 2 | Bob | Smartphone |
This table is not in 1NF because the items column contains multiple values (e.g., “Laptop, Tablet”). To bring it into 1NF, we need to split the items so that each row contains only one item per order:
items
| order_id | customer_name | item | |----------|---------------|------------| | 1 | Alice | Laptop | | 1 | Alice | Tablet | | 2 | Bob | Smartphone |
Now, each column contains only atomic values, meeting the criteria for 1NF.
A table is in Second Normal Form (2NF) if it:
Consider an orders table that includes customer information:
orders
| order_id | customer_id | customer_name | item | |----------|-------------|---------------|------------| | 1 | 101 | Alice | Laptop | | 1 | 101 | Alice | Tablet | | 2 | 102 | Bob | Smartphone |
This table is not in 2NF because customer_name depends only on customer_id, which is part of the primary key (order_id, customer_id). To normalize it, we can separate customer information into its own table.
customer_name
customer_id
order_id, customer_id
orders:
| order_id | customer_id | item | |----------|-------------|------------| | 1 | 101 | Laptop | | 1 | 101 | Tablet | | 2 | 102 | Smartphone |
customers:
| customer_id | customer_name | |-------------|---------------| | 101 | Alice | | 102 | Bob |
Now, the orders table only contains data related to the order, and the customers table holds information specific to customers. This structure meets 2NF requirements.
customers
A table is in Third Normal Form (3NF) if it:
Consider an orders table with additional customer location information:
| order_id | customer_id | customer_name | city | item | |----------|-------------|---------------|----------|------------| | 1 | 101 | Alice | New York | Laptop | | 2 | 102 | Bob | Chicago | Smartphone |
This table is not in 3NF because city depends on customer_id through customer_name, rather than directly on the primary key (order_id). To bring it into 3NF, we should split city into the customers table.
city
order_id
| order_id | customer_id | item | |----------|-------------|------------| | 1 | 101 | Laptop | | 2 | 102 | Smartphone |
| customer_id | customer_name | city | |-------------|---------------|----------| | 101 | Alice | New York | | 102 | Bob | Chicago |
Now, each non-key column depends only on the primary key in each table, meeting 3NF requirements.
Normalization provides several advantages:
Convert to 1NF: Consider the following table and make it 1NF compliant.
Solution:
Convert to 2NF: Consider the following table and make it 2NF compliant.
Solution: Separate customer information into a new table:
Convert to 3NF: Consider the following table and make it 3NF compliant.
Solution: Move city to the customers table:
By following normalization principles, you can create efficient, scalable, and consistent databases. In the next lesson, we’ll go further into Boyce-Codd Normal Form (BCNF) for advanced normalization techniques.
< Previous Next >