SQL更新撤消

发布于 2024-08-20 13:43:19 字数 26 浏览 3 评论 0原文

有没有办法可以撤消 SQL 更新查询?

Is there a way we can undo a SQL update query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不弃不离 2024-08-27 13:43:19

除非您在 交易

根据您的 DBMS,事务的处理方式会有所不同,因此请阅读文档。例如,使用 mysql 的情况如下:

START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;

使用 Postresql

BEGIN;
UPDATE accounts SET balance = balance - 100.00
    WHERE name = 'Alice';
-- etc etc
COMMIT;

使用 TSQL

DECLARE @TranName VARCHAR(20)
SELECT @TranName = 'MyTransaction'
BEGIN TRANSACTION @TranName
GO
USE pubs
GO
UPDATE roysched
SET royalty = royalty * 1.10
WHERE title_id LIKE 'Pc%'
GO
COMMIT TRANSACTION MyTransaction
GO

也有一些操作不能放入事务中,但在大多数数据库管理系统中您会发现这些天您应该能够回滚更新

最后,如果您保留数据库中发生的所有情况的精确日志,那么您始终可以撤消更新,但这是另一回事了。

You can't unless you ran it inside a transaction.

Depending on your DBMS transactions will be handled differently, so read the documentation. For instance with mysql here's how it goes:

START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;

With Postresql:

BEGIN;
UPDATE accounts SET balance = balance - 100.00
    WHERE name = 'Alice';
-- etc etc
COMMIT;

With TSQL:

DECLARE @TranName VARCHAR(20)
SELECT @TranName = 'MyTransaction'
BEGIN TRANSACTION @TranName
GO
USE pubs
GO
UPDATE roysched
SET royalty = royalty * 1.10
WHERE title_id LIKE 'Pc%'
GO
COMMIT TRANSACTION MyTransaction
GO

There are also some actions that can't be put into a transaction, but in most of the database management systems you can find these days you should be able to rollback an update.

Finally you can always undo an update if you keep a precise log of everything that happens in the database, but that's another story.

夜空下最亮的亮点 2024-08-27 13:43:19

如果您在事务内运行更新,则可以回滚该事务。

SQL Server:

begin transaction

update [Table] set col1 = 'test' where Id = 3

rollback transaction

另一种技术可能是向表添加更新触发器,并且每当记录更新时,将以前的值保存到“历史”表中。如果该表被大量使用,这可能是个好主意,也可能不是一个好主意。

If you run the update inside a transaction, you can rollback the transaction.

SQL Server:

begin transaction

update [Table] set col1 = 'test' where Id = 3

rollback transaction

Another technique might be to add an update trigger to the table, and anytime the record is updated, save the previous values off into a "history" table. This may or may not be a good idea if this table is heavily used.

年少掌心 2024-08-27 13:43:19

SQL Server 并不直接支持此功能,但在某些情况下可以通过读取事务日志来恢复数据。如果数据库处于完全恢复模式并且使用 ApexSQL Log日志救援。还有一种使用 DBCC LOG 命令读取 t-log 的方法,但它可能会变得太困难。

您还可以查看以下讨论此问题的主题:

如何在 SQL Server 2005 中回滚 UPDATE 查询?

读取 SQL Server 2008 中的日志文件(*.LDF)

SQL Server doesn’t support this out of the box but in some cases it is possible to recover data by reading transaction log. This works if database was in full recovery mode and 3rd party tools like ApexSQL Log or Log Rescue are needed. There is also a way to read t-log using DBCC LOG command but it will probably turn out to be too difficult.

You can also check out following topics where this is discussed:

How can I rollback an UPDATE query in SQL server 2005?

Read the log file (*.LDF) in SQL Server 2008

一念一轮回 2024-08-27 13:43:19

撤消在 SQL 中称为回滚。完成提交后,如果不恢复备份就无法撤消它。

请注意,执行回滚将撤消整个事务,这意味着自事务开始(通常是自上次提交或回滚以来)以来的每次更新、插入和删除。这意味着如果您连续进行两次更新,则无法仅撤消第二次更新。一些数据库提供“保存点”来允许这样做。

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. This means that if you do two consecutive updates, you can't undo just the second one. Some databases provide "savepoints" which do allow this.

楠木可依 2024-08-27 13:43:19

仅当您处于事务中(如 marcgg 所说)或者您有最近的备份时......以这种方式恢复确实会延长“撤消”一词。

Only if you are in a transaction (as marcgg) said or if you have a recent backup ... recovering that way does stratch the term "undo" rathermuch ..

摇划花蜜的午后 2024-08-27 13:43:19

您可以使用第三方工具,例如 Red Gate 的 Log Rescue。他们的 SQL Toolkit 有 14 天的免费功能试用版。 (假设是 SQL Server 2000!)。

You can use a third party tool like Red Gate's Log Rescue. There is a 14-day free, functional trial of their SQL Toolkit. (Assuming SQL Server 2000!).

温柔女人霸气范 2024-08-27 13:43:19

在生产数据库上的更新语句出现错误且最近没有正确备份的情况下,来到了这篇文章。

如果您使用的是 Microsoft SQL Server 并且您的数据库使用的是“完整”恢复模式(在 SSMS 中,右键单击数据库,数据库属性,选项),

您可能会被保存,您可以将数据库恢复到以前的时间,之前您的更新,感谢交易日志。

我不确定他们是如何做到这一点的,但单击“恢复到时间轴按钮”-> “具体日期和时间”并选择更新之前的时间。
这需要一点时间,但救了我。

之后,请确保您的备份策略正确:)

Came to this post after a mistake on an update statement on a production database, without proper recent backup.

If you're using Microsoft SQL Server and your database is using a "Full" Recovery model (in SSMS, right click on the DB, DB Properties, Options )

You might be saved, you can restore the database to a previous time, before your update, thanks to the transaction log.

I'm not sure on how they do that but click on "Restore to, Timeline button" -> "Specific date and time" and select the time before your update.
It takes a little while but saved me.

And please after that, make sure your backup strategy is on point :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文