Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of ROLLBACK command in Oracle

I created table

SQL>CREATE TABLE Student
(
StudID         NUMBER(6),
StudName       VARCHAR2(25),
JoinDate       DATE
);   
Table created.

SQL>INSERT INTO Student
VALUES (123,'JOHN',SYSDATE);
1 row created.

SQL>DELETE Student;
1 row deleted.

How can I get back the row ? If I use

SQL>ROLLBACK;
Rollback complete.

But after that

SQL>SELECT * FROM Student;
no rows selected.

Why is this coming?

like image 694
bibhudash Avatar asked May 19 '13 07:05

bibhudash


People also ask

What is use of ROLLBACK command?

The ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database. This command can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued.

What is the use of ROLLBACK in SQL?

Rolls back an explicit or implicit transaction to the beginning of the transaction, or to a savepoint inside the transaction. You can use ROLLBACK TRANSACTION to erase all data modifications made from the start of the transaction or to a savepoint. It also frees resources held by the transaction.

What is ROLLBACK and COMMIT in Oracle?

COMMIT commits the current transaction, making its changes permanent. ROLLBACK rolls back the current transaction, canceling its changes. SET autocommit disables or enables the default autocommit mode for the current session.

How do I ROLLBACK an Oracle package?

create or replace package pckg as procedure prc (column in number) as begin insert into smth(column1) SELECT column1 FROM somewhere; end prc; end emp_pac; --ROLLBACK?? DECLARE column1 NUMBER; BEGIN SAVEPOINT savepnt; column1 := NULL; pckg.

What does rollback mean in Oracle?

ROLLBACK tells Oracle to roll back the entire transaction. In your case, both the INSERT and the DELETE are part of the same transaction so the ROLLBACK reverses both operations. That returns the database to the state it was in immediately following the CREATE TABLE statement.

What is the difference between commit and rollback in Oracle?

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.

What is the use of rollback in Salesforce?

The Rollback method rolls back all pending transactions within the specified connection. This method has no effect if a transaction has not begun. When a session-wide transaction is in progress, you can use this call to prematurely roll back the transactions for the specified connection.

What is the use of the rollback method?

The Rollback method rolls back all pending transactions within the specified session. The Rollback method is valid only when a transaction has been started. If a transaction has not been started, the use of the Rollback method results in an error.


1 Answers

This might be a bit confusing if you are not doing rollbacks on a regular basis. I put here mistakes that you are likely to make if not using SAVEPOINTS / ROLLBACK on a regular basis.

1. This is your case. We rollback all transaction to the beginning, ie. to the last (implicit) commit

enter image description here


2. Here we have SAVEPOINT but it does not matter, as it is not mentioned in ROLLBACK statment. So we are rolling back to the last commit, as before.

enter image description here

3. This is tricky. The rollback still goes to the last commit, as the syntax was not correct (but no error reported by the 11g database). Correct syntax is ROLLBACK TO <SAVEPOINT-NAME>:

enter image description here

4. Finally we have correct SYNTAX: ROLLBACK TO <SAVEPOINT-NAME>

enter image description here

Some readings: https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9021.htm

like image 185
Witold Kaczurba Avatar answered Sep 20 '22 13:09

Witold Kaczurba