Tag Archives: ROLLBACK

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


COMMIT, ROLLBACK and SAVEPOINT

TRANSACTION

A transaction is a set of SQL statements which Oracle treats as a Single Unit i.e. all the statements should execute successfully or none of the statements should execute.

Transaction Control Language (TCL)

Transaction Control Language (TCL) statements manage changes made by Data Manipulation Language (DML) statements.It allows statements to be grouped together into logical transactions

To control transactions Oracle does not made permanent any DML statements unless you commit it. If you don’t commit the transaction and power goes off or system crashes then the transaction is roll backed.

TCL Statements available in Oracle are

COMMIT : Make changes done in transaction permanent.

ROLLBACK: Rollbacks the state of database to the last commit point.

SAVEPOINT: Use to specify a point in transaction to which later you can rollback.

COMMIT

To make the changes done in a transaction permanent issues the COMMIT statement.

The syntax of COMMIT Statement is
COMMIT [WORK] [COMMENT ‘Your Comments’];

WORK: is optional.
COMMENT: is also optional; specify this if you want to identify this transaction in data dictionary DBA_2PC_PENDING.

Example:

INSERT INTO EMP (EMPNO, ENAME, SAL) VALUES (101,’ABID’, 2300);
COMMIT;

ROLLBACK

To rollback the changes done in a transaction give rollback statement. Rollback restore the state of the database to the last commit point.

Example:

DELETE FROM EMP;
ROLLBACK; --undo the changes

SAVEPOINT

Specify a point in a transaction to which later you can roll back.

Example:

INSERT INTO EMP (EMPNO,ENAME,SAL) VALUES (109,’SAMI’,3000);
SAVEPOINT A;
INSERT INTO DEPT VALUES (10,’SALES’,’HYD’);
SAVEPOINT B;
INSERT INTO SALGRADE VALUES (‘III’,9000,12000);

Now if you give

ROLLBACK TO A;

Then row from SALGRADE table and DEPT will be roll backed. Now you can COMMIT the row inserted into EMP table or ROLLBACK the transaction.

If you give

ROLLBACK TO B;

Then row inserted into SALGRADE table will be roll backed. Now you can COMMIT the row inserted into DEPT table and EMP table or ROLLBACK to SAVEPOINT A or completely roll backed the transaction.

If you give

ROLLBACK;

Then the whole transactions are roll backed.

If you give

COMMIT;

Then the whole transaction is committed and all SAVEPOINT are removed.

Reference: http://www.oracle-dba-online.com/sql/commit_rollback_savepoint.htm