如何在 MySQL 中回滚上次删除命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2356566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:24:41  来源:igfitidea点击:

How can I roll back my last delete command in MySQL?

mysqlsqlsql-deleterollback

提问by Vijay

I accidentally deleted some huge number of rows from a table...

我不小心从表中删除了大量的行...

How can I roll it back?

我怎样才能回滚它?

I executed the query using PuTTY.

我使用PuTTY执行了查询。

I'll be grateful if any of you can guide me safely out of this...

如果你们中的任何人能引导我安全地摆脱这种情况,我将不胜感激......

采纳答案by Aaron Digulla

If you didn't commit the transaction yet, try rollback. If you have already committed the transaction (by commitor by exiting the command line client), you must restore the data from your last backup.

如果您还没有提交事务,请尝试rollback。如果您已经提交了事务(通过commit或通过退出命令行客户端),您必须从上次备份中恢复数据。

回答by Omry Yadan

If you haven't made a backup, you are pretty much fudged.

如果您还没有进行备份,那么您就被骗了。

回答by Select0r

A "rollback" only works if you used transactions. That way you can group queries together and undo all queries if only one of them fails.

“回滚”仅在您使用transactions时才有效。这样您就可以将查询组合在一起,并在只有一个查询失败时撤消所有查询。

But if you already committed the transaction (or used a regular DELETE-query), the only way of getting your data back is to recover it from a previously made backup.

但是,如果您已经提交了事务(或使用了常规的 DELETE 查询),则取回数据的唯一方法是从以前制作的备份中恢复它。

回答by Praveen Patel G

Use the BEGIN TRANSACTIONcommand before starting queries. So that you can ROLLBACKthings at any point of time.

BEGIN TRANSACTION在开始查询之前使用该命令。这样您就可以ROLLBACK在任何时间点进行处理。

FOR EXAMPLE:

例如:

  1. begin transaction
  2. select * from Student
  3. delete from Student where Id=2
  4. select * from Student
  5. rollback
  6. select * from Student
  1. 开始交易
  2. 从学生中选择*
  3. 从学生中删除,其中 Id=2
  4. 从学生中选择*
  5. 回滚
  6. 从学生中选择*

回答by Phil Lello

The accepted answer is not always correct. If you configure binary logging on MySQL, you can rollback the database to any previous point you still have a snapshot and binlog for.

接受的答案并不总是正确的。如果您在 MySQL 上配置二进制日志记录,您可以将数据库回滚到您仍然拥有快照和二进制日志的任何先前点。

7.5 Point-in-Time (Incremental) Recovery Using the Binary Logis a good starting point for learning about this facility.

7.5 时间点(增量)恢复 使用二进制日志是了解此功能的一个很好的起点。

回答by Shu Zhang

In MySQL:

在 MySQL 中:

start transaction;

savepoint sp1;

delete from customer where ID=1;

savepoint sp2;

delete from customer where ID=2;

rollback to sp2;

rollback to sp1;

回答by Kishore Kishore

If you want rollback data, firstly you need to execute autocommit =0 and then execute query delete, insert, or update.

如果要回滚数据,首先需要执行autocommit =0,然后执行查询删除、插入或更新。

After executing the query then execute rollback...

执行查询后执行回滚...

回答by Nikki

I also had deleted some values from my development database, but I had the same copy in QA database, so I did a generate script and selected option "type of data to script" to "data only" and selected my table.

我还从我的开发数据库中删除了一些值,但我在 QA 数据库中有相同的副本,所以我做了一个生成脚本并将“数据类型到脚本”选择为“仅数据”并选择了我的表。

Then I got the insert statements with same data, and then I run the script on my development database.

然后我得到了具有相同数据的插入语句,然后在我的开发数据库上运行脚本。

回答by Andreas

In Oracle this would be a non issue:

在 Oracle 中,这不是问题:

SQL> delete from Employee where id = '01';

1 row deleted.

SQL> select id, last_name from Employee where id = '01';

no rows selected

SQL> rollback;

Rollback complete.

SQL> select * from Employee  where id = '01';

ID   FIRST_NAME LAST_NAME  START_DAT END_DATE      SALARY CITY       DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01   Jason      Martin     25-JUL-96 25-JUL-06    1234.56 Toronto    Programmer

回答by ram

Rollback normally won't work on these delete functions and surely a backup only can save you.

回滚通常不适用于这些删除功能,当然备份只能拯救您。

If there is no backup then there is no way to restore it as delete queries ran on PuTTY,Derby using .sql files are auto committed once you fire the delete query.

如果没有备份,则无法恢复它,因为删除查询在 PuTTY 上运行,一旦您触发删除查询,使用 .sql 文件的 Derby 将自动提交。