In MySQL, the ON DELETE behavior determines what happens to related data in a child table when a record is deleted from the parent table. By default, MySQL employs the NO ACTION behavior.
Default Behavior: NO ACTION
Both NO ACTION and RESTRICT prevent changes that violate foreign key constraints. When the ON DELETE clause is omitted, these default behaviors take effect. They preserve the integrity of referenced data by preventing its deletion or modification.
Other Behavior Options
SET NULL:
Deletes the parent row and sets the foreign key in the child table to NULL (if permitted).
CASCADE:
Deletes child rows that reference the deleted parent row.
SET DEFAULT:
This option is not supported by InnoDB. It was recognized by the parser but rejected upon table definition.
Summary
The following table summarizes the ON DELETE behaviors available in MySQL:
Behavior | Description |
---|---|
NO ACTION | Prevents deletion or update of parent rows |
RESTRICT | Same as NO ACTION |
SET NULL | Sets foreign key to NULL when parent row is deleted |
CASCADE | Deletes child rows when parent row is deleted |
The above is the detailed content of What Happens to Related Data When a Parent Row is Deleted in MySQL?. For more information, please follow other related articles on the PHP Chinese website!