Skip to content

SAVEPOINT

START TRANSACTION;
INSERT INTO employees (first_name, last_name) VALUES ('John', 'Doe');
SAVEPOINT my_savepoint;
INSERT INTO employees (first_name, last_name) VALUES ('Jane', 'Doe');
ROLLBACK TO my_savepoint;
COMMIT;

The output for the above code is not directly visible to the user as it affects the database state and not return a result set.

The above SQL transaction begins with START TRANSACTION;. Two INSERT statements are then executed.

SAVEPOINT my_savepoint; creates a savepoint that we can later roll back to.

The second INSERT statement is executed after the savepoint.

ROLLBACK TO my_savepoint; undoes the operations done after the savepoint was created. In this case, Jane Doe would not be inserted into the ‘employees’ table as that operation is rolled back.

The COMMIT; statement makes permanent all changes made to the database, up to the last committed savepoint which is ‘my_savepoint’.

In the end, only John Doe would be inserted into the ‘employees’ table as the insert operation for Jane Doe was rolled back.