【问题标题】:How to truncate all user tables?如何截断所有用户表?
【发布时间】:2010-12-14 09:09:32
【问题描述】:

如何在 oracle 中截断所有用户表?我有表约束的问题。

【问题讨论】:

    标签: oracle constraints truncate


    【解决方案1】:

    改进了上述脚本,以防您因为存在依赖项而无法删除约束(以依赖于此约束的外键的形式 - ORA-02297。)并通过打印所有(禁用、截断和启用)语句.

    set serveroutput on;
    
    declare
    
    begin
    
    for c1 in (select y1.table_name, y1.constraint_name from user_constraints y1, user_tables x1 where x1.table_name = y1.table_name order by y1.r_constraint_name nulls last) 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
            dbms_output.put_line('truncate table '||t1.table_name || ';');    
            execute immediate ('truncate table '||t1.table_name);
        end;
    end loop;
    
    for c2 in (select y2.table_name, y2.constraint_name from user_constraints y2, user_tables x2 where x2.table_name = y2.table_name order by y2.r_constraint_name nulls first) loop
        begin
            dbms_output.put_line('alter table '||c2.table_name||' enable constraint '||c2.constraint_name || ';');        
            execute immediate ('alter table '||c2.table_name||' enable constraint '||c2.constraint_name);
        end;
    end loop;
    
    end;
    

    【讨论】:

    • 请注意,如果在其他架构中存在引用用户架构中的表的依赖表,这将不起作用。
    【解决方案2】:

    改进以防您遇到导致上述脚本失败的特殊限制:

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

    【讨论】:

    • 遗憾的是,约束是按顺序“约束”的。您可以通过在“禁用”命令的末尾添加“级联”来修复约束的停用。但很可能您将无法自动激活约束。
    【解决方案3】:

    你可以输出,然后执行你喜欢的:

    set serveroutput on;
    
    begin
    
    for r in (select table_name from user_tables) loop
    
        dbms_output.put_line('truncate table ' || r.table_name);
    
      end loop;
    
    end;
    

    【讨论】:

      【解决方案4】:
      declare
      
      begin
      
      for c1 in (select table_name, constraint_name from user_constraints) loop
          begin
              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;
      /
      

      【讨论】:

      • 请注意,如果在其他架构中存在引用用户架构中的表的依赖表,这将不起作用。
      【解决方案5】:

      不需要变量

      begin
        for r in (select table_name from user_tables) loop
          execute immediate 'truncate table ' || r.table_name;
        end loop;
      end;
      

      问候 克

      【讨论】:

      • @APC,我想当一个表被截断时,它不会影响表的任何索引、触发器或依赖项?
      • @LyuboslavKarmidzhanov - 我说“约束”:如果有外键引用的依赖表,我们不能截断表。
      猜你喜欢
      • 2015-01-18
      • 2011-08-27
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 2010-09-14
      • 1970-01-01
      • 2013-09-25
      相关资源
      最近更新 更多