Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Constraints  ›  Capstone Project

CHECK

CHECK 15 minA CHECK constraint attaches a condition that every inserted or updated row must not make false, turning business rules into rules the database enforces.

CHECK

A bug in the checkout code sends quantity = -3 to the database, and the shop's stock count goes up when a customer "buys" something. The code will be fixed, but the next bug is already waiting. A CHECK constraint writes the business rule into the table itself, where every writer has to obey it.

The rule

CHECK states a rule that no row may break.

CREATE TABLE products (
    product_id INTEGER PRIMARY KEY,
    name       VARCHAR(100) NOT NULL,
    price      DECIMAL(10,2) NOT NULL CHECK (price >= 0),
    discount   DECIMAL(10,2) DEFAULT 0,
    CONSTRAINT discount_below_price CHECK (discount <= price)
);

A CHECK holds a condition, just like a WHERE clause. On every INSERT and UPDATE, the database evaluates it for the new row; if the condition is false, the change is rejected.

  • A column CHECK sits next to one column: CHECK (price >= 0).
  • A table CHECK can compare several columns of the same row: CHECK (discount <= price). Giving it a name with CONSTRAINT … makes error messages readable and lets you drop it later.

Common uses in the shop:

quantity INTEGER NOT NULL CHECK (quantity > 0)
status   VARCHAR(20) NOT NULL CHECK (status IN ('pending', 'shipped', 'cancelled'))
rating   INTEGER CHECK (rating BETWEEN 1 AND 5)

The one surprise is NULL. A CHECK rejects a row only when the condition is false. If rating is NULL, rating BETWEEN 1 AND 5 is unknown, not false, so the row is accepted. When a value is required as well as valid, add NOT NULL.

Two practical limits: a CHECK sees only the row being written — it cannot look at other rows or other tables (that is a foreign key's job). And MySQL versions before 8.0.16 parsed CHECK but silently ignored it, so on legacy systems confirm it is enforced.

The shortcut

Take the business rule in plain words and rewrite it as a WHERE condition that every good row would pass. That condition is your CHECK.

Guarding order quantitiesStep 1 of 4
  1. Business rule

    "You cannot order zero or fewer items."

Drills

Answer fast — trust the rule.

quick drill

quantity INTEGER CHECK (quantity > 0). What happens on an insert with quantity 0?

quick drill

rating INTEGER CHECK (rating BETWEEN 1 AND 5) with no NOT NULL. Is a row with rating NULL accepted?

quick drill

Which CHECK limits status to the three allowed values?

quick drill

Can CHECK (customer_id IN (SELECT customer_id FROM customers)) enforce that an order's customer exists?

Remember

  • CHECK states a rule that no row may break.
  • Rewrite the business rule as a WHERE condition good rows pass.
  • NULL slips past CHECK (unknown is not false) — pair it with NOT NULL when the value is required.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTurn rules into constraints

Turn these shop rules into constraints and say which kind of constraint each needs: (1) a product's price cannot be negative; (2) an order's status must be pending, shipped or cancelled; (3) an order's customer must exist; (4) every product must have a name. Explain why rule 3 cannot be a CHECK.