最简单的方法是禁用外键检查,然后截断表.由于禁用了外键,因此截断表的顺序无关紧要.
set foreign_key_checks = 0;
truncate table parent;
truncate table child;
truncate table ...
Run Code Online (Sandbox Code Playgroud)
您甚至可以使用information_schema为您生成truncate table语句.像这样的东西:
select concat('truncate table ',table_schema,'.',table_name,';') as sql_stmt
from information_schema.tables
where table_schema = 'your_schema_name'
and table_type = 'base table';
Run Code Online (Sandbox Code Playgroud)