Oracle Database Certified Associate (OCA) 1 minute read

Perintah TRUNCATE TABLE yaitu untuk menghapus semua data dari table, by default oracle akan deallocate semua space yang digunakan dalam disk. Mungkin temen-temen bingung apa bedanya dengan perintah delete

Yap sebetulnya sama saja tujuannya adalah menghapus data, tapi disini bedanya adalah pemakaian space setelah di delete atau truncate. contohnya seperti berikut:

Berikut hasilnya:

SQL> create table deleted_emp as
select *
from EMPLOYEES;
Table created.

create table TRUNCATE_EMP as
select *
from EMPLOYEES;
Table created.

SQL> analyze table deleted_emp compute statistics;

Table analyzed.

SQL> analyze table truncate_emp compute statistics;

Table analyzed.

SQL> select TABLE_NAME, NUM_ROWS, ((BLOCKS * 8192) / 1024) as kb
from user_tables
where TABLE_NAME in ('DELETED_EMP', 'TRUNCATE_EMP');

TABLE_NAME              NUM_ROWS         KB
---------------------   ---------- ----------
DELETED_EMP             107         40
TRUNCATE_EMP            107         40


SQL> delete from deleted_emp;
truncate table TRUNCATE_EMP;
107 rows deleted.

SQL>

Table truncated.

SQL> commit;

Commit complete.

SQL> analyze table deleted_emp compute statistics;
Table analyzed.

SQL> analyze table truncate_emp compute statistics;
Table analyzed.

SQL> select TABLE_NAME, NUM_ROWS, ((BLOCKS * 8192) / 1024) as kb
from user_tables
where TABLE_NAME in ('DELETED_EMP', 'TRUNCATE_EMP');

TABLE_NAME            NUM_ROWS         KB
--------------------- ---------- ----------
DELETED_EMP           0          40
TRUNCATE_EMP          0           0