Relationships, Constraints, and Normalisation
Connect books to authors while protecting the relationships between records.
database, sql, postgresql, relationships, constraints, normalisationSeparate facts and connect them
A book can have an author, and one author can have several books. A
relationship connects records without requiring the author's name to be
copied into every book row. authors.id is a primary key; books.author_id
is a foreign key referencing it. A non-NULL foreign key must match an
author, but multiple books may reference the same author.
SQL illustration
Use a fresh PostgreSQL session with no transaction already open. This temporary, fictional setup is also used in the next four lessons; keep the same session open while trying their examples. Intuvo itself does not run SQL.
BEGIN;
CREATE TEMP TABLE authors (
id integer PRIMARY KEY,
name text NOT NULL
);
CREATE TEMP TABLE books (
id integer PRIMARY KEY,
title text NOT NULL,
author_id integer REFERENCES authors(id),
pages integer CHECK (pages >= 0)
);
INSERT INTO authors (id, name) VALUES
(1, 'Ada'), (2, 'Bo'), (3, 'Cy');
INSERT INTO books (id, title, author_id, pages) VALUES
(1, 'Kite', 1, 120), (2, 'Oak', 1, NULL),
(3, 'Tide', 2, 200), (4, 'Fern', NULL, 120);
COMMIT;
SELECT id, title, author_id, pages FROM books ORDER BY id;
Prepared result:
| id | title | author_id | pages |
|---|---|---|---|
| 1 | Kite | 1 | 120 |
| 2 | Oak | 1 | NULL |
| 3 | Tide | 2 | 200 |
| 4 | Fern | NULL | 120 |
Kite and Oak both point to author 1, Ada. Fern has no recorded author; Cy
has no books in this dataset. A non-NULL author ID such as 99 would be
rejected unless the referenced author existed. The page check rejects a
negative value, but allows NULL: add NOT NULL if a value is mandatory.
See the reasoning
Follow the example one step at a time
Follow keys from fictional books to their authors. This is a prepared teaching demonstration, not live code execution.
Store author facts once
authors id name 1 Ada 2 Bo 3 Cy
Each author has a primary key; the name is stored in the authors table.
Why normalise?
Normalisation organises facts around their dependencies to reduce unnecessary duplication and update anomalies. If Ada's name were repeated on each book, changing only one copy would leave inconsistent names. Keeping the name once under its author key avoids that particular problem.
This is an intuition, not a complete proof of a normal form. Design depends
on the actual rules: a real book may have multiple authors, which could
require a separate association table. Primary keys imply uniqueness and
non-null values; NOT NULL, UNIQUE, CHECK, and foreign keys enforce
different rules. Constraints do not prove that the facts entered are true.
As a starting guide, first normal form calls for individual column values rather than repeating groups; second normal form also addresses attributes depending on only part of a composite candidate key. Third normal form addresses certain dependencies through non-key attributes. For example, an author's name depends on the author ID, not on the book ID alone as an independent fact. Exact normal-form analysis requires identifying candidate keys and functional dependencies. Separating tables can require joins, so measure trade-offs rather than assuming any design is universally best.
Reflect on the concept
Four fixed questions to reinforce the reasoning. Answers are not saved or scored.
