UNIQUE, CHECK & DEFAULT in SQL
Column-level rules: no duplicates, no nonsense, no blanks
Beyond keys, tables carry column rules. UNIQUE forbids repeated values (usernames, emails). CHECK enforces a boolean condition on every write (salary >= 0). DEFAULT supplies a value when INSERT leaves the column out. Together they push data validation into the database — the one place every writer must pass through.
UNIQUE guards sets of values
email VARCHAR(80) UNIQUE — the engine maintains a hidden unique index and rejects the second occurrence of any value at write time.
CHECK guards conditions
salary DECIMAL(10,2) CHECK (salary >= 0) — evaluated on every INSERT and UPDATE; a false row is rejected with the constraint’s name in the error.
DEFAULT fills the gaps
bonus DECIMAL(10,2) DEFAULT 0 — omit the column in INSERT and the engine writes 0, not NULL. The written value is real, so later reads and aggregates see it.
Rules compose per column
One column can carry NOT NULL, UNIQUE and a CHECK together; table-level CHECKs can compare two columns (end_date > start_date).
Validation is total
Constraints apply to every writer — the app, the migration script, the analyst’s bulk load. The database is the one gate everyone must pass.
1CREATE TABLE staff (2 id INTEGER PRIMARY KEY,3 email VARCHAR(80) UNIQUE,4 salary DECIMAL(10,2) CHECK (salary >= 0),5 bonus DECIMAL(10,2) DEFAULT 06);7INSERT INTO staff (id, email, salary) VALUES (1, 'a@x.com', -5);
| error |
|---|
| ERROR: new row for relation "staff" violates check constraint "staff_salary_check" |
The CHECK rejects the negative salary regardless of which app, script or intern sent it. The same table would also refuse a second a@x.com (UNIQUE) and fill bonus with 0 whenever it is omitted (DEFAULT).
CHECK rejects bad data
A negative salary violates the declared rule — regardless of who sent it.
1CREATE TABLE staff (2 id INTEGER PRIMARY KEY,3 email VARCHAR(80) UNIQUE,4 salary DECIMAL(10,2) CHECK (salary >= 0),5 bonus DECIMAL(10,2) DEFAULT 06);7INSERT INTO staff (id, email, salary) VALUES (1, 'a@x.com', -5);
| error |
|---|
| ERROR: new row for relation "staff" violates check constraint "staff_salary_check" |
UNIQUE rejects the second value
Two accounts cannot share one email.
1INSERT INTO staff (id, email, salary) VALUES (2, 'a@x.com', 60000);2-- row (1, 'a@x.com', …) already exists
| error |
|---|
| ERROR: duplicate key value violates unique constraint "staff_email_key" |
DEFAULT supplies the omitted value
bonus was not mentioned, but the row gets 0 — not NULL.
1INSERT INTO staff (id, email, salary) VALUES (3, 'b@x.com', 61000);2SELECT id, email, salary, bonus FROM staff WHERE id = 3;
| id | salary | bonus | |
|---|---|---|---|
| 3 | b@x.com | 61000 | 0 |
Table-level CHECK spans columns
A booking cannot end before it starts.
1CREATE TABLE bookings (2 id INTEGER PRIMARY KEY,3 start_at DATE NOT NULL,4 end_at DATE NOT NULL,5 CHECK (end_at > start_at)6);7INSERT INTO bookings VALUES (1, '2026-05-10', '2026-05-08');
| error |
|---|
| ERROR: new row for relation "bookings" violates check constraint "bookings_check" |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.