且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从所有表中删除外键关系

更新时间:2023-02-02 22:54:32

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

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';

then run the output generated.

You can do something similar in order to truncate all tables.

select concat('truncate ',table_name,';') 
from information_schema.tables
where table_schema = 'your_db' and table_type = 'base table'

this one will truncate all tables within the specified database. So use it with care.