设置表关系“Cascade”、“Set Null”的作用是什么?和“限制”做?

发布于 2024-10-26 01:48:56 字数 433 浏览 2 评论 0原文

我想在新项目中开始使用表关系。

经过一番谷歌搜索后,我得到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满地尘埃落定 2024-11-02 01:48:56

当父级更改时,CASCADE 将传播更改。 (如果删除一行,则约束表中引用该行的行也将被删除,等等)

当父行消失时,SET NULL 将列值设置为 NULL。

RESTRICT 导致尝试删除父行失败。

编辑:您没有询问它们,但 SQL 标准定义了另外两个操作:SET DEFAULTNO ACTION。在 MySQL 中,NO ACTION 相当于 RESTRICT。 (在某些 DBMS 中,NO ACTION 是延迟检查,但在 MySQL 中,所有检查都是立即进行的。)MySQL 解析器接受 SET DEFAULT,但 InnoDB 和 NDB 引擎都拒绝这些语句,因此 SET DEFAULT 实际上不能用于 ON UPDATEON 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 and NO ACTION. In MySQL, NO ACTION is equivalent to RESTRICT. (In some DBMSs, NO ACTION is a deferred check, but in MySQL all checks are immediate.) The MySQL parser accepts SET DEFAULT, but both the InnoDB and NDB engines reject those statements, so SET DEFAULT can't actually be used for either an ON UPDATE or ON DELETE constraint.

Also, note that cascading foreign key actions do not activate triggers in MySQL.

脸赞 2024-11-02 01:48:56

包含外键的表称为引用子表,包含候选键的表称为引用父表

Set NULL:删除父表行时将列值设置为NULL

CASCADE:当父级更改时,CASCADE 将传播更改。如果删除一行,则约束表中引用该行的行也将被删除,等等。

RESTRICT :如果存在引用给定父行的值的子行,RESTRICT 会导致您无法删除该父行。

NO ACTION:NO ACTION 和 RESTRICT 非常相似。当对引用的表执行 UPDATE 或 DELETE 语句时,DBMS 在语句执行结束时验证没有任何引用关系违反。在简短的子行中,无需担心父行是否删除更新

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.

Set NULL : Sets the column value to NULL when you delete the parent table row.

CASCADE : 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.

RESTRICT : RESTRICT causes you can not delete a given parent row if a child row exists that references the value for that parent row.

NO ACTION : NO ACTION and RESTRICT are very much alike. when an UPDATE or DELETE statement is executed on the referenced table, the DBMS verifies at the end of the statement execution that none of the referential relationships are violated. in short child row no concern if parent row delete or update.

平生欢 2024-11-02 01:48:56

SQL 中外键约束的三种主要引用操作类型是 Cascade、Set Null 和 Restrict。

级联
当父表中的记录被删除时,子表中的所有相关记录也将被删除。

示例:

CREATE TABLE orders (
  order_id INT NOT NULL,
  customer_id INT NOT NULL,
  PRIMARY KEY (order_id),
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
  ON DELETE CASCADE
);

SET NULL
当父表中的记录被删除时,子表中的任何相关记录都将被设置为NULL

示例:

CREATE TABLE orders (
  order_id INT NOT NULL,
  customer_id INT,
  PRIMARY KEY (order_id),
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
  ON DELETE SET NULL
);

RESTRICT
当删除父表中的记录时,将抛出错误并阻止删除。

例子:

CREATE TABLE orders (
  order_id INT NOT NULL,
  customer_id INT NOT NULL,
  PRIMARY KEY (order_id),
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
  ON DELETE 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:

CREATE TABLE orders (
  order_id INT NOT NULL,
  customer_id INT NOT NULL,
  PRIMARY KEY (order_id),
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
  ON DELETE CASCADE
);

SET NULL:
When a record in the parent table is deleted, any related records in the child table will be set to NULL.

Example:

CREATE TABLE orders (
  order_id INT NOT NULL,
  customer_id INT,
  PRIMARY KEY (order_id),
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
  ON DELETE SET NULL
);

RESTRICT:
When a record in the parent table is deleted, an error will be thrown and the deletion will be prevented.

Example:

CREATE TABLE orders (
  order_id INT NOT NULL,
  customer_id INT NOT NULL,
  PRIMARY KEY (order_id),
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
  ON DELETE RESTRICT
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文