简体   繁体   English

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

[英]How do I delete a foreign key constraint that doesn't exist?

I ran across this problem to day in the process of trying to change my PSA_Landing.tblSubsDetails SubmissionID field (Which is currently the PK) to an Auto Increment field for some quick testing of something unrelated. 在尝试将我的PSA_Landing.tblSubsDetails SubmissionID字段(当前为PK)更改为“自动增量”字段的过程中,我每天遇到此问题,以便对不相关的内容进行一些快速测试。

When trying to run: 尝试运行时:

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

I get error: 我得到错误:

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

The problem being... I don't have a table PSA_Landing.tblSubsStatus... I renamed that to PSA_Landing.tblSubmissionStatus a while back. 问题是...我没有表PSA_Landing.tblSubsStatus ...我不久前将其重命名为PSA_Landing.tblSubmissionStatus。 It also does not have a FK on it. 它还没有FK。

When trying to alter tblSubmissionStatus BACK to tblSubsStatus (just to see if it helps) I get an Error 1025: 当尝试将tblSubmissionStatus BACK更改为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`

So I then ran: 于是我跑了:

SHOW ENGINE innodb STATUS

And find this: 并找到这个:

 ------------------------
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

Finally this lead me to try: 最后,这使我尝试:

alter table PSA_Landing.tblSubsStatus
drop foreign key fk_StatusSubmissionID

Which expectedly gives: 可以预期:

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

And out of desperation I tried this: 出于绝望,我尝试了以下方法:

alter table PSA_Landing.tblSubmissionStatus
drop foreign key fk_StatusSubmissionID

Which yields: 产生:

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

So in short... 简而言之...

HOW do I delete constraint fk_StatusSubmissionId from table PSA_Landing.tblSubsStatus when that table does not exist?? 当该表不存在时,如何从表PSA_Landing.tblSubsStatus中删除约束fk_StatusSubmissionId?

SET @@FOREIGN_KEY_CHECKS = 0; causes InnoDB to disable referential integrity checking on DML and to permit some invalid DDL operations related to foreign keys, such as creating a table with a foreign key constraint referencing a table that doesn't yet exist, or dropping a table referenced by a foreign key, while still disallowing blatantly invalid requests, like REFERENCES referring to a column with a mismatched data type, or malformed foreign key definitions. 导致InnoDB禁用DML上的参照完整性检查,并允许一些与外键相关的无效DDL操作,例如创建具有外键约束的表以引用尚不存在的表,或删除由外键引用的表,同时仍然禁止明显无效的请求,例如REFERENCES引用的数据类型不匹配的列或外键定义格式错误。

This statement operates at the session level, so it doesn't disable foreign key constraint enforcement for the entire server. 该语句在会话级别上运行,因此不会禁用整个服务器的外键约束实施。 It is normally used only during the process of restoring from backups (the statement is injected into dump files by mysqldump and periodically executed during the reload operation) and is operationally necessary since it's not always possible to resolve circular dependencies in foreign keys (and mysqldump doesn't see a need to -- tables are dumped in lexical order). 它通常仅在从备份还原的过程中使用(该语句由mysqldump注入到转储文件中,并在重载操作期间定期执行),并且由于并非总是能够解决外键中的循环依赖关系而在操作上是必需的(而mysqldump不会看不到需要-表以词法顺序转储)。

In this case, the solution was to recreate and then drop the phantom referencing table, while FOREIGN_KEY_CHECKS was disabled. 在这种情况下,解决方案是重新创建然后删除幻影引用表,同时禁用FOREIGN_KEY_CHECKS

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM