如何重命名mysql中的外键?
我们刚刚在一个大表上完成了一个长时间运行的迁移,并最终在我们的conversation_tags表上得到了以下约束:
CONSTRAINT `conversation_tags_ibfk_1` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`)
不幸的是,某处有一个错误,因为我们想要的是:
CONSTRAINT `fk_conversation_tags_tags` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`)
删除并重新添加约束意味着另外两个长查询。有没有办法在单个查询中重命名约束?
We've just completed a long-running migration on a large table, and ended up with the following constraint on our conversation_tags table:
CONSTRAINT `conversation_tags_ibfk_1` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`)
Unfortunately, there was a bug somewhere, because what we wanted was:
CONSTRAINT `fk_conversation_tags_tags` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`)
Dropping and re-adding the constraint would mean another two long queries. Is there any way to rename the constraint in a single query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自文档:
这样,您可以将删除和重新创建合并到一个查询中,这应该比删除约束并在两个查询中创建它更快:
From the documentation:
This way you can combine the drop and recreate into one query, and that should be faster than dropping the constraint and creating it in two queries:
抱歉,约束只能在 mySQL 中删除并重新附加
I'm sorry, but constraints can only be dropped and re-attacched in mySQL
该功能似乎在 mysql ALTER TABLE 语法中不可用。
但是 Oracle 支持它。
The feature does not seems to be available in mysql ALTER TABLE syntax.
However it is supported for Oracle.
请参考我的答案MySQL术语“约束”与“外部”键”差异? 了解为什么约束名称与您想要的不同。
然而,MySQL 没有重命名约束功能,因此需要使用所需的名称 DROP 和 ADD FK 。
https://dev.mysql.com/doc/refman/ 5.5/en/alter-table.html
Please refer to my answer in MySQL terminology "constraints" vs "foreign keys" difference? to understand why the constraint name was different than what you desired.
However, MySQL doesnot have rename constraint feature and hence need to DROP and ADD FK with desired name .
https://dev.mysql.com/doc/refman/5.5/en/alter-table.html