×

sql · level 2 · unit 6 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

refuses any row that fails your test.

A price of minus 5 makes no sense, and nor does 6 stars out of 5. CHECK lets you write the rule yourself, as a test in brackets after the column.

Every row that goes in, or gets changed by UPDATE, must pass. If one fails, the statement stops with CHECK constraint failed, followed by your test.

Tests can use anything a WHERE can, like CHECK (stars >= 1 AND stars <= 5) or CHECK (size IN ('S', 'M', 'L')).

CREATE TABLE cakes (
  name TEXT NOT NULL,
  price REAL CHECK (price > 0)
);
INSERT INTO cakes VALUES ('Brownie', 1.5), ('Scone', 1.25);
SELECT * FROM cakes;

A test on a NULL counts as a pass, so pair CHECK with NOT NULL when the box must be filled.