Learning navigation

Code with Animation

Constraints & Defaults

Let the database enforce your rules with NOT NULL, UNIQUE, CHECK and keys, fill gaps with defaults, and avoid the CHECK and NULL trap.

SQL7 min readConcept 36 of 44

Rules the database enforces for you

A constraint is a rule attached to a table. Every row that is inserted or changed must obey it, or the statement is rejected. The main ones are NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY and CHECK.

A default is the value a column takes when an INSERT leaves it out. If you declare no default, the default is NULL.

Rules in one place

If the rule "a price is above zero" lives only in your program, any other program, script or person can break it. A constraint protects the data no matter who writes it.

A table with rules

CREATE TABLE items (
  id integer PRIMARY KEY,
  name text NOT NULL UNIQUE,
  price integer CHECK (price > 0),
  qty integer DEFAULT 1,
  created time DEFAULT CURRENT_TIMESTAMP,
  total integer GENERATED ALWAYS AS (price * qty)
);

INSERT INTO items (name, price) VALUES ('Pen', 5) works: id comes from a sequence, qty becomes 1, created gets the time of this insert, and total is worked out from price and qty. A default that is an expression, such as CURRENT_TIMESTAMP, is evaluated each time a row is inserted, not once when the table is created.

A repeated name breaks UNIQUE. A price of 0 breaks the CHECK. A generated column such as total is always computed from other columns, so you cannot write to it (you may write DEFAULT). In PostgreSQL 18 a generated column is virtual by default: it is worked out when you read it, and STORED keeps it on disk.

Break the rules on purpose

Insert a good row, then try a repeated name, a price of 0, and a NULL price. Switch to NOT NULL and CHECK and try the NULL price again.

INSERT INTO items (name, price)
VALUES ('Pen', 5);
CREATE TABLE items (
id integer PRIMARY KEY,
name text NOT NULL UNIQUE,
price integer CHECK (price > 0),
qty integer DEFAULT 1,
created time DEFAULT CURRENT_TIMESTAMP,
total integer GENERATED ALWAYS AS (price * qty)
);
items (0 rows, next id: 1)
idintegernametextpriceintegerqtyintegercreatedtimetotalinteger

Run Name and price, then try the other rows. Press Price NULL with and without NOT NULL on price.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1A column is declared `price integer CHECK (price > 0)`. Can you insert a row with `price` set to NULL?
  2. 2A column has `DEFAULT CURRENT_TIMESTAMP`. When is the default time worked out?
  3. 3`total` is declared `GENERATED ALWAYS AS (price * qty)`. What does `INSERT ... (total) VALUES (99)` do?

Remember this

  • A constraint is a rule the database enforces on every insert and update: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK.
  • A column with no declared default has NULL as its default. An expression default is evaluated at each insert.
  • A CHECK passes when its test is true or NULL, so add NOT NULL to reject NULLs.
  • A generated column is computed from other columns and cannot be written to.

Done with this concept?

Mark it complete to track your progress. No login needed.