CHECK Constraint

Last updated: 2026-11-04

CHECK Constraint

A CHECK constraint requires the values in a column or group of columns to satisfy a given condition, and is the usual way to push business rules down into the database. It is evaluated per row and cannot query across rows or other tables, so it suits row-level rules such as 'amount must be positive' but not aggregate rules such as 'the total must not exceed the budget'.

Kindconstraint
Example LanguageSQL

📝 Syntax

Column level: `column_name type CHECK (expression)`
Table level: `CONSTRAINT constraint_name CHECK (expression)`
Multi-column: `CHECK (col_a > 0 AND col_b < col_a)`

⚙️ Parameters

expressionA boolean expression evaluated per row; the row is rejected when it yields false or NULL. It must be immutable and cannot reference other tables.

Returns:A CHECK constraint has no return value. On violation PostgreSQL raises `new row for relation "..." violates check 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 'insert price = 19.99' AS step, 'OK' AS result
UNION ALL SELECT 'insert price = -1.00', (SELECT pg_try('INSERT INTO products VALUES (3,''Broken'',-1.00)'))
UNION ALL SELECT 'rows stored', (SELECT count(*)::TEXT FROM products)
 step                 | result
----------------------+-------------------------------------------------------------------------------------
 insert price = 19.99 | OK
 insert price = -1.00 | new row for relation "products" violates check constraint "products_price_positive"
 rows stored          | 2
(3 rows)

📌 Key Points