SQL / MySQL Advanced Relational Design · 9 min read

Database Normalization: 1NF, 2NF, and 3NF

Lesson 15 of 33 in the SQL / MySQL learning path.

How do I split repeating course lists out of a user row?

A users row that stuffs several course codes into one column breaks as soon as a course title changes. Normalization gives each fact its own table. First normal form puts one value in each cell, so a course code is its own enrollment row. Second normal form keeps the course title with the course, because that title depends on the course alone. Third normal form keeps a student name dependent on the user id and a title dependent on the course id.

The script builds that model: users, courses, and enrollments. Ada's three courses become three enrollment rows joined to three titles. Renaming a course updates one row in courses. Dropping one registration deletes one enrollment. The pitfalls describe the comma-separated column and the anomalies it causes.

Syntax & Parameters

Syntax
CREATE TABLE users (id INT UNSIGNED PRIMARY KEY, email VARCHAR(254) UNIQUE);
CREATE TABLE courses (id INT UNSIGNED PRIMARY KEY, code VARCHAR(16) UNIQUE);
CREATE TABLE enrollments (
  id INT UNSIGNED PRIMARY KEY,
  user_id INT UNSIGNED NOT NULL,
  course_id INT UNSIGNED NOT NULL,
  FOREIGN KEY (user_id) REFERENCES users (id),
  FOREIGN KEY (course_id) REFERENCES courses (id)
);

Example

schema.sql
CREATE DATABASE IF NOT EXISTS school CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE school;
DROP TABLE IF EXISTS enrollments;
DROP TABLE IF EXISTS courses;
DROP TABLE IF EXISTS users;
CREATE TABLE users (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(120) NOT NULL,
  email VARCHAR(254) NOT NULL,
  password_hash VARCHAR(255) NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_users_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE courses (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  code VARCHAR(16) NOT NULL,
  title VARCHAR(160) NOT NULL,
  seats SMALLINT UNSIGNED NOT NULL DEFAULT 30,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_courses_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE enrollments (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL,
  course_id INT UNSIGNED NOT NULL,
  enrolled_on DATE NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_enrollments_user_course (user_id, course_id),
  CONSTRAINT fk_enrollments_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
  CONSTRAINT fk_enrollments_course FOREIGN KEY (course_id) REFERENCES courses (id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO users (name, email, password_hash, created_at) VALUES
('Ada Lovelace', 'ada@school.test', 'sample-hash-not-a-secret', '2026-01-15 09:00:00'),
('Grace Hopper', 'grace@school.test', 'sample-hash-not-a-secret', '2026-02-11 09:30:00');
INSERT INTO courses (code, title, seats) VALUES
('CS101', 'Intro to Databases', 30),
('CS220', 'Relational Design', 24),
('CS310', 'Query Tuning', 18);
INSERT INTO enrollments (user_id, course_id, enrolled_on) VALUES
(1, 1, '2026-01-20'),
(1, 2, '2026-01-21'),
(1, 3, '2026-01-22'),
(2, 1, '2026-01-20');
SELECT u.name, u.email, c.code, c.title
FROM users AS u
INNER JOIN enrollments AS e ON e.user_id = u.id
INNER JOIN courses AS c ON c.id = e.course_id
ORDER BY u.name, c.code;

Expected Output

name         | email             | code  | title
Ada Lovelace | ada@school.test   | CS101 | Intro to Databases
Ada Lovelace | ada@school.test   | CS220 | Relational Design
Ada Lovelace | ada@school.test   | CS310 | Query Tuning
Grace Hopper | grace@school.test | CS101 | Intro to Databases
4 rows in set

Line-by-Line Walkthrough

  1. The sample is the third-normal-form registrar: users, courses, and enrollments, with integer keys and created_at on each table.
  2. First normal form: each course code is its own enrollment row. Ada is not stored as the text CS101,CS220,CS310.
  3. A repeating group of columns such as course_1, course_2, and course_3 would also break first normal form. The enrollment table replaces that group.
  4. Second normal form: the grade or the enrolled date, when you add them, depends on the whole pair of user and course. The course title depends only on the course, so it lives in courses.
  5. If title were copied onto every enrollment, a composite key of user_id plus course_id would still leave title dependent on only part of that key.
  6. Third normal form: name and email depend on the user id. Title and seats depend on the course id. Nothing in users depends on another non-key column such as a track advisor.
  7. A track advisor stored beside a track code on users would depend on the track, which depends on the user. That transitive dependency belongs in a tracks table. This sample does not carry that extra column.
  8. The inserts give Ada three enrollment rows and Grace one. The join prints four lines, which is the class list a comma-separated cell was hiding.
  9. Renaming Intro to Databases is one UPDATE of courses WHERE id = 1. Every roster that joins to that row shows the new title.
  10. Deleting Ada enrollment in CS310 removes one registration and leaves the course, and leaves her other courses, because the facts are separate rows.

Common Pitfalls & Security

The denormalized shortcut is a users-style row with a single course_list column holding CS101,CS220,CS310. It looks like one student record and it creates update anomalies. Changing the title of CS101 means finding every comma-separated cell that contains that token, and any cell you miss keeps the old title. You cannot add CS410 until some student lists it, if the course has no table of its own. Deleting the last student who listed CS310 deletes the only copy of that course. Those are the update, insert, and delete anomalies in plain language.

  • A column course_list VARCHAR(255) with the value CS101,CS220,CS310 violates first normal form. One cell holds a list, and a foreign key cannot point at a fragment of that string.
  • Columns named course_1, course_2, and course_3 are the same repeating group with a fixed width. A fourth course has nowhere to go.
  • Copying course title onto every enrollment row means a rename must update many rows. One missed row still prints the old title on a transcript.
  • A course that exists only inside a student list cannot be offered before the first enrollment. The courses table can hold CS410 with zero students.
  • Deleting the last enrollment must not delete the course. ON DELETE RESTRICT on the course key, and a separate courses row, keep the catalog.
  • An UPDATE or DELETE without WHERE is a catastrophe on any of these tables. Normalization does not protect you from a write that names every row. Update one course with WHERE id = 1.

Best Practices & Modern Standards

Store one fact in one place. Users hold the student identity, courses hold the class identity, and enrollments hold the relationship and the facts that depend on both, such as the enrolled date. Declare foreign keys so the relationship cannot dangle. When a new attribute depends on something other than the row key, give it a table.

Stop at third normal form for this registrar. Further splits are worth it when a measured workload says a report is easier with a controlled copy, and that copy should be rebuilt from these tables rather than edited by hand. The join lesson is how you assemble the class list the comma-separated column was trying to display.

Real-World Use

A school enrollment schema is the normalized model of students, courses, and enrollments. The student account is a users row, the class is a courses row, and the registration is an enrollments row. Ada can take three courses without a comma-separated list, the title of CS101 is stored once, and a new course can exist before anyone enrolls. Staff reports join the three tables instead of splitting a string in application code.

Frequently Asked Questions

What is wrong with storing CS101,CS220,CS310 in one column?

The cell holds three course codes, so it is not first normal form. You cannot foreign-key one of those tokens to courses, and a title change means editing every student who listed that code. One missed row keeps the old title.

What is an update anomaly in this school?

If the course title is copied onto every enrollment, renaming Intro to Databases requires many updates. Any row you skip still shows the old name on a transcript. In the normalized model you update the single courses row.

What are the insert and delete anomalies?

Insert anomaly: a new course has nowhere to live if course facts exist only inside a student list. Delete anomaly: removing the last student who mentioned CS310 removes the only stored copy of that course. Separate tables keep the course when the last enrollment goes away.

Does third normal form forbid reporting joins?

No. The join is how you rebuild the class list. The sample query prints the three courses for Ada as three rows. The storage stays normalized so the title and the student are each stored once.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…