Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rollback the effect of last executed mysql query

Tags:

mysql

rollback

I just ran a command

update sometable set col = '1';

by mistake without specifying the where condition. Is it possible to recover the previous version of the table?

like image 376
Rohit Banga Avatar asked May 15 '10 07:05

Rohit Banga


People also ask

How do I rollback the last query in SQL?

You can see that the syntax of the rollback SQL statement is simple. You just have to write the statement ROLLBACK TRANSACTION, followed by the name of the transaction that you want to rollback.

How do I rollback a MySQL query?

A COMMIT or ROLLBACK statement ends the current transaction and a new one starts. If a session that has autocommit disabled ends without explicitly committing the final transaction, MySQL rolls back that transaction.

How do I undo an action in MySQL?

At any point, you may undo all of the statements for the transaction by entering the following SQL statements: ROLLBACK; 5.


1 Answers

Unless you...

  1. Started a transaction before running the query, and...
  2. Didn't already commit the transaction

...then no, you're out of luck, barring any backups of previous versions of the database you might have made yourself.

(If you don't use transactions when manually entering queries, you might want to in the future to prevent headaches like the one you probably have now. They're invaluable for mitigating the realized-5-seconds-later kind of mistake.)

like image 108
Amber Avatar answered Sep 20 '22 09:09

Amber