从所有表中删除外键关系
我有一个包含多个表的数据库。许多表的字段具有外键约束。我想截断表,然后用新数据重新填充它们,并且我还想删除外键,因为某些关系已经改变。基本上,我想再次从头开始构建 FK 约束。如何从所有表中删除当前的 FK 约束?
I have a database that has several tables. Many of the tables have fields with foreign key constraints. I want to truncate the tables and then repopulate them with new data, and I also want to take off the foreign keys, as some of the relationships have changed. basically, I want to build the FK constraints up from scratch again. How can I remove the current FK constraints from all tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 information_schema。
看看这个页面
http://dev .mysql.com/doc/refman/5.0/en/key-column-usage-table.html
然后运行生成的输出。
您可以执行类似的操作来截断所有表。
这将截断指定数据库中的所有表。所以要小心使用。
You can play with the information_schema.
Take a look at this page
http://dev.mysql.com/doc/refman/5.0/en/key-column-usage-table.html
then run the output generated.
You can do something similar in order to truncate all tables.
this one will truncate all tables within the specified database. So use it with care.
我猜你已经找到了一个解决方案,因为这篇文章已经有六个月了,但我最近不得不想出一个脚本来删除 MySQL 中特定表的外键约束,这样可能会帮助其他有同样情况的人:
要运行它,只需使用:
如果您不想保留该过程:
当然,如果您想删除表中的所有 FK 约束,您可以删除 refcolumn 参数和每个 where 子句的最后一行。
I"m guessing you already found a solution, since this post is six months old, but I recently had to come up with a script to drop foreign key constraints to a particular table in MySQL, so that may help anyone else in the same boat:
To run it, simply use:
And if you don't want to keep the procedure around:
Of course if you want to drop all FK constraints to the table, you can remove the refcolumn parameter and the last line of each where clause.