Integrity Constraints
Constraints are the database's promises. If the data violates them, the DBMS rejects the change instead of letting the application decide.
In plain English
These are house rules: every room needs a number, and every key must open a real door.
The one idea
Constraints are executable business rules: the DBMS refuses any state that violates them, regardless of which application wrote the data.
Application validation is bypassed by imports, scripts, and bugs. Database constraints are the last line of defense for shared data.
By the end
- 01Explain entity, domain, and referential integrity.
- 02Use NOT NULL, UNIQUE, CHECK, and foreign keys.
- 03Know when to cascade and when to restrict.
- 04Distinguish column constraints from table constraints.
Entity and domain integrity
Entity integrity requires every tuple to have a non-null primary key, so each row is identifiable. Domain integrity requires every value to belong to the attribute's domain: correct type, allowed range, and valid format. CHECK constraints and NOT NULL enforce domain rules; data types enforce the basic domain.
CREATE TABLE Student (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER CHECK (age >= 0 AND age <= 150)
);Referential integrity
A foreign key must match an existing referenced key or be NULL. When a referenced row is deleted or updated, the database must choose an action. RESTRICT blocks the change. CASCADE propagates it. SET NULL clears the reference, and SET DEFAULT assigns a default. The right action depends on the business rule, not on convenience.
Column versus table constraints
A column constraint applies to one attribute. A table constraint can span several attributes, such as a composite primary key or a CHECK comparing two columns. Name your constraints so errors are readable and migrations are manageable.
A salary that cannot be negative
Employee has salary and department_id. The business requires salary > 0 and a valid department.
Add CHECK (salary > 0).
An insert with -1 is rejected even if the application forgets to validate.
Add FOREIGN KEY (department_id) REFERENCES Department(id).
An employee cannot point to a department that does not exist.
Choose ON DELETE RESTRICT.
Deleting a department with employees fails instead of silently orphaning them.
ConclusionThe constraint belongs to the data model, not to one UI. Put the rule where every writer must pass through it.
The trade-off
No free correctness.
Gain
Guaranteed integrity and less duplicated validation logic.
Cost
Every write pays a check; bulk loads and cascades can become slower or surprising.
Verdict
Prefer declarative constraints over triggers; relax them only with a measured reason.
Recap
What to keep.
Takeaways
- +Entity integrity: no null primary keys.
- +Domain integrity: values must be valid for their type and rules.
- +Referential integrity: foreign keys must match existing rows or be NULL.
- +CASCADE, RESTRICT, SET NULL, and SET DEFAULT are delete/update actions.
- +Table constraints can enforce multi-column rules.
Traps
- !Assuming a data type alone enforces all domain rules.
- !Using CASCADE without understanding the business rule.
- !Forgetting that NULL foreign keys are allowed unless declared otherwise.
Focus
- →Write a table with primary, foreign, and CHECK constraints.
- →Choose cascade versus restrict for a concrete relationship.
- →Explain entity, domain, and referential integrity in one sentence each.
Check yourself
Before you turn the page.
What does entity integrity require?
What does ON DELETE CASCADE do?