如何在mysql中重命名外键? [英] How do I rename a foreign key in mysql?

查看:555
本文介绍了如何在mysql中重命名外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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?

推荐答案

您可以将drop和recreate组合成一个查询,这应该比删除约束并在两个查询中创建它快:

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:

ALTER TABLE conversation_tags
DROP FOREIGN KEY 'conversation_tags_ibfk_1',
ADD CONSTRAINT `fk_conversation_tags_tags` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`);

这篇关于如何在mysql中重命名外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆