CodeGym /Courses /SQL SELF /Second Normal Form (2NF) Principles

Second Normal Form (2NF) Principles

SQL SELF
Level 25 , Lesson 2
Available

A table is considered to be in Second Normal Form if:

  1. It's already in First Normal Form (1NF).
  2. Every non-key column depends on the whole primary key, not just a part of it.

If the primary key is made up of several fields (a composite key), then none of the non-key attributes (columns) should depend on just one part of that key. In other words, 2NF gets rid of partial dependencies.

Example of 2NF Violation

Let's say we have a student_courses table that stores info about students, their courses, and instructors:

student_id course_id course_name instructor_name
1 101 Mathematics Lin
1 102 Literature Song
2 101 Mathematics Lin
  • student_id and course_id together make up the composite primary key.
  • But check out the course_name and instructor_name columns. They depend only on course_id, not on the whole pair (student_id, course_id).

There it is, a partial dependency! course_name and instructor_name depend only on part of the composite key (course_id). That's a 2NF violation.

Bringing the Table to 2NF

Our goal is to get rid of the partial dependency by splitting the table in two. This will cut down on redundancy and make our data more consistent.

Let's move the course info into a separate courses table:

course_id course_name instructor_name
101 Mathematics Lin
102 Literature Song

And the main table now looks like this:

student_id course_id
1 101
1 102
2 101

Now every column depends on the whole primary key. We've split the data so everything is logically connected, and the 2NF violation is gone.

The Magic of Killing Redundancy

Look at the table before normalization. In the instructor_name column, the name "Lin" repeats. Imagine how many repeats you'd get in a real database with thousands of rows! By splitting the tables, we killed redundancy and lowered the chance of mistakes, like typos ("Lin" vs "Ling").

Real-Life Example

Imagine you're keeping track of product orders. You have this order_items table, where order_id and item_id make up the primary key:

order_id item_id item_name price
1 101 Laptop 50000
1 102 Mouse 1000
2 101 Laptop 50000

See how the prices and product names repeat? That's a sign of a 2NF violation, because item_name and price depend only on item_id.

To bring the table to 2NF, let's make an items table:

item_id item_name price
101 Laptop 50000
102 Mouse 1000

And change the order_items table to only keep order and item IDs:

order_id item_id
1 101
1 102
2 101

Now the data is as clean as code after a review—no redundancy at all.

Hands-On Task: Try It Yourself!

Suppose you have an employee_projects table with info about employees, their projects, and project managers:

employee_id project_id project_name manager_name
1 201 CRM Upgrade Lin
2 202 Website Revamp Ming
1 202 Website Revamp Ming

Try to:

  1. Find the dependencies that break 2NF rules.
  2. Split the table in two to fix the violation.

Why Bother with 2NF?

Why even care about Second Normal Form (2NF)? Simple: so your data doesn't get duplicated and messy. When you get rid of partial dependencies, your tables get cleaner—no more repeating info, like the same instructor name in every row. That saves space and keeps things consistent: update a name in one place, and it's up to date everywhere.

Plus, writing queries for a database like this is easier: when the structure makes sense and data isn't scattered all over, filtering and grouping work faster and more reliably. Sure, you pay for it with a few more SQL queries using JOINs, since there are more tables. But hey, better one JOIN than a hundred rows with the same last names. Most of the time, normalization is totally worth it.

Integrating with Real Projects

Knowing 2NF will help you out:

  • When designing a database: helping you dodge chaos in your tables.
  • At job interviews: people often ask you to explain or normalize tables.
  • On the job: when you're trusted to optimize an existing database by breaking up "monolithic" tables into normalized ones.

Second Normal Form (2NF) helps you get rid of partial dependencies in a table where the primary key is composite. We split tables into logical blocks so every column depends only on the whole key, not just part of it. This makes your database better, kills redundancy, and makes it more flexible. Ready for Third Normal Form? Let's keep going!

2
Task
SQL SELF, level 25, lesson 2
Locked
Bringing a table to 2NF
Bringing a table to 2NF
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION