Constraints: UNIQUE, CHECK and NOT NULL

Rules the database refuses to break, written once in the schema instead of everywhere in the application.

Constraints: UNIQUE, CHECK and NOT NULL

query.sql SQLite
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.

In this guide
  1. Put durable rules beside the data
  2. NULL and defaults need care
  3. Add rules safely
  4. Foreign-key actions are domain rules
  5. Further reading

Put durable rules beside the data

query.sqlSQLite
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.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why does UNIQUE usually permit several NULLs?

  2. What can a CHECK constraint do that NOT NULL and UNIQUE cannot?

  3. Why is a constraint stronger than the same rule in application code?

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.

DATABASE · vizlearn.in/database/constraints_in_sql.html

About the author

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.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.