How do I rename a foreign key in mysql?

前端 未结 4 2076
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 15:27

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


        
4条回答
  •  离开以前
    2020-12-14 15:42

    From the documentation:

    Multiple ADD, ALTER, DROP, and CHANGE clauses are permitted in a single ALTER TABLE statement, separated by commas. This is a MySQL extension to standard SQL, which permits only one of each clause per ALTER TABLE statement.

    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:

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

提交回复
热议问题