Tag Archives: FLASHBACK

TRUNCATE, DELETE and DROP

Data Definition Language (DDL) statements are used to define the database structure or schema.

Data Manipulation Language (DML) statements are used for managing data within schema objects.

DELETE

It is a DML statement; it is used to remove the data from the table.

It also generates REDO information and deleted data can be ROLLBACK

It provides the facility of conditional-based deletion, a WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed.

This operation will cause all DELETE triggers on the table to fire.

SQL> SELECT COUNT(*) FROM EMP;

COUNT(*)
----------
14

SQL> DELETE FROM EMP WHERE JOB = 'CLERK';

4 rows deleted.

SQL> COMMIT;

Commit complete.

SQL> SELECT COUNT(*) FROM EMP;

COUNT(*)
----------
10

After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it.

TRUNCATE

It is a DDL statement; it is used to removes all rows from a table permanently and cannot be rolled back.

TRUNCATE drops the storage held by the table. It is the faster command because it directly drops the storage (dropped storage can be use by this table again or some other table).

SQL> TRUNCATE TABLE EMP;

Table truncated.

SQL> SELECT COUNT(*) FROM EMP;

COUNT(*)
----------
0

In case of TRUNCATE,

  • Delete the contents only not the structure.
  • No Trigger get fired,
  • No WHERE clause used
  • No undo space used.

DROP

This is the DDL statement; it is used to removes a table from the database.

It removes the entire rows and information along with structure. It also removes all information about the table from data dictionary

All the tables’ rows, indexes and privileges will also be removed.

SQL> DROP TABLE EMP;

Table dropped.

SQL> SELECT * FROM EMP;

SELECT * FROM EMP
*
ERROR at line 1:
ORA-00942: table or view does not exist

In case of DROP

  • No Trigger get fired.

Note: We can not recover the table before Oracle 10g. But Oracle 10g provides the command to recover it by using the command (FLASHBACK), From Oracle 10g a table can be “undropped”. Example:

SQL> FLASHBACK TABLE EMP TO BEFORE DROP;

Flashback complete.

Discussion

When you type DELETE all the data get copied into the Rollback TABLESPACE first and then delete operation get performed. That’s why when you type ROLLBACK after deleting a table; you can get back the data (The system gets it for you from the Rollback TABLESPACE). All this process takes time but when you type TRUNCATE, it removes data directly without copying it into the Rollback TABLESPACE. TRUNCATE is faster. Once you TRUNCATE you can’t get back the data.

DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command.

DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.

Reference: http://www.orafaq.com/faq/difference_between_truncate_delete_and_drop_commands