gpt4 book ai didi

mysql - 如何删除不存在的外键约束?

转载 作者:行者123 更新时间:2023-11-29 03:26:43 24 4
gpt4 key购买 nike

我今天在尝试将我的 PSA_Landing.tblSubsDetails SubmissionID 字段(目前是 PK)更改为自动增量字段以快速测试不相关的内容时遇到了这个问题。

尝试运行时:

ALTER TABLE `PSA_Landing`.`tblSubsDetails` CHANGE COLUMN `SubmissionID`
`SubmissionID` INT(11) NOT NULL AUTO_INCREMENT ;

我得到错误:

ERROR 1833: Cannot change column 'SubmissionID': used in a foreign key 
constraint 'fk_StatusSubmissionId' of table 'PSA_Landing.tblSubsStatus'

问题是...我没有表 PSA_Landing.tblSubsStatus...我不久前将其重命名为 PSA_Landing.tblSubmissionStatus。它也没有 FK。

当试图将 tblSubmissionStatus 改回 tblSubsStatus(只是为了看看它是否有帮助)时,我收到错误 1025:

 Apply changes to tblSubsStatus Error 1025: Error on rename of
'./PSA_Landing/tblSubmissionStatus' to './PSA_Landing/tblSubsStatus' (errno:
150 - Foreign key constraint is incorrectly formed) SQL Statement: ALTER
TABLE `PSA_Landing`.`tblSubmissionStatus` RENAME TO `PSA_Landing`.`tblSubsStatus`

然后我跑了:

显示引擎 innodb 状态

找到这个:

 ------------------------
LATEST FOREIGN KEY ERROR
------------------------
2016-01-24 03:15:32 7fc4410fb700 Error in foreign key constraint of table PSA_Landing/tblSubsStatus:
there is no index in the table which would contain
the columns as the first columns, or the data types in the
table do not match the ones in the referenced table
or one of the ON ... SET NULL columns is declared NOT NULL. Constraint:
,
CONSTRAINT "fk_StatusSubmissionId" FOREIGN KEY ("fk_SubmissionId") REFERENCES "tblSubsDetails" ("SubmissionID") ON DELETE NO ACTION ON UPDATE NO ACTION

最后这让我尝试:

alter table PSA_Landing.tblSubsStatus
drop foreign key fk_StatusSubmissionID

预期会给出:

alter table PSA_Landing.tblSubsStatus  drop foreign key fk_StatusSubmissionID      
Error Code: 1146. Table 'PSA_Landing.tblSubsStatus' doesn't exist

出于绝望,我尝试了这个:

alter table PSA_Landing.tblSubmissionStatus
drop foreign key fk_StatusSubmissionID

产生:

Error Code: 1091. Can't DROP 'fk_StatusSubmissionID'; check that column/key exists  

所以简而言之......

当表 PSA_Landing.tblSubsStatus 不存在时,如何删除约束 fk_StatusSubmissionId?

最佳答案

SET @@FOREIGN_KEY_CHECKS = 0; 导致 InnoDB 禁用对 DML 的引用完整性检查并允许一些与外键相关的无效 DDL 操作,例如创建表使用外键约束引用尚不存在的表,或删除外键引用的表,同时仍然禁止公然无效的请求,例如 REFERENCES 引用具有不匹配数据类型的列,或格式错误的外键定义。

此语句在 session 级别运行,因此它不会禁用整个服务器的外键约束强制执行。它通常仅在从备份恢复过程中使用(该语句由 mysqldump 注入(inject)转储文件并在重新加载操作期间定期执行)并且在操作上是必需的,因为它并不总是能够解决循环依赖在外键中(mysqldump 认为没有必要——表按词法顺序转储)。

在这种情况下,解决方案是重新创建然后删除虚拟引用表,同时禁用 FOREIGN_KEY_CHECKS

http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_foreign_key_checks

关于mysql - 如何删除不存在的外键约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34972062/

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