Home  >  Article  >  Database  >  What Happens to Related Data When a Parent Row is Deleted in MySQL?

What Happens to Related Data When a Parent Row is Deleted in MySQL?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-04 17:15:02967browse

What Happens to Related Data When a Parent Row is Deleted in MySQL?

MySQL's Default ON DELETE Behavior Explained

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn