级联和限制之间的区别?SQL DDL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20619211/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:28:58  来源:igfitidea点击:

difference between cascade and restrict? SQL DDL database

sqldatabaseddl

提问by user3102872

Could anyone tell me what exactly cascade, and restrict mean? It's in database systems subject the DDL Part

谁能告诉我级联和限制究竟是什么意思?它在数据库系统主题 DDL 部分

and what if i don't write any of them in my delete statement?

如果我不在我的删除语句中写任何一个怎么办?

回答by Aram

The ON DELETE CASCADE and ON DELETE RESTRICT are the foreign key property and you set them when you are creating the relationship between two tables.

ON DELETE CASCADE 和 ON DELETE RESTRICT 是外键属性,您在创建两个表之间的关系时设置它们。

If you set the relationship to be ON DELETE CASCADE, when you run a DELETE statement on a parent table it will DELETE all the corresponding rows from the CHILD table automatically. But the RESTRICT (which is the default foreign key relationship behavior) is when you try to delete a row from the parent table and there is a row in the child table with the same ID, it will fail complaining about the existing child rows.

如果将关系设置为 ON DELETE CASCADE,则在父表上运行 DELETE 语句时,它将自动从 CHILD 表中删除所有相应的行。但是 RESTRICT(这是默认的外键关系行为)是当您尝试从父表中删除一行并且子表中存在具有相同 ID 的行时,它将无法抱怨现有的子行。

Either way, you don't need to mention anything in your DELETE clause.

无论哪种方式,您都不需要在 DELETE 子句中提及任何内容。

I also wrote a blog post about the different rules for Delete and Update commands in more detail here:

我还在这里写了一篇关于删除和更新命令的不同规则的博客文章:

https://koukia.ca/sql-server-foreign-key-update-and-delete-rules-556cf09117fe

https://koukia.ca/sql-server-foreign-key-update-and-delete-rules-556cf09117fe

回答by Aditya Parmar

There are three types of on delete associated with foreign key

与外键关联的 on delete 有三种类型

  1. On Delete Cascade: when data is removed from a parent table, automatically data deleted from child table (foreign key table).
  2. On Delete set Null: when data is removed from a parent table, the foreign key associated cell will be null in a child table.
  3. On Delete Restrict: when data is removed from a parent table, and there is a foreign key associated with child table it gives error, you can not delete the record.
  1. 在删除级联上:当从父表中删除数据时,自动从子表(外键表)中删除数据。
  2. 删除集空:当从父表中删除数据时,子表中的外键关联单元格将为空。
  3. 关于删除限制:当数据从父表中删除,并且有一个与子表关联的外键时,它会出错,您不能删除记录。

回答by elifares

Right so these ON DELETE and ON UPDATE apply to the parent table/row. The reason NO ACTION and RESTRICT are the same thing is because you cannot have a relationship with a child and a parent if the parent doesn't exist.

所以这些 ON DELETE 和 ON UPDATE 适用于父表/行。NO ACTION 和 RESTRICT 是同一件事的原因是因为如果父母不存在,您就无法与孩子和父母建立关系。