Skip to content
Contents/CH 14
Keys & Constraints/Intermediate · 12 min

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

  1. 01Explain entity, domain, and referential integrity.
  2. 02Use NOT NULL, UNIQUE, CHECK, and foreign keys.
  3. 03Know when to cascade and when to restrict.
  4. 04Distinguish column constraints from table constraints.
01

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.

chapter-14.db
CREATE TABLE Student (
    id      INTEGER PRIMARY KEY,
    name    TEXT NOT NULL,
    age     INTEGER CHECK (age >= 0 AND age <= 150)
);
02

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.

03

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.

Worked example3 steps

A salary that cannot be negative

Employee has salary and department_id. The business requires salary > 0 and a valid department.

01

Add CHECK (salary > 0).

An insert with -1 is rejected even if the application forgets to validate.

02

Add FOREIGN KEY (department_id) REFERENCES Department(id).

An employee cannot point to a department that does not exist.

03

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.

0 / 2 answered
01

What does entity integrity require?

02

What does ON DELETE CASCADE do?

DB//COSMOS

A field manual for database systems: model the domain, normalize the facts, query the relations, and protect the result.

Edition 2026 · 32 chaptersTopic catalogInterview prep