MySQL的默认ON DELETE行为是什么? [英] What is MySQL's default ON DELETE behavior?

查看:76
本文介绍了MySQL的默认ON DELETE行为是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析MySQL 文档.他们可能会更清楚.他们似乎在说的是,有五种可能性:SET NULL,NO ACTION,RESTRICT,CASCADE和SET DEFAULT.

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.

NO ACTION和RESTRICT会执行相同的操作(防止任何破坏FK的数据库更改),并且该操作是默认操作,因此,如果省略ON DELETE子句,则表示NO ACTION(或RESTRICT -是相同的操作).

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).

SET NULL允许删除父行,将FK设置为​​NULL.

SET NULL allows a parent row deletion, sets the FK to NULL.

CASCADE删除子行.

CASCADE deletes child row.

SET DEFAULT应该永远不被使用.

SET DEFAULT should just never be used.

这或多或少是正确的吗?

Is this more or less correct?

推荐答案

是的,这是正确的:

不采取行动:[...] InnoDB 拒绝删除或更新操作 用于父表.

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

限制:拒绝删除或更新 父表的操作. 指定RESTRICT(或NO ACTION)为 等同于省略ON DELETE或 ON UPDATE子句. [...]

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 ACTIONRESTRICT是同义词.此外,由于每当没有ON DELETE / 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.

设置为空:从父表中删除或更新该行,并设置 外键列或 子表为NULL. [...]

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. [...]

外部列设置为NULL,前提是未将其声明为NOT NULL(否则InnoDB将不允许删除或更新).

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

CASCADE :从父表中删除或更新该行,然后 自动删除或更新 匹配子表中的行. [...]

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.

设置默认值:可以识别此操作 通过解析器,但InnoDB拒绝 包含ON DELETE的表定义 SET DEFAULT或ON UPDATE SET DEFAULT 条款.

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.

这篇关于MySQL的默认ON DELETE行为是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆