简体   繁体   English

如何在mysql中删除外键?

[英]How to delete foreign key in mysql?

I have tried both the syntaxes: 我尝试了两种语法:

Alter Table bc DROP FOREIGN KEY STUD_ID;

It is giving the error: Can't DROP 'STUD_ID'; 它给出了错误:不能DROP'STUD_ID'; check that column/key exists 检查列/键是否存在

Alter Table bc DROP CONSTRAINT STUD_ID;

It is giving the error: 它给出了错误:

ERROR 1064 (42000): You have an error in your SQL syntax; 错误1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT STUD_ID' at line 1 检查与MySQL服务器版本对应的手册,以便在第1行的“CONSTRAINT STUD_ID”附近使用正确的语法

Suggest me the possible ways. 建议我可能的方法。

ALTER TABLE TableName DROP FOREIGN KEY ForeignKeyConstraintName;

希望这可以帮助 :)

Your first query works. 您的第一个查询有效。 It is telling you that there is no such key to drop. 它告诉你没有这样的钥匙掉下来。 This means your key has another name. 这意味着您的密钥有另一个名称。 It has not the same name as the column it indexes. 它与索引的列名称不同。 Run

show index from bc

to show all key names and then run your query again with the correct name 显示所有键名,然后使用正确的名称再次运行查询

Alter Table bc DROP FOREIGN KEY <STUD_ID_index_name>
alter table bc drop foreign key forconstraintname

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

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