Rules the database refuses to break, written once in the schema instead of everywhere in the application.
Constraints: UNIQUE, CHECK and NOT NULL
query.sqlSQLite
-- Each of these violates exactly one constraint.
-- Run them one at a time and read which one fires.
INSERT INTO staff (id, email, age, salary) VALUES (4, 'ada@example.com', 30, 50000);
-- INSERT INTO staff (id, email, age, salary) VALUES (5, 'new@example.com', 15, 50000);
-- INSERT INTO staff (id, email, age, salary) VALUES (6, 'x@example.com', 30, -1);
-- INSERT INTO staff (id, email, age, salary, grade) VALUES (7, 'y@example.com', 30, 1, 'boss');
-- INSERT INTO staff (id, email, age) VALUES (8, 'z@example.com', 30);
Result
Worth knowing
NOT NULL says the value must be known. UNIQUE says no other row may hold it.
CHECK takes an expression that must be true for every row. It is the general case the others specialise.
A constraint holds regardless of which client wrote the row. Application validation only holds for the clients that run it.
UNIQUE permits multiple NULLs in most engines, because two unknowns are not known to be equal.
Overview
A constraint is a rule the database checks when data is inserted or changed. Unlike validation in one application screen, the rule applies to every writer that reaches the table. Use constraints for facts that must always hold, such as an identifier being unique or an order referring to an existing customer.
A constraint is a rule the database checks when data is inserted or changed. Unlike validation in one application screen, the rule applies to every writer that reaches the table. Use constraints for facts that must always hold, such as an identifier being unique or an order referring to an existing customer.
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
total NUMERIC CHECK (total >= 0),
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
Result
NOT NULL rejects missing values; UNIQUE prevents duplicate non-NULL keys in SQLite and PostgreSQL; CHECK requires a condition not to be false; and a foreign key enforces a reference when foreign-key checking is enabled. SQLite applications must enable PRAGMA foreign_keys = ON for each connection unless the build/configuration sets it otherwise.
NULL and defaults need care
A CHECK condition that evaluates to NULL is not false, so it can pass. Combine it with NOT NULL when a value is required. In SQLite and PostgreSQL, a UNIQUE constraint permits multiple NULL values because NULLs are not treated as equal for this purpose. Other engines offer different options.
A DEFAULT is not a validation rule: it supplies a value when a column is omitted (or DEFAULT is requested). It does not replace an explicitly supplied NULL. Test the exact behavior in your target engine.
Add rules safely
For an existing table, first find and correct rows that violate the proposed constraint. Then apply a migration and check how the engine validates or locks the table. Constraint enforcement and ALTER TABLE capabilities differ by database. See primary and foreign keys for choosing identifiers and normalization for schema structure.
Foreign-key actions are domain rules
A foreign key can reject deletion of a referenced parent, cascade the deletion to children, or set the child reference to NULL. These choices change what data survives, so they should reflect the domain. Cascading deletion may be correct for temporary child records but dangerous for invoices or audit history. Constraint failures are useful signals: surface a clear application error instead of retrying blindly. Also test enforcement in the deployed environment, especially with connection pools and SQLite where the foreign-key setting is connection-specific.
Comparing NULL to NULL yields NULL, not true. The constraint only rejects rows it can prove equal, so unknown values slip through - add NOT NULL if that is not what you meant.
What can a CHECK constraint do that NOT NULL and UNIQUE cannot?
CHECK takes an arbitrary expression over the row, so multi-column rules are enforceable. They are otherwise checked nowhere and violated eventually.
Why is a constraint stronger than the same rule in application code?
Application validation holds only for the code paths that run it. The database is the one component every write must pass through.
Cheat sheet
Constraints: UNIQUE, CHECK and NOT NULL
Add UNIQUE, CHECK and NOT NULL constraints to a table and watch the database reject every row that violates a rule you wrote once.
Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.