Design tables with normalization

PostgreSQL 19.0

Store each kind of fact in the table it belongs to. Join related tables when you need the full record.

On this page

Read a normalized enrollment list

Run this in learning_db after Setup. Each enrollment stores one student ID and one course ID. Joining with students and courses shows the names and titles. Aye can appear twice in the report while their name is stored once in students.

SELECT s.name AS student, c.title AS course, e.enrolled_on
FROM enrollments AS e
JOIN students AS s ON s.id = e.student_id
JOIN courses AS c ON c.id = e.course_id
ORDER BY s.id, c.id;

Follow what each field depends on

For first normal form, store each registration in its own row instead of adding course_1 and course_2 fields to the student record. For second normal form, enrolled_on depends on the whole enrollment key (student_id, course_id). The title depends only on course_id, so it belongs in courses. For third normal form, if courses have a department_id, store department names in departments because department_id determines the department name.

CREATE TABLE departments (
  id integer PRIMARY KEY,
  name text NOT NULL
);

CREATE TABLE courses (
  id integer PRIMARY KEY,
  title text NOT NULL,
  department_id integer REFERENCES departments(id)
);

CREATE TABLE enrollments (
  student_id integer REFERENCES students(id),
  course_id integer REFERENCES courses(id),
  enrolled_on date NOT NULL,
  PRIMARY KEY (student_id, course_id)
);

Official references