MySQL 的默认删除行为是什么?

我正在尝试解析 MySQL 医生。它们可以更清晰。他们似乎在说有五种可能性: SET NULL、 NO ACTION、 RESTRICT、 CASCADE 和 SET DEFAULT。

NO ACTION 和 RESTRICT 做同样的事情(防止任何破坏 FK 的 DB 更改) ,这是默认的,所以如果你省略了 ON DELETE 子句,你说 NO ACTION (或 RESTRICT ——同样的事情)。

SETNULL 允许删除父行,将 FK 设置为 NULL。

CASCADE 删除子行。

SET DEFAULT 应该永远不要使用。

这么说是对的还是错的?

49198 次浏览

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.