FOREIGN KEY Constraint
Last updated: 2026-11-04
FOREIGN KEY Constraint
A foreign key enforces referential integrity: every child row must point at a row that really exists in the parent. When a parent row is deleted or updated, ON DELETE and ON UPDATE decide the child's fate — CASCADE removes it, SET NULL blanks the reference, RESTRICT refuses the operation. The constraint can also be declared DEFERRABLE, deferring the check until commit time.
| Kind | constraint |
|---|---|
| Example Language | SQL |
📝 Syntax
`FOREIGN KEY (col) REFERENCES parent_table (parent_col)`
Referential actions: `ON DELETE` / `ON UPDATE` accept `CASCADE` | `SET NULL` | `SET DEFAULT` | `RESTRICT` | `NO ACTION`
⚙️ Parameters
| col | The referencing column in the child table; its type must match the referenced column exactly (or be implicitly convertible). |
|---|---|
| referenced_table (referenced_col) | The parent table and column. The referenced column must be the parent's primary key or carry a unique constraint. |
| ON DELETE / ON UPDATE | How child rows are handled when the parent row is deleted or updated; defaults to NO ACTION. |
Returns:A foreign key has no return value. It blocks child rows from referencing a non-existent parent, and on violation PostgreSQL raises `insert or update on table "..." violates foreign key constraint "..."`.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT 'ON DELETE CASCADE -> orders left' AS step, (SELECT count(*)::TEXT FROM orders) AS result
UNION ALL SELECT 'ON DELETE SET NULL -> address.customer_id', (SELECT customer_id::TEXT FROM addresses WHERE address_id = 1)
UNION ALL SELECT 'ON DELETE RESTRICT -> delete customer 3', (SELECT pg_try('DELETE FROM customers WHERE customer_id = 3'))
UNION ALL SELECT 'FK violation -> insert order with customer_id 999', (SELECT pg_try('INSERT INTO orders VALUES (99, 999)'))
step | result
---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------
ON DELETE CASCADE -> orders left | 0
ON DELETE SET NULL -> address.customer_id | NULL
ON DELETE RESTRICT -> delete customer 3 | update or delete on table "customers" violates RESTRICT setting of foreign key constraint "contracts_customer_id_fkey" on table "contracts"
FK violation -> insert order with customer_id 999 | insert or update on table "orders" violates foreign key constraint "orders_customer_id_fkey"
(4 rows)
📌 Key Points
- Five actions: NO ACTION (default, deferrable), RESTRICT (refuse immediately), CASCADE, SET NULL and SET DEFAULT.
- RESTRICT and NO ACTION differ only in timing: RESTRICT can never be deferred, while NO ACTION can be postponed on a DEFERRABLE constraint.
- The referenced column must carry a unique constraint, otherwise the foreign key cannot be created.
- ON DELETE SET NULL requires the referencing column to be nullable; otherwise blanking it fails and rolls back.