设置表关系“Cascade”、“Set Null”的作用是什么?和“限制”做?
我想在新项目中开始使用表关系。
经过一番谷歌搜索后,我得到了 2 个设置为 InnoDB 的表:
我想要链接的键是
->users->userid (主要) ->sessions->userid (index)
在此过程中我唯一不明白的是“更新时”和“删除时”的不同设置的作用
这里的选项是:
- --(什么都没有?)
- Cascade (???)
- Set Null(将所有内容设置为 null?)
- 无操作(嗯……)
- 限制(???)
我基本上希望在用户完全删除时删除会话中的数据 这是因为只有当我的会话管理器检测到过期时,会话才会被删除......
因此,如果有人能告诉我这些选项的作用,我将不胜感激。
I want to start using table relations in a new project.
After some googling I got 2 tables set up as InnoDB:
The keys I want to link are
->users->userid (primary)
->sessions->userid (index)
The only thing that I don't understand in this process is what the different settings for "On update" and "On delete" do
The options here are:
- -- (nothing?)
- Cascade (???)
- Set Null (sets everything to null?)
- No action (well duh...)
- Restrict (???)
I basically want the data in sessions to be deleted when a user is completely deleted
This since the sessions will only be deleted when the expiration is detected by my session manager...
So if anyone can tell me what these options do it would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当父级更改时,
CASCADE
将传播更改。 (如果删除一行,则约束表中引用该行的行也将被删除,等等)当父行消失时,
SET NULL
将列值设置为 NULL。RESTRICT
导致尝试删除父行失败。编辑:您没有询问它们,但 SQL 标准定义了另外两个操作:
SET DEFAULT
和NO ACTION
。在 MySQL 中,NO ACTION
相当于RESTRICT
。 (在某些 DBMS 中,NO ACTION
是延迟检查,但在 MySQL 中,所有检查都是立即进行的。)MySQL 解析器接受SET DEFAULT
,但 InnoDB 和 NDB 引擎都拒绝这些语句,因此SET DEFAULT
实际上不能用于ON UPDATE
或ON DELETE
约束。另请注意,级联外键操作不会激活 MySQL 中的触发器。
CASCADE
will propagate the change when the parent changes. (If you delete a row, rows in constrained tables that reference that row will also be deleted, etc.)SET NULL
sets the column value to NULL when a parent row goes away.RESTRICT
causes the attempted DELETE of a parent row to fail.EDIT: You didn't ask about them, but the SQL standard defines two other actions:
SET DEFAULT
andNO ACTION
. In MySQL,NO ACTION
is equivalent toRESTRICT
. (In some DBMSs,NO ACTION
is a deferred check, but in MySQL all checks are immediate.) The MySQL parser acceptsSET DEFAULT
, but both the InnoDB and NDB engines reject those statements, soSET DEFAULT
can't actually be used for either anON UPDATE
orON DELETE
constraint.Also, note that cascading foreign key actions do not activate triggers in MySQL.
包含外键的表称为引用或子表,包含候选键的表称为引用或父表。
The table containing the foreign key is called the referencing or child table, and the table containing the candidate key is called the referenced or parent table.
SQL 中外键约束的三种主要引用操作类型是 Cascade、Set Null 和 Restrict。
级联
:当父表中的记录被删除时,子表中的所有相关记录也将被删除。
示例:
SET NULL
:当父表中的记录被删除时,子表中的任何相关记录都将被设置为
NULL
。示例:
RESTRICT
:当删除父表中的记录时,将抛出错误并阻止删除。
例子:
The three main types of referential actions for foreign key constraints in SQL are Cascade, Set Null and Restrict.
CASCADE
:When a record in the parent table is deleted, any related records in the child table will also be deleted.
Example:
SET NULL
:When a record in the parent table is deleted, any related records in the child table will be set to
NULL
.Example:
RESTRICT
:When a record in the parent table is deleted, an error will be thrown and the deletion will be prevented.
Example: