MySQL 的默认 ON DELETE 行为是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1027656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is MySQL's default ON DELETE behavior?
提问by Ethan
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 DEFAULT.
我正在尝试解析 MySQL文档。他们可能会更清楚。他们似乎在说有五种可能性:SET NULL、NO ACTION、RESTRICT、CASCADE 和 SET DEFAULT。
NO ACTION and RESTRICT do the same thing (prevent any DB change that breaks an FK) and that thing is the default so if you omit an ON DELETE clause you're saying NO ACTION (or RESTRICT -- same thing).
NO ACTION 和 RESTRICT 做同样的事情(防止任何破坏 FK 的 DB 更改)并且那件事是默认值,因此如果您省略 ON DELETE 子句,您就是说 NO ACTION(或 RESTRICT - 同样的事情)。
SET NULL allows a parent row deletion, sets the FK to NULL.
SET NULL 允许删除父行,将 FK 设置为 NULL。
CASCADE deletes child row.
CASCADE 删除子行。
SET DEFAULT should just never be used.
永远不应该使用 SET DEFAULT。
Is this more or less correct?
这或多或少是正确的吗?
回答by Anax
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. [...]
NO ACTION: [...] InnoDB 拒绝父表的删除或更新操作。
RESTRICT:拒绝父表的删除或更新操作。指定 RESTRICT(或 NO ACTION)与省略 ON DELETE 或 ON UPDATE 子句相同。[...]
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.
显然NO ACTION
和RESTRICT
是同义词。此外,由于它们在没有ON DELETE / UPDATE
子句时使用,因此这是默认行为。
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. [...]
SET NULL:从父表中删除或更新行并将子表中的外键列或列设置为NULL。[...]
The foreign column is set to NULL, provided it is not declared as NOT NULL (or InnoDB will not allow deletion or update).
外部列设置为 NULL,前提是它未声明为 NOT NULL(或 InnoDB 将不允许删除或更新)。
CASCADE: Delete or update the row from the parent table and automatically delete or update the matching rows in the child table. [...]
CASCADE:从父表中删除或更新行并自动删除或更新子表中的匹配行。[...]
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.
SET DEFAULT:解析器识别此操作,但 InnoDB 拒绝包含 ON DELETE SET DEFAULT 或 ON UPDATE SET DEFAULT 子句的表定义。
So basically you cannot use that option.
所以基本上你不能使用那个选项。