TExcelTAD
Home › SQL › SQL CHECK

SQL CHECK

A CHECK constraint holds a condition that every row must satisfy. If an INSERT or UPDATE would make the condition false, the statement is rejected.

sql
CREATE TABLE t (
  col datatype CHECK (condition),          -- column check
  ...,
  CHECK (condition on several columns)     -- table check
);

Valid ratings only

sql · try it yourself
CREATE TABLE ratings (
  product_id INTEGER NOT NULL,
  stars      INTEGER NOT NULL CHECK (stars BETWEEN 1 AND 5)
);

INSERT INTO ratings VALUES (1, 5), (2, 3);
SELECT * FROM ratings ORDER BY product_id;

INSERT INTO ratings VALUES (3, 6);
2 rows affected.

product_id | stars
-----------+------
1          | 5
2          | 3
(2 rows)

Error: CHECK constraint failed: stars BETWEEN 1 AND 5

Rules across columns

A table-level check can compare columns of the same row. A shipment cannot arrive before it was sent:

sql · try it yourself
CREATE TABLE shipments (
  shipment_id INTEGER PRIMARY KEY,
  sent        TEXT NOT NULL,
  arrived     TEXT,
  CHECK (arrived IS NULL OR arrived >= sent)
);

INSERT INTO shipments (sent, arrived) VALUES ('2025-05-01', '2025-05-03');
INSERT INTO shipments (sent, arrived) VALUES ('2025-05-02', NULL);
SELECT * FROM shipments ORDER BY shipment_id;

INSERT INTO shipments (sent, arrived) VALUES ('2025-05-05', '2025-05-04');
1 row affected.

1 row affected.

shipment_id | sent       | arrived
------------+------------+-----------
1           | 2025-05-01 | 2025-05-03
2           | 2025-05-02 | NULL
(2 rows)

Error: CHECK constraint failed: arrived IS NULL OR arrived >= sent

Allowed values

CHECK … IN (…) limits a column to a fixed list — a lightweight alternative to an ENUM type:

sql · try it yourself
CREATE TABLE tickets (
  ticket_id INTEGER PRIMARY KEY,
  priority  TEXT NOT NULL CHECK (priority IN ('low', 'normal', 'high')),
  title     TEXT NOT NULL CHECK (length(title) >= 5)
);

INSERT INTO tickets (priority, title) VALUES ('high', 'Payment page down');
INSERT INTO tickets (priority, title) VALUES ('urgent', 'Typo in footer');
1 row affected.

Error: CHECK constraint failed: priority IN ('low', 'normal', 'high')

CHECK and NULL

A check only fails when the condition is false. If it evaluates to NULL (unknown), the row is accepted:

sql · try it yourself
CREATE TABLE discounts (percent INTEGER CHECK (percent > 0));

INSERT INTO discounts VALUES (10);
INSERT INTO discounts VALUES (NULL);   -- accepted: NULL > 0 is unknown

SELECT COUNT(*) AS rows, COUNT(percent) AS with_value FROM discounts;
1 row affected.

1 row affected.

rows | with_value
-----+-----------
2    | 1
(1 row)

Add NOT NULL if the column must have a value.

Checks also apply to UPDATE

sql · try it yourself
CREATE TABLE wallet (customer_id INTEGER PRIMARY KEY, balance REAL NOT NULL CHECK (balance >= 0));
INSERT INTO wallet VALUES (6, 20.0);

UPDATE wallet SET balance = balance - 15 WHERE customer_id = 6;
SELECT * FROM wallet;

UPDATE wallet SET balance = balance - 15 WHERE customer_id = 6;
1 row affected.

1 row affected.

customer_id | balance
------------+--------
6           | 5
(1 row)

Error: CHECK constraint failed: balance >= 0

The second withdrawal would make the balance negative, so it is refused and the balance stays at 5.

Database differences

Database CHECK support
SQLite, PostgreSQL, SQL Server, Oracle enforced
MySQL 8.0.16+ enforced
MySQL before 8.0.16 parsed but ignored

A check may only look at the current row. Rules that involve other rows or tables ("total stock per warehouse ≤ capacity") need triggers or application logic.

Test yourself

Is a row with percent = NULL accepted by CHECK (percent > 0)?

  1. Yes, because the check is not false
  2. No
  3. Only in SQLite
  4. Only if there is a DEFAULT

Which constraint would ensure end_date is not before start_date?

  1. UNIQUE (start_date, end_date)
  2. CHECK (end_date >= start_date)
  3. FOREIGN KEY (end_date)
  4. DEFAULT start_date