DROP (4)

여러가지 이유로 테이블을 삭제할 상황이 있다.

Drop table로 하나하나 지우기에 너무 양이 많을때 테이블 전체를 한번에 삭제하는 방법이 있다.

그 방법을 아래에 소개하겠다.

 

먼저 sql에 접속한 후

 

SQL> Begin

2    for c in (select table_name from user_tables) loop

3    execute immediate ('drop table '||c.table_name||' cascade constraints');

4    end loop;

5    End;

6    . <- 쩜 찍어야 합니다.

엔터

 

이렇게 입력하면

 

SQL>

 

다시 이렇게 아무것도 안뜨는 명령어가 뜬다.

 

SQL> run

을 실행한다.

실행하면 아래 구문이 나올것이다.

 

1    Begin

 

2    for c in (select table_name from user_tables) loop

3    execute immediate ('drop table '||c.table_name||' cascade constraints');

4    end loop;

5    End;

PL/SQL procedure successfully completed.<--메시지가 나오면 성공

 

그 후에

SQL> purge recyclebin; 

Recyclebin purged.<===휴지통이 비워졌다는 문구가 나온다.

 

그후 select * from tab;을 해보면 깨끗히 지워진 테이블을 볼 수 있을것이다.




출처 : http://munjee.tistory.com/16

여러개의 테이블을 한꺼번에 삭제하고 싶은데 like 조건절을 쓸 수가 없다.

뭐 결국 테이블 하나하나를 각각 삭제해줘야 한다는 건데....그렇다고 테이블이 수 십개가 넘는데 이걸 다 손으로 타이핑하기란 짜증하는 일이 아닐 수 없다.


이럴 때 다음 쿼리로 특정 사용자의 전체 테이블을 삭제 할 수 있는 쿼리를 얻어올 수 있다.

 

SELECT 'DROP TABLE ' || TABLE_NAME || ';' FROM ALL_TABLES WHERE OWNER = 'user1';


조건절은 마음대로 바꿀 수 있고 위에서 사용한 ALL_TABLES는 DBA권한이 있어야 접근이 가능하다.


따라서 일반 사용자이면서 자신이 소유한 테이블을 삭제할 때는


ALL_TABLES 대신 USER_TABLES 에서 조회한 테이블명을 사용하면 된다.


SELECT 'DROP TABLE ' || TABLE_NAME || ';' FROM USER_TABLES WHERE OWNER = 'user2';


테이블을 삭제하려면
SQL > drop table 테이블명;

하면 됩니다. 그런데 사실 이게 물리적으로 공간을 다시 내주는 것이 아닙니다.
오라클의 drop table관련 문서를 보면 테이블을 drop할 때 purge라는 옵션을 주지 않으면 휴지통으로 넣기만 하고 물리적인 공간은 그대로 가지고 있게 됩니다. 윈도우의 휴지통 개념과 동일하다고 보면 됩니다.

오라클이 10g에서 새롭게 추가한 '휴지통' 이라는 기능.

테이블을 DROP 하더라도 언제던지 복원할 수 있게 하고자 하는 것이고요,
윈도우의 휴지통과 같다고 보시면 됩니다.

 

휴지통에 들은 테이블을 조회.
SQL> show recyclebin;

 

휴지통의 모든 내용이 비워집니다.
SQL> purge recyclebin;

 

삭제된 테이블을 되살리고 싶다면
SQL> flashback table 테이블명 to before drop;

 

만약, 특정 테이블을 휴지통에 남기지 않고 모두 삭제하려면..
SQL> drop table 테이블명 purge;

 

purge문 없이 그냥 drop 한 후에는
SQL> purge table 테이블명;


💻 Programming/Oracle 11g

[SQL] Truncate vs Drop vs Delete

DELETE

The DELETE command is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire.

 

TRUNCATE

TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUCATE is faster and doesn't use as much undo space as a DELETE.

 

DROP

The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back.  

 

 

* DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command. Therefore DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.