TExcelTAD
Home › SQL › SQL Constraints

SQL Constraints

Constraints are rules the database enforces on every insert and update. They keep bad data out — no matter which application, script or person writes to the table.

Constraint Guarantees
NOT NULL the column always has a value
UNIQUE no two rows share the same value(s)
PRIMARY KEY unique + not null; identifies each row
FOREIGN KEY the value exists in another table
CHECK the value satisfies a condition
DEFAULT a value is filled in when none is given (strictly a default, not a rule)

Column and table constraints

Constraints can be written next to a column (column constraint) or on their own line (table constraint) — needed when the rule involves several columns:

sql · try it yourself
CREATE TABLE shifts (
  shift_id    INTEGER PRIMARY KEY,                         -- column constraint
  employee_id INTEGER NOT NULL REFERENCES employees(employee_id),
  day         TEXT NOT NULL,
  start_hour  INTEGER NOT NULL CHECK (start_hour BETWEEN 0 AND 23),
  end_hour    INTEGER NOT NULL,
  CHECK (end_hour > start_hour),                           -- table constraint
  UNIQUE (employee_id, day)                                -- one shift per day
);

INSERT INTO shifts (employee_id, day, start_hour, end_hour) VALUES (7, '2025-10-20', 8, 16);
INSERT INTO shifts (employee_id, day, start_hour, end_hour) VALUES (8, '2025-10-20', 12, 20);

SELECT * FROM shifts ORDER BY shift_id;
1 row affected.

1 row affected.

shift_id | employee_id | day        | start_hour | end_hour
---------+-------------+------------+------------+---------
1        | 7           | 2025-10-20 | 8          | 16
2        | 8           | 2025-10-20 | 12         | 20
(2 rows)

Violations are rejected

The last insert below breaks the UNIQUE (employee_id, day) rule — employee 7 already has a shift that day — so it is rejected with an error:

sql · try it yourself
CREATE TABLE shifts (
  shift_id    INTEGER PRIMARY KEY,
  employee_id INTEGER NOT NULL,
  day         TEXT NOT NULL,
  start_hour  INTEGER NOT NULL CHECK (start_hour BETWEEN 0 AND 23),
  end_hour    INTEGER NOT NULL,
  CHECK (end_hour > start_hour),
  UNIQUE (employee_id, day)
);
INSERT INTO shifts (employee_id, day, start_hour, end_hour) VALUES (7, '2025-10-20', 8, 16);

INSERT INTO shifts (employee_id, day, start_hour, end_hour) VALUES (7, '2025-10-20', 17, 20);
1 row affected.

Error: UNIQUE constraint failed: shifts.employee_id, shifts.day

Change the last line to try the other rules: an end_hour of 6 breaks the table CHECK, a start_hour of 25 breaks the column CHECK, and leaving out day breaks NOT NULL.

Naming constraints

Give a constraint a name with CONSTRAINT name. The name appears in error messages and lets other databases drop the constraint later:

sql · try it yourself
CREATE TABLE prices (
  product_id INTEGER,
  price      REAL,
  CONSTRAINT price_positive CHECK (price > 0)
);

INSERT INTO prices VALUES (1, -3);
Error: CHECK constraint failed: price_positive

Inspecting constraints

In SQLite, constraints are part of the table's CREATE TABLE text, which you can read with SELECT sql FROM sqlite_schema WHERE name = 'products'. Pragmas list some of them in table form — pragma_table_info shows NOT NULL, defaults and the primary key, and pragma_foreign_key_list shows foreign keys:

sql · try it yourself
SELECT "from" AS column_name, "table" AS refers_to, "to" AS refers_to_column
FROM pragma_foreign_key_list('products')
ORDER BY column_name;
column_name | refers_to  | refers_to_column
------------+------------+-----------------
category_id | categories | category_id
supplier_id | suppliers  | supplier_id
(2 rows)

Why use constraints?

  • One source of truth. Rules in the database apply to every program that writes data.
  • Earlier errors. A bad value is rejected immediately, not discovered months later in a report.
  • Better performance. PRIMARY KEY and UNIQUE create indexes; the optimizer can also rely on NOT NULL.

Note: SQLite does not enforce foreign keys unless you turn them on with PRAGMA foreign_keys = ON; (see the FOREIGN KEY lesson). All other constraints are always enforced.

Test yourself

Which constraint must be written as a table constraint when it involves two columns?

  1. NOT NULL
  2. DEFAULT
  3. UNIQUE (a, b)
  4. None, all are column-only

Which constraint combines UNIQUE and NOT NULL?

  1. CHECK
  2. FOREIGN KEY
  3. PRIMARY KEY
  4. DEFAULT