DEV Community

dana
dana

Posted on

Understanding Database Normalization with Examples

Database normalization is a fundamental concept in database theory and design. It's a systematic approach to organizing data in a database to reduce redundancy and improve data integrity. The process involves dividing large tables into smaller, more manageable pieces and defining relationships between them.

What is Database Normalization?

Normalization involves applying a series of rules, or "normal forms," to your database. These rules are designed to:

  • Minimize duplicate data
  • Organize data logically
  • Ensure data dependencies make sense

Why Normalize a Database?

The main goals of normalizing a database are:

  • To eliminate redundant (duplicate) data
  • To ensure data dependencies are sensible (only storing related data in a table)
  • To protect the data and make the database more scalable

The Normal Forms

There are several normal forms, each with its own set of rules. The most commonly used normal forms are:

  • First Normal Form (1NF)
  • Second Normal Form (2NF)
  • Third Normal Form (3NF)
  • Boyce-Codd Normal Form (BCNF)

First Normal Form (1NF)

A table is in 1NF if:

  • All columns contain atomic, indivisible values
  • There are no repeating groups or arrays

Example of 1NF:

| StudentID | Name           | CourseIDs       |
|-----------|----------------|-----------------|
| 1         | Alice Brown    | CS101, MATH201  |
| 2         | Bob Crown      | CS101, ENG210   |
| 3         | Charlie Davis  | MATH201, ENG210 |
Enter fullscreen mode Exit fullscreen mode

To convert it to 1NF, we would separate the CourseIDs into individual rows:

| StudentID | Name           | CourseID |
|-----------|----------------|----------|
| 1         | Alice Brown    | CS101    |
| 1         | Alice Brown    | MATH201  |
| 2         | Bob Crown      | CS101    |
| 2         | Bob Crown      | ENG210   |
| 3         | Charlie Davis  | MATH201  |
| 3         | Charlie Davis  | ENG210   |
Enter fullscreen mode Exit fullscreen mode

Second Normal Form (2NF)

A table is in 2NF if:

  • It is in 1NF
  • All non-key attributes are fully functionally dependent on the primary key

Example of 2NF:

Using the 1NF example above, we can further normalize it by separating the courses into a different table:

| StudentID | Name           |
|-----------|----------------|
| 1         | Alice Brown    |
| 2         | Bob Crown      |
| 3         | Charlie Davis  |

| CourseID | CourseName |
|----------|------------|
| CS101    | Comp Sci   |
| MATH201  | Calculus   |
| ENG210   | English    |

| StudentID | CourseID |
|-----------|----------|
| 1         | CS101    |
| 1         | MATH201  |
| 2         | CS101    |
| 2         | ENG210   |
| 3         | MATH201  |
| 3         | ENG210   |
Enter fullscreen mode Exit fullscreen mode

Third Normal Form (3NF)

A table is in 3NF if:

  • It is in 2NF
  • It has no transitive functional dependencies

Example of 3NF:

If we had a table that included a TeacherID that was dependent on the CourseID, we would separate that into its own table to satisfy 3NF:

| StudentID | Name           |
|-----------|----------------|
| 1         | Alice Brown    |
| 2         | Bob Crown      |
| 3         | Charlie Davis  |

| CourseID | CourseName | TeacherID |
|----------|------------|-----------|
| CS101    | Comp Sci   | T1        |
| MATH201  | Calculus   | T2        |
| ENG210   | English    | T3        |

| TeacherID | TeacherName |
|-----------|-------------|
| T1        | Dr. Smith   |
| T2        | Prof. Jones |
| T3        | Mr. Lee     |

| StudentID | CourseID |
|-----------|----------|
| 1         | CS101    |
| 1         | MATH201  |
| 2         | CS101    |
| 2         | ENG210   |
| 3         | MATH201  |
| 3         | ENG210   |
Enter fullscreen mode Exit fullscreen mode

Conclusion

Normalization is a critical process in database design that can greatly enhance the efficiency and integrity of your data. By following the normal forms, you can ensure that your database is free of unnecessary redundancy and is structured in a way that supports the logical relationships between data points.

Remember, normalization is a balance. Over-normalization can lead to excessive complexity and performance issues, while under-normalization can cause data redundancy and integrity problems. It's important to find the right level for your specific application and use case.

Bonus

I have another article for options when you design a database. Check it out for TOP 3 Database Design Tools
.

Top comments (0)