Search code examples
mysqlrollback

MySQL "Error Code 1305: SAVEPOINT a does not exist"


I just started learning MySQL and I just got introduced to savepoints and rollbacks.

I did this:

START TRANSACTION;

CREATE TABLE test (
    c1 int,
    c2 varchar(10)
);

INSERT INTO test VALUES ('1', 'LINHA1');

SAVEPOINT A;

INSERT INTO test VALUES ('2', 'LINHA2');

ROLLBACK TO SAVEPOINT A;

When I run "rollback to savepoint A", it says "SAVEPOINT a does not exist".


Solution

  • In mysql, DDL (such as CREATE TABLE) can't be inside a transaction. Your CREATE TABLE is ending your transaction, and every statement thereafter is auto-committed, so there is no savepoint A by the time you do the ROLLBACK.