Empty a relational database schema

前端 未结 2 807
后悔当初
后悔当初 2021-01-12 15:14

I have a database which I have backed up . Now i am trying to delete all the content from the original db and restore it to it\'s empty state. since it\'s a relational db it

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-12 16:15

    The easiest way to do this is probably to disable foreign key checks, then truncate the tables. Since foreign keys are disabled, the order in which you truncate the tables does not matter.

    set foreign_key_checks = 0;
    truncate table parent;
    truncate table child;
    truncate table ...
    

    You can even use the information_schema to generate the truncate table statements for you. Something like this:

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

提交回复
热议问题