concept Data & storage
Foreign keys and cascading deletes
A foreign-key action can block a parent’s removal, cascade it to referencing children, or keep the children with NULL keys. The resulting rows must still satisfy the applicable constraints.
What it means
When you remove a parent row, its children do not necessarily disappear with it. A foreign key declares how rows in the child table reference the parent. Its delete action determines whether those references block removal, the referencing rows are removed too, or the rows stay with a changed key. PostgreSQL 18: foreign-key actions
In this example, C1 and C2 point to P1, while CX points to P2. The question is what happens if you remove P1. The preview uses synthetic rows and never sends a DELETE to a database.
Try it
Start with Foreign-key delete action set to restrict, Rows referencing P1 at 2 child rows, Child key permits NULL set to Yes, and Constraint timing set to immediate. Both children still reference P1, so restrict blocks its removal. No rows are removed and no keys are set to NULL.
Foreign keys and cascading deletes
Use synthetic parent/child tables and declared restrict/cascade/set-null policies; preview all affected rows and blocking constraints before any simulated action.
Local simulation only: no device, service, stored object or database is changed, and no real operation is authorized.
RESTRICT blocks the referenced parent deletion and does not defer its check.
Preview the reference action before any simulated deletion
- Constraint outcome
- blocked
- Rows removed by preview
- None
- Child keys set to NULL
- None
- Unrelated rows preserved
- P2 · CX
- Applied to a real database
- No
Evaluated evidence
- Foreign-key delete action
- restrict
- Rows referencing P1 (child rows)
- 2
- Child key permits NULL
- yes
- Constraint timing
- immediate
PostgreSQL18-inspired one-level preview. No triggers, deeper cascades or actual mutation. SET NULL must satisfy other constraints; only nullability is varied here.
About this model
Every input is local synthetic evidence. Numbers stay as raw text until this model validates them. The shown evaluated values come from the same result as the explanation.
Presets replace the entire scenario. Reset restores all starting fields, scenario selection and model state; it does not change the open or closed state of this help or article FAQ.
No external request, device command, stored-input history or measurement is performed.
- Choose Preview a cascading delete. With
cascade, the preview follows both references toP1: the removal list isP1,C1,C2.P2andCXstay becauseCXpoints toP2, notP1. - Change the action to
set-null. OnlyP1is on the removal list.C1andC2stay, but each child’s foreign key would change fromP1toNULL. - Change only Child key permits NULL to No. Now the proposed replacement value violates
NOT NULL. The preview blocks the removal ofP1; it neither deletes the children nor changes their keys.
The references matter as much as the action. With restrict and immediate, setting Rows referencing P1 to 0 leaves no child to block removal of P1. Enter a whole-number string from 0 through 3: 2.000000000000000001 is invalid, not rounded to 2, and produces no preview. Reset restores the starting scenario and all four defaults, returns the step and virtual time to zero, and clears errors, history, and comparisons.
In practice
Separate the action from the timing of the check. In PostgreSQL 18, RESTRICT cannot be deferred, but a NO ACTION check can be deferred when the constraint is configured for it. Foreign-key actions Deferring that check does not relax nullability: SET CONSTRAINTS does not control NOT NULL or CHECK constraints. Constraint timing
With two children, no-action, and immediate, removing P1 would leave C1 and C2 pointing to a missing parent, so the preview blocks it. Inspect the evidence boundary uses no-action and deferred instead. Its result is unknown, not “allowed”: the preview does not model later changes that might repair the references or the check at commit.
This preview stops at one level. It does not model triggers or deeper cascades, and nullability is the only child constraint it varies for set-null. To reason about a removal, first follow the declared references and list the rows that would disappear or change. Then check whether the surviving rows and their proposed key values satisfy the applicable constraints when that database checks them.
Frequently asked questions
Editorial questions based on the listed sources and explicitly labeled local examples.
Does cascade delete unrelated rows?
Not in this declared foreign-key relationship. The preview identifies only children referencing the selected parent; the unrelated parent and its child remain unchanged. Other relationships would require additional modeled rules.
What can make SET NULL fail?
The replacement NULL must still satisfy the child table’s constraints. In the example, a nonnullable foreign-key column blocks SET NULL instead of silently deleting or invalidating the row.
Are RESTRICT and deferred NO ACTION identical?
No. PostgreSQL 18 distinguishes nondeferrable RESTRICT from a NO ACTION check that can be deferred when configured. This one-action preview leaves deferred final-state validity unknown.
Does the preview change a database?
No. The tables are fixed synthetic records. All affected-row lists and blocking outcomes are computed locally, without a database connection or a DELETE statement.
Sources & model boundaries
Primary references used for this explanation. The interactive example is an educational model, not a live check.
- PostgreSQL constraints
Foreign keys: ON DELETE actions. RESTRICT cannot be deferred; NO ACTION can be deferred when configured. CASCADE follows references and SET NULL still must satisfy constraints.
Checked .
- PostgreSQL: SET CONSTRAINTS
Description: immediate and deferred checks. Configured deferrable constraints may be checked later; NOT NULL and CHECK constraints are not controlled by SET CONSTRAINTS.
Checked .