简体   繁体   English

如何撤消刚刚执行的mysql语句?

[英]How can I undo a mysql statement that I just executed?

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

If you define table type as InnoDB, you can use transactions. 如果将表类型定义为InnoDB,则可以使用事务。 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. 您需要设置AUTOCOMMIT=0 ,然后在查询或会话结束时发出COMMITROLLBACK以提交或取消事务。

ROLLBACK -- will undo the changes that you have made

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

Basically: If you're doing a transaction just do a rollback. 基本上:如果您正在进行交易,只需进行回滚即可。 Otherwise, you can't "undo" a MySQL query. 否则,您无法“撤消”MySQL查询。

对于某些指令,如ALTER TABLE,即使使用事务( 12 ),MySQL也无法做到这一点。

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;

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; 在提交查询之前复制原始数据,并根据两个表中必须相同的唯一ID按需写回; 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 (eg XML). 它将包含实际表的名称,行的唯一ID,并在第三个字段中包含任何所需格式的内容,它们清楚地表示数据结构和值(例如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. 因为尽管在事务中“回滚”,但是在SQL语言中没有真正的撤销(请纠正我,如果我错了 - 也许现在有一个),这是唯一的方法,我想,你必须编写代码为你自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM