61

如何撤消最近执行的 mysql 查询?

4

6 回答 6

45

如果将表类型定义为 InnoDB,则可以使用事务。您将需要 set AUTOCOMMIT=0,然后您可以发出COMMITROLLBACK在查询或会话结束时提交或取消交易。

ROLLBACK -- will undo the changes that you have made
于 2010-05-27T06:10:16.370 回答
38

您只能在交易期间这样做。

BEGIN;
INSERT INTO xxx ...;
DELETE FROM ...;

然后你可以:

COMMIT; -- will confirm your changes

或者

ROLLBACK -- will undo your previous changes
于 2010-05-27T06:08:21.337 回答
22

基本上:如果您正在执行事务,只需执行回滚。否则,您无法“撤消” MySQL 查询。

于 2010-05-27T06:07:10.853 回答
7

对于某些指令,例如 ALTER TABLE,这对于 MySQL 是不可能的,即使对于事务(12)也是如此。

于 2011-08-24T13:36:00.913 回答
4

您可以停止正在处理的查询

通过=> show processlist; 找到查询进程的Id;

然后 => 杀死 id;

于 2010-05-27T06:32:37.437 回答
1

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.

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.

于 2017-10-23T10:30:50.770 回答