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:

 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 do not use the InnoDB storage engine, MySQL 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.





Sams Teach Yourself MySQL in 10 Minutes
Sams Teach Yourself MySQL in 10 Minutes
ISBN: 0672328631
EAN: 2147483647
Year: 2006
Pages: 165
Authors: Chris Newman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net