问题描述
外键使我在修改数据库结构以满足新要求时遇到太多问题-我想修改主键,但是当外键引用有问题的表时,我似乎无法(我想是因为MySQL删除了该表,并且重新创建).
Foreign keys are causing me too many problems modifying an database structure to meet new requirements - I want to modify primary keys but it seems I can't when foreign keys reference the table in question (I think because MySQL drops the table and re-creates).
因此,当我在数据库上工作时,我想简单地删除所有外键并在以后重新创建它们.有没有一种整洁的方法?
So while I work on the DB, I'd like to simply remove all the foreign keys and re-create them later. Is there a neat way to do so?
推荐答案
您可以在要创建的任何Alter Table语句之前简单地发出以下命令:
You can simply issue the following command before any Alter Table statements you are going to make:
SET foreign_key_checks = 0;
这将关闭数据库连接的外键约束检查.然后,您可以进行更改,而不必担心约束.
This will turn off foreign key constraint checks for your database connection. You can then make your changes without needing to worry about constraints.
完成后,请不要忘记发出:
After you are done, don't forget to issue:
SET foreign_key_checks = 1;
要重新打开它们.
请注意,这仍然不允许您创建新的外键约束,因为列数据类型不匹配,该约束将失败.
Note that this will still not allow you to create a new foreign key constraint that would fail because the column data types don't match.
这篇关于删除MYSQL数据库中的所有外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!