Concept · Designing databases

CHECK

CHECK rejects a row that breaks a condition defined in the schema, such as a discount out of range.

Before continuing: This entry is part of the constraints and keys guide. If you are new to the topic, start with CREATE TABLE.

CHECK example

SQL
CREATE TABLE coupons (
  id INTEGER PRIMARY KEY,
  discount INTEGER CHECK (discount BETWEEN 0 AND 100)
);

CREATE TABLE periods (
  start DATE,
  end DATE,
  CONSTRAINT later_end CHECK (end >= start)
);

A named constraint makes the error easier to identify and the schema easier to change. The exact syntax for adding or dropping constraints after the table is created varies between engines.

Check the result with small data

The table accepts percentages from 0 to 100 inclusive.

Self-contained test · SQLite
CREATE TABLE demo_discounts (code TEXT, percentage INTEGER CHECK(percentage BETWEEN 0 AND 100));
INSERT INTO demo_discounts VALUES ('OCT', 30);
SELECT code, percentage FROM demo_discounts;
Check result
codepercentage
OCT30

The example stores 30 because it meets the rule; 130 would be rejected. CHECK is independent of NOT NULL: you need both if the column must always have a value.

Check your understanding: Could NULL be stored here if we did not add NOT NULL?

Show answer

Yes. With NULL, the CHECK condition is not false but unknown, and CHECK only rejects the row when the condition is false. To prevent it, add NOT NULL.

How CHECK is used

A named constraint makes the error easier to identify and the schema easier to change. The exact syntax for adding or dropping constraints after the table is created varies between engines.

What changes from case to case

In mainstream engines, CHECK blocks a row when the expression is false; an expression that is unknown because of NULL may not block it. If the column must also be required, combine the rule with NOT NULL.

CHECK does not replace every relationship. To guarantee that a value exists in another table, use a FOREIGN KEY, not a subquery inside CHECK.

Common mix-ups with CHECK

Copying a complex rule into many tables

A constraint should be clear, stable and checkable against the current row. When the rule depends on processes, history or several tables, you may need a different design or transaction logic.