programing

모든 사용자 테이블을 잘라내는 방법은 무엇입니까?

mailnote 2023. 7. 20. 22:04
반응형

모든 사용자 테이블을 잘라내는 방법은 무엇입니까?

오라클에서 모든 사용자 테이블을 자를 수 있는 방법은 무엇입니까?테이블 제약 조건에 문제가 있습니다.

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

종속성이 존재하기 때문에 제약 조건을 제거할 수 없는 경우(이 제약 조건에 종속된 외래 키 형식 - 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;

변수 필요 없음

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

K에 대하여

위의 스크립트를 실패하게 만드는 특수 제약 조건이 있는 경우 개선 사항:

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

출력한 다음 원하는 것을 실행할 수 있습니다.

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;

언급URL : https://stackoverflow.com/questions/1696738/how-to-truncate-all-user-tables

반응형