SAVEPOINT is a transactional command in MySQL that allows you to mark a point within a transaction. You can roll back to this point without undoing the entire transaction.
A SAVEPOINT creates a named checkpoint in the current transaction. You can roll back to this checkpoint anytime before committing.
-- Create a savepoint inside a transaction
SAVEPOINT sp1;
-- Start transaction and create savepoints
START TRANSACTION;
INSERT INTO orders VALUES (1,'Laptop');
SAVEPOINT sp1;
INSERT INTO orders VALUES (2,'Mobile');
SAVEPOINT sp2;
INSERT INTO orders VALUES (3,'Tablet');
If an error occurs after inserting the third record, you can roll back only to sp2 or sp1 instead of cancelling the entire transaction.
-- Rollback to a specific savepoint
ROLLBACK TO sp2;