CHECK constraints: validate conditions on each row
Learn how `CHECK` validates row values, why NULL needs separate treatment, what cross-row rules it cannot guarantee, and how to add one safely.
CHECK constraints: validate conditions on each row
A CHECK constraint asks the database to test whether values satisfy a condition when a row is inserted or updated. It can require a positive quantity or a discount below the regular price. Application checks alone can be bypassed by another write path or race, so rules that must hold for every write belong in the database too.
What conditions can it express?
A condition is a Boolean expression calculated from the row's columns. In PostgreSQL, a false result rejects the insert or update. The example below limits quantity and price and requires any supplied discount to be nonnegative and below the price:
CREATE TABLE order_lines (
order_id bigint NOT NULL,
product_id bigint NOT NULL,
quantity integer NOT NULL CHECK (quantity > 0),
price numeric(10, 2) NOT NULL CHECK (price >= 0),
discount_price numeric(10, 2),
CHECK (discount_price IS NULL OR discount_price >= 0),
CHECK (discount_price IS NULL OR discount_price < price)
);A check written beside one column is convenient for a rule about that column. A condition comparing columns belongs in a table-level CHECK. Naming a constraint can make it easier to identify in migrations and errors.
NULL can make a condition pass
In PostgreSQL, a CHECK passes when its expression is true or NULL; false fails. Therefore CHECK (quantity > 0) alone does not reject a NULL quantity. Declare NOT NULL separately when the value is required.
Here, discount_price may be NULL to mean no discount. When supplied, it must be at least zero and below the regular price. Because price is NOT NULL, the comparison is well-defined. Keep the meaning of an optional value separate from its range rule.
What CHECK does not guarantee
PostgreSQL CHECK evaluates the row being inserted or updated. Do not use it to guarantee a rule involving other rows or another table: later changes elsewhere are not automatically rechecked and can even make a restore fail. Use purpose-built constraints such as UNIQUE for uniqueness and FOREIGN KEY for references. More complex cross-row rules need a suitable data model and DBMS-specific design.
PostgreSQL also assumes a check expression is immutable for the same row input. A condition based on current time or external state may change without the row being updated, so the database might not notice a violation. The documentation recommends immutable expressions for checks.
Add a check to an existing table safely
Define the rule and exceptions, including whether NULL is allowed, boundary values, units, and rounding.
Find existing rows that violate it and agree on remediation with the data owner. Do not hide the issue by deleting rows arbitrarily.
Confirm the PostgreSQL version and operational procedure before adding the constraint; existing violations may cause the change to fail.
Verify that valid values are accepted and invalid inserts and updates are rejected. Application validation can give a friendly early message, while the database constraint protects consistency for every write.
For example, if a stored discount equals or exceeds the regular price, first agree on what that data means. Setting it to NULL may be appropriate if it means “no discount,” but that is not a universal automatic repair.
Choosing the right rule
A value range or relationship among columns in one row: consider `CHECK`.
A value must not be missing: combine it with `NOT NULL`.
Uniqueness across rows or references to another table: use `UNIQUE` or `FOREIGN KEY`, respectively.
A condition that changes with time, external state, or other rows: do not rely on a simple `CHECK`; choose an appropriate model and database feature.
Source and scope
[PostgreSQL 18: Constraints — CHECK, NOT NULL, UNIQUE, FOREIGN KEY](https://www.postgresql.org/docs/18/ddl-constraints.html)
Documentation checked 2026-11-23. SQL is illustrative and was not executed. CHECK capabilities and migration options vary by DBMS.