How MySQL Handles Updates to Parent Keys Referenced by Foreign Keys
When a parent table’s key (usually a PRIMARY KEY or UNIQUE key) is updated, MySQL enforces foreign key rules to maintain referential integrity. The behavior depends on the ON UPDATE action defined in the foreign key constraint.
ON UPDATE CASCADE → Automatically updates the child table’s foreign key values.
ON UPDATE SET NULL → Sets the child foreign key column to NULL (only allowed if the column is nullable).
ON UPDATE RESTRICT → Prevents the update if child rows exist.
ON UPDATE NO ACTION → Same as RESTRICT in MySQL (checks after the statement).
MySQL checks all referencing child rows before modifying the parent key.
If CASCADE is used, MySQL updates child rows in the same transaction.
If RESTRICT/NO ACTION is used, the update fails if any child rows depend on the parent.
All updates involve index lookups, since foreign keys require indexed columns.
Parent key updates are rare because primary keys are usually stable identifiers.
CASCADE can cause large update operations if many child rows depend on the parent.
Using BIG, complex primary keys increases the cost of cascading updates.
MySQL requires that referenced keys be indexed to ensure fast update propagation.
In summary, MySQL ensures referential integrity during parent key updates by applying ON UPDATE rules—automatically propagating changes with CASCADE, nullifying child references with SET NULL, or blocking the operation entirely when RESTRICT or NO ACTION is used.