Keys and constraints: the rules behind a row
Watch PostgreSQL 18 accept, reject and explain rows with primary keys, uniqueness, null rules, checks and foreign keys. Foundations · Databases.
You add a row that looks fine, yet PostgreSQL refuses it. The error is not random: one rule protects identity, another protects a value, and another protects a relationship. We will use PostgreSQL 18 syntax and trace the row that meets each rule or trips over it.
Let the table name what makes a row itself
The first frame shows two small tables. learners has one-column identity in learner_id; registrations has a two-column identity in (learner_id, cohort_id). Declaring a primary key tells the database and readers which value, or tuple of values, identifies a row. A primary-key column is also non-null.
The same schema gives email a UNIQUE rule, requires email and age with NOT NULL, and limits age with CHECK (age BETWEEN 13 AND 90). The registration columns are both required and each is a foreign key. A foreign key asks for a matching key in its parent table; it does not invent the parent row.
One row, several constraint decisions
Read the steps as text
- The primary key names row identity. Registrations use the complete learner and cohort tuple, while the other declarations protect values and parent links.
- Learner 7 repeats, but cohort 102 makes a new composite tuple. The candidate is under review before the database accepts it.
- The complete tuple (7, 102) is unique, and both parent keys exist, so the registration is accepted.
- The candidate repeats the complete tuple (7, 101). Repeating either component is fine, but repeating the pair is a primary-key violation.
- Learner 9 has a new id but reuses Ava's non-null email. UNIQUE protects the value independently of the primary key.
- This learner supplies no email. UNIQUE does not make a missing value present; NOT NULL rejects the null before it can be stored.
- Age 12 is present, but the expression age BETWEEN 13 AND 90 is false. CHECK rejects a false result; it is not a uniqueness rule.
- A new learner has no learner_id. A primary key is also NOT NULL, so the row has no usable identity and is rejected.
- Registration (9, 101) names a learner that is absent. The foreign key rejects the link instead of creating learner 9.
- Registration (8, 999) finds learner 8 but no cohort 999. Every referenced key in the row must satisfy its own foreign key.
- The accepted rows satisfy identity, value and parent rules. Each rejected row has one named reason, so the constraint—not a vague database failure—explains the outcome.
Start by reading the declarations in the left panel, then follow the candidate row on the right. The snapshots are a compact view of DDL decisions, not an execution plan. The learner and cohort rows already exist, so registration (7, 101) is a valid pair. The next registration (7, 102) repeats learner 7 but forms a new tuple, so the composite key accepts it. A composite key makes the combination unique; it does not make each component unique on its own.
When (7, 101) is tried again, the complete tuple already exists and the primary key rejects it. A learner with the existing email trips UNIQUE, even though its numeric id is new. A missing email trips NOT NULL; the database does not treat an absent required value as a new identity. An age of 12 makes the check expression false, so CHECK rejects it.
The final attempts show the relationship rule. Registration (9, 101) cannot pass because learner 9 is absent. Registration (8, 999) cannot pass because cohort 999 is absent. A foreign key checks the referenced key; it does not insert a parent row or turn an orphan into a valid relationship.
The rules in one sentence each
A missing value is not always a failure
The second scene uses mentors and notes. It is a fresh dataset so you can separate three different cases: an optional foreign key, an unknown CHECK result, and nulls under ordinary UNIQUE.
Nullable links and unknown checks
Read the steps as text
- Mentor 2 already has a null handle. The notes table allows a nullable mentor and a nullable score, so each null needs its own PostgreSQL rule.
- A null mentor_id is exempt under PostgreSQL's default MATCH SIMPLE. It does not match a parent null; the relationship is optional for this row.
- The row is accepted: its score is positive, its state is valid, and the null mentor leaves the optional relationship without a parent lookup.
- Note 13 has a null score. score > 0 is unknown rather than false, and PostgreSQL treats an unknown CHECK result as satisfied.
- A score of 0 makes score > 0 false, so the same CHECK now rejects the row. Null and false are different outcomes.
- Mentor 3 uses another null handle. Ordinary PostgreSQL UNIQUE treats nulls as distinct by default, so this second null is accepted.
- The three null cases have three explanations: an optional link is exempt, an unknown check passes, and ordinary UNIQUE keeps nulls distinct by default.
In PostgreSQL's default MATCH SIMPLE, notes.mentor_id is nullable. The row with a null mentor is accepted without matching a parent whose key is null. This is an exemption for the incomplete foreign-key value, not a claim that NULL = NULL is true. Add NOT NULL when the relationship is mandatory. For a composite foreign key, MATCH FULL is a different convention: all parts must be null together or the complete tuple must match.
The score column has CHECK (score > 0) but no NOT NULL. When score is null, the expression is unknown, and PostgreSQL treats a check as satisfied when it is true or unknown. A score of 0 is false and is rejected. Pair CHECK with NOT NULL when absence must fail.
The second null mentor handle is accepted by ordinary PostgreSQL UNIQUE: nulls are distinct by default. This is PostgreSQL's default, not a promise about every SQL implementation. If the declaration says UNIQUE NULLS NOT DISTINCT, nulls compare as one uniqueness group and that second null is rejected.
Recap
- Primary keys identify rows; a composite primary key protects the whole tuple.
UNIQUE,NOT NULLandCHECKprotect values, each with its own null rule.- A foreign key protects an existing parent relationship, with PostgreSQL's nullable-FK convention stated explicitly.
The DDL details worth carrying forward
The primary-key declaration communicates which key is the table's identity. UNIQUE protects a value such as email; it does not silently become the table's identity.
For a composite foreign key under MATCH SIMPLE, any null referencing component exempts the row. This is a relationship convention; it does not make null equal to null. Referential actions and delete design are separate decisions.
For more practice tracing data structures that preserve identity, explore hash maps in Fundamentals. A map can find a key quickly; a database constraint still decides whether the row or relationship is allowed.