如何撤消刚刚执行的 mysql 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2918831/
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
How can I undo a mysql statement that I just executed?
提问by Hari kanna
How can I undo the most recently executed mysql query?
如何撤消最近执行的 mysql 查询?
采纳答案by Vimard
If you define table type as InnoDB, you can use transactions. You will need set AUTOCOMMIT=0
, and after you can issue COMMIT
or ROLLBACK
at the end of query or session to submit or cancel a transaction.
如果将表类型定义为 InnoDB,则可以使用事务。您将需要 set AUTOCOMMIT=0
,并且在您可以发出COMMIT
或ROLLBACK
在查询或会话结束时提交或取消交易。
ROLLBACK -- will undo the changes that you have made
回答by Palantir
You can only do so during a transaction.
您只能在交易期间这样做。
BEGIN;
INSERT INTO xxx ...;
DELETE FROM ...;
Then you can either:
然后你可以:
COMMIT; -- will confirm your changes
Or
或者
ROLLBACK -- will undo your previous changes
回答by halfdan
Basically: If you're doing a transaction just do a rollback. Otherwise, you can't "undo" a MySQL query.
基本上:如果您正在执行事务,只需回滚即可。否则,您无法“撤消”MySQL 查询。
回答by adelarsq
回答by RINSON KE
You can stop a query which is being processed by this
您可以停止正在处理的查询
Find the Id of the query process by => show processlist;
通过=> show processlist; 找到查询进程的Id;
Then => kill id;
然后 => 杀死 id;
回答by meistermuh
in case you do not only need to undo your last query (although your question actually only points on that, I know) and therefore if a transaction might not help you out, you need to implement a workaround for this:
如果您不仅需要撤消上一个查询(尽管我知道您的问题实际上只针对这一点),因此如果事务可能无法帮助您,您需要为此实施解决方法:
copy the original data before commiting your query and write it back on demand based on the unique id that must be the same in both tables; your rollback-table (with the copies of the unchanged data) and your actual table (containing the data that should be "undone" than). for databases having many tables, one single "rollback-table" containing structured dumps/copies of the original data would be better to use then one for each actual table. it would contain the name of the actual table, the unique id of the row, and in a third field the content in any desired format that represents the data structure and values clearly (e.g. XML). based on the first two fields this third one would be parsed and written back to the actual table. a fourth field with a timestamp would help cleaning up this rollback-table.
在提交查询之前复制原始数据,并根据两个表中必须相同的唯一 ID 按需将其写回;您的回滚表(带有未更改数据的副本)和您的实际表(包含应该“撤消”的数据)。对于具有许多表的数据库,最好使用一个包含原始数据结构化转储/副本的“回滚表”,然后为每个实际表使用一个。它将包含实际表的名称、行的唯一 id,并在第三个字段中包含任何所需格式的内容,清楚地表示数据结构和值(例如 XML)。基于前两个字段,第三个字段将被解析并写回实际表。带有时间戳的第四个字段将有助于清理此回滚表。
since there is no real undo in SQL-dialects despite "rollback" in a transaction (please correct me if I'm wrong - maybe there now is one), this is the only way, I guess, and you have to write the code for it on your own.
尽管在事务中“回滚”,但 SQL 方言中没有真正的撤消(如果我错了,请纠正我 - 也许现在有一个),我猜这是唯一的方法,你必须编写代码靠你自己。