skip to content

Relational / SQL

The relational tier, in two halves: `db-relational-concepts` holds everything true of every SQL engine — the model, schema and constraints, transactions and isolation, indexes, the planner, storage and recovery, replication, partitioning and access control — and the eleven engine subtrees beside it hold what is true of one product only. A generic roadmap references the concepts child; a roadmap that names an engine references that engine.

on this pageshow

explore

→ has its own guide

questions

683 · 1 section

What does a CHECK constraint do in a relational table, and at what point does the database engine evaluate it?

level: juniorimportance: must knowfreq 60%
basics
~20 s

A CHECK constraint is a boolean condition attached to a table. The engine tests it for every row a statement inserts or updates; if the condition comes out false, the statement fails with a constraint-violation error and the row is not stored.

open as a page

If the application already validates every field before saving, why still declare constraints such as NOT NULL, unique keys and foreign keys in the database schema?

level: juniorimportance: must knowfreq 58%
basics
~20 s

Because the application is not the only writer, and its checks are not atomic with the write. Scripts, migrations, other services and future bugs all reach the same tables. Database constraints are the last line of defense that no code path can bypass; app validation exists for fast, friendly feedback.

open as a page

A column in one table references the key of another table under a foreign key constraint. What exactly does that constraint guarantee about the stored data, and what happens if the referencing column holds NULL?

level: juniorimportance: must knowfreq 78%
basics
~20 s

It guarantees every non-NULL value in the referencing (child) column exists in the referenced (parent) key, so there are no orphan rows. A NULL referencing value points at nothing, so the check is skipped and the row is accepted.

open as a page

A table column is declared NOT NULL with a DEFAULT value. Explain the difference between an INSERT that omits that column entirely and one that explicitly supplies NULL for it.

level: juniorimportance: must knowfreq 62%
basics
~20 s

Omitting the column makes the engine apply the DEFAULT, so the insert succeeds. Explicitly writing NULL is a real value being supplied — the DEFAULT is not consulted, and the NOT NULL constraint rejects the row.

open as a page

What does declaring a primary key on a table actually guarantee, and how does a relational engine enforce that guarantee?

level: juniorimportance: must knowfreq 82%
basics
~20 s

A primary key guarantees every row has a value in those columns (no NULLs) and no two rows share the same combination. The engine enforces it by marking the columns NOT NULL and maintaining a unique index it probes on every insert and key update.

open as a page