Sub topic 4
Constraints That Keep the Data Honest
Assume you know basic CREATE TABLE, INSERT, and SELECT, and you have PostgreSQL 12 or newer. This chapter focuses on constraints: rules that PostgreSQL checks before it stores a row.
Without constraints, an application could insert a book with no title, a negative page count, or the same ISBN twice. Constraints move these checks into the database, where every client must follow them.
Required values with `NOT NULL`
NOT NULL means a column must contain a value. Use it for facts that every row needs.
CREATE TABLE authors (
author_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE books (
book_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title text NOT NULL,
isbn13 text,
published_year smallint,
page_count integer
);An empty string is still a value, so name = '' satisfies NOT NULL. If an empty name is invalid, add a CHECK constraint as well.
Unique values with `UNIQUE`
An ISBN identifies a particular edition. It should not appear twice.
ALTER TABLE books
ADD CONSTRAINT books_isbn13_unique UNIQUE (isbn13);PostgreSQL permits multiple NULL values in a UNIQUE column. This is useful when an ISBN is unknown, because “unknown” does not mean “the same ISBN.” If every book must have an ISBN, combine UNIQUE with NOT NULL.
Valid ranges with `CHECK`
A CHECK constraint must be true for each stored row. Give constraints names so error messages and later maintenance are clearer.
ALTER TABLE books
ADD CONSTRAINT books_isbn13_format
CHECK (isbn13 IS NULL OR isbn13 ~ '^[0-9]{13}$'),
ADD CONSTRAINT books_published_year_valid
CHECK (published_year IS NULL OR published_year BETWEEN 1400 AND 2100),
ADD CONSTRAINT books_page_count_positive
CHECK (page_count IS NULL OR page_count > 0);The OR ... IS NULL parts allow optional columns to remain unknown. A CHECK expression that evaluates to NULL does not fail. Add NOT NULL when unknown values are not acceptable.
Combining constraints
Constraints work together. A column can be required, unique, and restricted to a format.
CREATE TABLE editions (
edition_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
isbn13 text NOT NULL UNIQUE,
page_count integer NOT NULL CHECK (page_count > 0),
publication_year smallint
CHECK (publication_year BETWEEN 1400 AND 2100)
);Here, PostgreSQL rejects a missing ISBN, duplicate ISBN, missing page count, non-positive page count, and publication year outside the chosen range.
How a failed insert is handled
INSERT INTO books (title, page_count)
VALUES ('A Valid Book', 0);This statement fails because page_count > 0 is false. PostgreSQL stores no partial version of the row. The named constraint identifies the rule that rejected it.
flowchart TD
A[Insert or update row] --> B{Constraints pass?}
B -- Yes --> C[Store the row]
B -- No --> D[Reject the statement]
D --> E[Return the constraint name]The database does not decide whether a title is literary or whether a year is historically plausible. It enforces rules that can be expressed precisely.
Exercise
Why might a column need both NOT NULL and UNIQUE?
Show answer
NOT NULL prevents the value from being missing, while UNIQUE prevents two rows from containing the same value.