What is MySQL's default ON DELETE behavior?

后端 未结 1 1443
面向向阳花
面向向阳花 2020-11-29 22:21

I\'m trying to parse the MySQL docs. They could be clearer. What they seem to be saying is that there are five possibilities: SET NULL, NO ACTION, RESTRICT, CASCADE, and SET

相关标签:
1条回答
  • 2020-11-29 22:36

    Yes, it is correct:

    NO ACTION: [...] InnoDB rejects the delete or update operation for the parent table.

    RESTRICT: Rejects the delete or update operation for the parent table. Specifying RESTRICT (or NO ACTION) is the same as omitting the ON DELETE or ON UPDATE clause. [...]

    Apparently NO ACTION and RESTRICT are synonymous. Additionally, since they are used whenever there is no ON DELETE / UPDATE clause, this is the default behavior.

    SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the child table to NULL. [...]

    The foreign column is set to NULL, provided it is not declared as NOT NULL (or InnoDB will not allow deletion or update).

    CASCADE: Delete or update the row from the parent table and automatically delete or update the matching rows in the child table. [...]

    Cascade deletes (or updates) the foreign column.

    SET DEFAULT: This action is recognized by the parser, but InnoDB rejects table definitions containing ON DELETE SET DEFAULT or ON UPDATE SET DEFAULT clauses.

    So basically you cannot use that option.

    0 讨论(0)
提交回复
热议问题