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 statement—remember to always include the WHERE clause—if it occurs within a transaction:

mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> DELETE FROM products;
Query OK, 3 rows affected (0.03 sec)
mysql> 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 ...

Get Sams Teach Yourself MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.