Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo a mysql UPDATE command

Tags:

mysql

I was trying to do an update on the MySQL server and accidentally forgot to add an additional WHERE clause which was supposed to edit one row.

I now have 3500+ rows edited due to my error.

I may have a back up but I did a ton of work since the last backup and I just dont want to waste it all because of 1 bad query.

Please tell me there is something i can do to fix this.

like image 795
Naveen Avatar asked Jun 06 '12 20:06

Naveen


People also ask

Can you undo MySQL update?

MySQL Rollback statement allows you to rollback or undo one more statements that have been recently executed. For example, if you have accidentally deleted or updated rows, you can use MySQL rollback those statements and restore original databases.

How do I rollback a MySQL update?

To have possibility to ROLLBACK DML statements (like INSERT , UPDATE or DELETE queries) you should use transaction blocks: START TRANSACTION; UPDATE CUSTOMERS SET ADDRESS = 'Pune' WHERE ID = 6; -- and more DML queries COMMIT; -- or ROLLBACK; Since transaction was COMMIT ed it can not be rolled back.

How do I undo a command in MySQL?

Basically: If you're doing a transaction just do a rollback. Otherwise, you can't "undo" a MySQL query. Show activity on this post. For some instrutions, like ALTER TABLE, this is not possible with MySQL, even with transactions (1 and 2).

Can you undo a SQL update query?

Undo is called rollback in SQL. Once you've done a commit , you can't undo it without getting into restoring backups. Note that doing a rollback will undo an entire transaction, which means every update, insert, and delete since the transaction began, which is usually since the last commit or rollback.


2 Answers

If you committed your transaction, it's time to dust off that backup, sorry. But that's what backups are for. I did something like that once myself... once.

like image 142
Jeremy Holovacs Avatar answered Oct 27 '22 01:10

Jeremy Holovacs


Just an idea - could you restore your backup to a NEW database and then do a cross database query to update that column based on the data it used to be?

like image 27
user158017 Avatar answered Oct 27 '22 03:10

user158017