Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 10 / Topic 3 of 9

Constraints — named, and tested by breaking them

A constraint is a business rule the database enforces so that bad data is impossible, not merely discouraged. The full set, on the loans table — note the names:

CREATE TABLE loans (
  loan_id     integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  book_id     integer NOT NULL REFERENCES books (book_id),
  member_name text    NOT NULL,
  loaned_on   date    NOT NULL DEFAULT current_date,
  due_on      date    NOT NULL,
  returned_on date,
  CONSTRAINT loans_due_after_loan  CHECK (due_on > loaned_on),
  CONSTRAINT loans_return_sensible CHECK (returned_on IS NULL OR returned_on >= loaned_on)
);
CREATE TABLE

CONSTRAINT name CHECK (...) — naming matters because the name is what errors print, what colleagues read, and what you later DROP CONSTRAINT by. (Unnamed ones get generated names like loans_check1 — accurate and useless.)

Now do what professionals do after writing constraints: try to break every one. A violation per rule, each failing loudly —

INSERT INTO loans (book_id, member_name, due_on)
VALUES (1, 'Asha', current_date + 14);
INSERT INTO loans (book_id, member_name, due_on)
VALUES (999, 'Ravi', current_date + 14);
ERROR:  insert or update on table "loans" violates foreign key constraint "loans_book_id_fkey"
DETAIL:  Key (book_id)=(1) is not present in table "books".
ERROR:  insert or update on table "loans" violates foreign key constraint "loans_book_id_fkey"
DETAIL:  Key (book_id)=(999) is not present in table "books".

Wait — both failed? The first insert needs books to have a book 1, and section 2's failed manual insert never created one. Add a book, then watch the honest one succeed and the FK violation fail:

INSERT INTO books (title, isbn) VALUES ('The Postgres Book', '978-0000000001')
RETURNING book_id;
INSERT INTO loans (book_id, member_name, due_on)
VALUES (1, 'Asha', current_date + 14);
INSERT INTO loans (book_id, member_name, due_on)
VALUES (999, 'Ravi', current_date + 14);
INSERT INTO loans (book_id, member_name, due_on, loaned_on)
VALUES (1, 'Meera', DATE '2026-01-01', DATE '2026-01-02');
 book_id 
---------
       1
(1 row)

INSERT 0 1
INSERT 0 1
ERROR:  insert or update on table "loans" violates foreign key constraint "loans_book_id_fkey"
DETAIL:  Key (book_id)=(999) is not present in table "books".
ERROR:  new row for relation "loans" violates check constraint "loans_due_after_loan"
DETAIL:  Failing row contains (5, 1, Meera, 2026-01-02, 2026-01-01, null).

Three outcomes, one lesson each: valid data flows; a reference to a non-existent book is impossible; a due date before the loan is impossible. A CHECK that never fired in testing is a rule you haven't actually proven exists.

🧪 Quick quiz — answer from memory, then check.

  1. Why name constraints?
  2. What's the professional step right after creating constraints?
Show the answers
  1. The name appears in every violation error and is the handle for ALTER/DROP — loans_due_after_loan beats loans_check1.
  2. Write one violating statement per rule and watch each fail — untested constraints are unproven rules.

Done here? The practice set for Part 10 — 6 exercises with solutions — is on the part page.

Search the guide