How To Use Commit and Rollback Command in Oracle
Introduction
Commit command is used to makes the changes permanent to the database. The user can continue with any number to insert, updates, deletes and still can undo the work, using Rollback command.
SQL * plus has the facility to automatically commit all the work, without explicitly, using Commit command.
SET AUTO COMMIT ON: Enable auto commit feature.
Implicit commit
After completion of any Data Manipulation Language (DML) statement, if the user experiences such as hardware failure and no explicit commit made, Oracle will automatically Rollback all un-committed work.
Thus, we learnt, Commit command is used to makes the changes permanent to the database. Rollback command is used to discard parts or all the work; the user has done in the current transaction. We learnt, how to use these commands with the examples in Oracle.
In this article, you will learn, how to use commit and rollback command in Oracle with the syntax and the examples.
- Commit command is used to make changes to the data (insert, updates, deletes) permanent.
- Rollback command is used to discard the parts or all the work; the user has done in the current transaction.
- Save points statements are used to discard or commit all the changes up to a point.
Commit command is used to makes the changes permanent to the database. The user can continue with any number to insert, updates, deletes and still can undo the work, using Rollback command.
SQL * plus has the facility to automatically commit all the work, without explicitly, using Commit command.
SET AUTO COMMIT ON: Enable auto commit feature.
SET AUTO COMMIT OFF: Is the default and disable the automatic committing.
The actions, which will force a commit to occur, even without, using Commit command are existing in SQL * plus, which helps in creating any object, granting or revoking resources, altering objects, connecting or disconnecting from Oracle.
- Select * from Customer;
Example
- Update customer set customer_name = ‘scott’ where customer_id = 105;
- Select * from customer;
- Commit;
Rollback
If the machine or database goes down, it cleans up the work; the next time the database is brought up.
Example
- Select * from customer;
- Insert Into customer
- (Customer_id, Customer_name, Product_code, Quantity)
- values
- (109, ‘Tiger’, ‘AB1’, 20);
- ROLLBACK;
- Select * from customer;
Save points
Save point identifies a point in a transaction, which one can later rollback with Rollback statement. It is helpful, when a transaction contains a large number of SQL statements and the user wants to commit only once, when all are done. If required, one can roll back to a particular transaction. It works in a Last-In-First-out (LIFO) manner.
Example
- Select * from customer;
- Insert Into customer
- (Customer_id, Customer_name, Product_code, Quantity)
- values
- (110, ‘Roy’, ‘DC1’, 10);
- Savepoint s1;
- update customer set customer_id = 109 where customer_name = 'Roy';
- Rollback to savepoint s1;
Summary