gpt4 book ai didi

mysql - MY SQL 外键约束错误

转载 作者:行者123 更新时间:2023-11-29 23:05:33 25 4
gpt4 key购买 nike

我使用 MySQL 数据库作为我的应用程序的后端。我正在使用数据库,几天前我做错了一些事情,现在我无法在我的数据库表上应用外键。我有 2 张 table ,店铺位置和披萨订单详细信息。在 Pizzaordrdetail 中,我将来自 shoplocation 的 id 作为外键传递。我收到以下错误。该表中已包含数据。早些时候,我在 Pizzaorderdetail 上有可用的外键约束,但不知何故它被删除了。请帮我解决这个错误。

执行:更改表订单pizzaorderdetail添加索引FK_idx(LocationID ASC);更改表订单pizzaorderdetail添加约束 FK 外键(LocationID) 引用订单位置 (id) 删除时不执行任何操作 更新时不执行任何操作;

操作失败:将 SQL 脚本应用到数据库时出错。错误 1452:无法添加或更新子行:外键约束失败(order.#sql-714_31,CONSTRAINT FK FOREIGN KEY ( LocationID) 引用 location (id) 删除时无操作 更新时无操作)SQL语句:更改表订单pizzaorderdetail添加约束 FK 外键(LocationID) 引用订单位置 (id) 删除时不执行任何操作 更新时无任何操作

最佳答案

你需要检查一下你的FK是否正常。当我收到这条消息时,我刚刚删除了我的FK,问题就解决了。

根据手册:https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html

InnoDB reports this error when you attempt to drop the last index that can enforce a particular referential constraint.

For optimal performance with DML statements, InnoDB requires an index to exist on foreign key columns, so that UPDATE and DELETE operations on a parent table can easily check whether corresponding rows exist in the child table. MySQL creates or drops such indexes automatically when needed, as a side-effect of CREATE TABLE, CREATE INDEX, and ALTER TABLE statements.

When you drop an index, InnoDB checks if the index is used for checking a foreign key constraint. It is still OK to drop the index if there is another index that can be used to enforce the same constraint. InnoDB prevents you from dropping the last index that can enforce a particular referential constraint.

关于mysql - MY SQL 外键约束错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28308755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com