How to truncate all user tables?

前端 未结 5 2410
抹茶落季
抹茶落季 2021-02-19 08:01

How can I truncate all user table in oracle? I have problem with tables constraints.

5条回答
  •  终归单人心
    2021-02-19 08:22

    Improvement in case you have special constraints which make above script fail:

    set serveroutput on;
    
    declare
    
    begin
    
    for c1 in (select y.table_name, y.constraint_name from user_constraints y, user_tables x where x.table_name = y.table_name) loop
        begin
            dbms_output.put_line('alter table '||c1.table_name||' disable constraint '||c1.constraint_name || ';');
            execute immediate  ('alter table '||c1.table_name||' disable constraint '||c1.constraint_name);
        end;
    end loop;
    
    for t1 in (select table_name from user_tables) loop
        begin
            execute immediate ('truncate table '||t1.table_name);
        end;
    end loop;
    
    for c2 in (select table_name, constraint_name from user_constraints) loop
        begin
            execute immediate ('alter table '||c2.table_name||' enable constraint '||c2.constraint_name);
        end;
    end loop;
    
    end;
    /
    

提交回复
热议问题