mysql - 收到错误无法删除或更新父行 : a foreign key constraint fails even setting ON DELETE NO ACTION ON UPDATE NO ACTION

标签 mysql

我已经检查了所有与此错误有关的问题,但找不到我的答案

这是我的 EER 图 EER Diagram 我在 Sales 中插入一行,例如像这样(SellerFId=1,GoodFId=1,CustomerFId=1)所以我希望能够从他们自己的表中删除 Seller Or Good 或 Customer 这样在 Sales 中插入的行仍然保留我使用This Foreign Constrain between Sales and Other tables

 ON DELETE NO ACTION ON UPDATE NO ACTION

但是当我从他们自己的表中删除卖家或好人或客户时,mysql 不允许我并给我这个错误

Cannot delete or update a parent row: a foreign key constraint fails (newmobarakkabul.sales, CONSTRAINT fk_Sellers_has_Goods_has_Customers_Sellers1 FOREIGN KEY (SellerFId) REFERENCES staffs (StaffId) ON DELETE NO ACTION ON UPDATE NO ACTION)

具体来说,我的问题是,我是否真的需要在 Sales 表和其他表之间设置外键约束(如果不需要,那么这个查询“ON DELETE NO ACTION ON UPDATE NO ACTION”是什么? )

最佳答案

在 MySQL 中,NO ACTION is an equivalent of RESTRICT

NO ACTION: A keyword from standard SQL. In MySQL, equivalent to RESTRICT. The MySQL Server rejects the delete or update operation for the parent table if there is a related foreign key value in the referenced table. Some database systems have deferred checks, and NO ACTION is a deferred check. In MySQL, foreign key constraints are checked immediately, so NO ACTION is the same as RESTRICT.

还有

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.

至于你最后一个问题,你是否真的需要这样的约束要根据你的应用程序的规范来确定。

关于mysql - 收到错误无法删除或更新父行 : a foreign key constraint fails even setting ON DELETE NO ACTION ON UPDATE NO ACTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35753708/

相关文章:

php - Laravel 5 无法从数据库返回或调用表结果

PHP MySQL - 按时差限制查询结果

PHP select 不显示任何值

php - 更新事务后提交

php - 按产品过滤订单

mysql - 如果在其他表中没有匹配项,则将什么用作外键 - MySQL

mysql - 显示数据库中的文章,但它有 html 标签

php - 是否可以在 SQL 中使用 if 语句?

php - MySQL - 如何根据用户 ID 合并行并生成 SUM

php - 如何使用 mySQL 为页面标题、下一篇文章、上一篇文章编写 php 代码?