What is save point in Oracle database?

Save points are used to divide a transaction into smaller parts. It allows rolling back of a transaction. Maximum five save points are allowed. It is used to save our data, whenever you encounter an error you can roll back from the point where you save your SAVEPOINT.

In Oracle database, a savepoint is a point within a transaction to which you can later roll back. It allows you to divide a transaction into smaller units and roll back to a specific savepoint without rolling back the entire transaction.

Here’s a breakdown of the key concepts related to savepoints:

  1. Transaction: A series of one or more SQL statements executed as a single unit of work.
  2. Savepoint: A named point in a transaction to which you can later roll back. It represents a specific point in the transaction to which you want to be able to roll back.
  3. ROLLBACK TO SAVEPOINT: This SQL statement is used to roll back a transaction to the specified savepoint. It undoes all changes made in the transaction since the savepoint was established.

Here’s an example of using savepoints in Oracle:

-- Start a transaction
BEGIN
-- Insert some data
INSERT INTO your_table (column1, column2) VALUES (value1, value2);
— Create a savepoint
SAVEPOINT my_savepoint;

— Update some data
UPDATE your_table SET column1 = new_value1 WHERE column2 = some_condition;

— If something goes wrong, roll back to the savepoint
ROLLBACK TO SAVEPOINT my_savepoint;

— Continue with the transaction
COMMIT;
END;

In this example, if the update statement encounters an issue, you can roll back to the savepoint “my_savepoint,” undoing only the changes made after that savepoint. This allows for more granular control over transactions.