Rolling Back a Transaction

You can also end a transaction without committing the changes with the ROLLBACK command. The result is that the database state is rolled back to how it was before you issued the START TRANSACTION command.

The following example shows how you can recover from a disastrously wrong DELETE statementremember to always include the WHERE clauseif it occurs within a transaction:

MariaDB> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)

MariaDB> DELETE FROM products;
Query OK, 3 rows affected (0.03 sec)

MariaDB> ROLLBACK;
Query OK, 0 rows affected (0.01 sec)

If you then query the products table, you will see that it still contains records.

Transactional Table Handlers

If you try to use transactions with tables that do not use the InnoDB storage engine, MariaDB will not give an error when you START TRANSACTION.

In the previous example, there would be a warning that ROLLBACK could not be used, but at this point it is already too late to undo the DELETE command.


Previous Page Next Page