반응형
모든 사용자 테이블을 잘라내는 방법은 무엇입니까?
오라클에서 모든 사용자 테이블을 자를 수 있는 방법은 무엇입니까?테이블 제약 조건에 문제가 있습니다.
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
반응형
'programing' 카테고리의 다른 글
| Python에서 현재 모듈의 속성에 대한 참조를 얻는 방법 (0) | 2023.07.20 |
|---|---|
| Oracle SQL Developer에서 관계를 볼 수 있는 방법이 있습니까? (0) | 2023.07.20 |
| $in을 사용한 대소문자 구분 안 함 검색 (0) | 2023.07.15 |
| "LINQ 표현식 노드 유형 'Invoke'는 LINQ to Entities에서 지원되지 않습니다." - 스텀핑! (0) | 2023.07.15 |
| 정렬된 결과 집합의 위치를 기준으로 mysql 테이블에서 레코드에 대한 단일 행 번호 가져오기 (0) | 2023.07.15 |