我有一个包含多个表的数据库。许多表的字段具有外键约束。我想截断表,然后用新数据重新填充它们,并且我还想删除外键,因为某些关系已经改变。基本上,我想再次从头开始构建 FK 约束。如何从所有表中删除当前的 FK 约束?
您可以使用 information_schema。
看看这个页面
http://dev.mysql.com/doc/refman/5.0/en/key-column-usage-table.html http://dev.mysql.com/doc/refman/5.0/en/key-column-usage-table.html
select concat('alter table ',table_name,' drop foreign key ',constraint_name,';')
from information_schema.key_column_usage
where constraint_schema = 'your_db' and referenced_table_name = 'table_name';
然后运行生成的输出。
您可以执行类似的操作来截断所有表。
select concat('truncate ',table_name,';')
from information_schema.tables
where table_schema = 'your_db' and table_type = 'base table'
这将截断指定数据库中的所有表。所以要小心使用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)