SQLServer恢复表级数据
最近几天,公司的技术维护人员频繁让我恢复数据库,因为他们总是少了where条件,导致update、delete出现了无法恢复的后果,加上那些库都是几十G。恢复起来少说也要十几分钟。为此,找了一些资料和工作总结,给出一下几个方法,用于快速恢复表,而不是库, 但
最近几天,公司的技术维护人员频繁让我恢复数据库,因为他们总是少了where条件,导致update、delete出现了无法恢复的后果,加上那些库都是几十G。恢复起来少说也要十几分钟。为此,找了一些资料和工作总结,给出一下几个方法,用于快速恢复表,而不是库,但是切记,防范总比亡羊补牢好。
在生产环境或者开发环境,往往都有某些非常重要的表。这些表存放了核心数据。当这些表出现数据损坏时,需要尽快还原。但是,正式环境的数据库往往都是非常大的,统计数据表明,1T的数据库还原时间接近24小时,所以因为一个表而还原一个库,不单空间,甚至时间上都是一个很大的挑战。本文介绍如何恢复单表,而不需要恢复整个库。
现在假设一个表:TEST_TABLE。我们需要尽快恢复这个表,并且把恢复过程中对其他表和用户的影响降到最低。
SQLServer(特别是2008以后),具有很多备份及恢复功能:完整、部分、文件、差异和事务备份。而恢复模式的选择严重影响备份策略和备份类型。
下面是几个可供参考的方案,但是记住,各有好坏,应该按照实际需要选择:
方案1:恢复到一个不同的数据库:
这对于小数据库来说不失为一种好的办法,用备份还原一个新的库,并把新库中的表数据同步回去。你可以做完整恢复,或者时间点恢复。但是对于大数据库,是非常耗时和耗费磁盘空间的。这个方法仅仅用于还原数据,在还原数据(就是同步数据)的时候,你要考虑触发器、外键等因素。
方案2:使用STOPAT来还原日志:
你可能想恢复最近的数据库备份,并回滚到某个时间点,即发生意外前的某个时刻。此时可以使用STOPAT子句,但是前提是必须为完整或大容量日志恢复模式。下面是例子:
1 2 3 4 5 6 7 8 9 |
|
注意:这种方法的主要缺点是会覆盖掉从stopat指定时间点之后所修改的所有数据。所以要衡量好得失。
方案3:数据库快照:
创建数据库快照。当发生意外时,可以从快照中直接获取原来的数据。但是必须是在发生意外之前创建的快照。这在核心表不经常更新,特别是有规律更新时很有用。但是当表经常、不定期被更新,或者很多用户在访问时,这种方法就不可取了。当需要使用这种方法时,记得在每次更新前先创建快照。
方案4:使用视图:
你可以创建一个新的数据库,并把TEST_TABLE移动到这个库里面。当你需要恢复的时候,你只需要恢复这个非常小的数据库即可。访问源数据库的数据时,最简单的方法就是创建一个视图,选择TEST_TABLE表中所有列的所有数据。但是注意这个方法需要在创建视图前,重命名或者删除源数据库的表:
1 2 3 4 5 6 7 |
|
使用这种方法,可以对视图使用SELECT /INSERT/UPDATE/DELETE语句,就像直接操作实体表似得。当TEST_TABLE更改时,要使用SP_REFRESHVIEW存储过程来更新元数据。
方案5:创建同义词(Synonym):
和方案4类似,把表移到另外一个数据库,然后对源数据库的这个表创建一个同义词:
1 2 3 4 5 |
|
这个方法的有点就是你不需要担心元数据更新所带来的结构变更不及时。但是这个方法的问题就是不能在DDL语句中引用同义词,或者不能在链接服务器中找到。
方案6:使用BCP保存数据:
你可以创建一个作业,使用BCP定期导出数据。但是这种方法的缺点和方案1类似,需要找到哪天的文件并导进去,同时要考虑触发器和外键问题。
各种方法的对比:
方法 | 优点 | 缺点 |
还原数据库 | 快且容易 | 适用于小库,且要注意触发器和外键等 |
还原日志 | 能指定时间点 | 所有时间点后的新数据会被覆盖 |
数据库快照 | 当表不是经常更新时很有用 | 当表并行更新时,快照容易出现问题 |
视图 | 把表的数据于库分开,没有数据丢失 | 元数据需要周期性更新,并要定期维护新数据库 |
同义词 | 把表的数据于库分开,没有数据丢失 | 在链接服务器上不能用,并要定期维护新数据库 |
BCP | 拥有表的专用备份 | 需要额外的空间、还会出现触发器、外键等问题 |
总结:
良好的编程习惯和良好的备份机制才是解决问题的根本,以上的措施都仅仅是一个亡羊补牢的办法。可能有人说SQLServer 新版本不是有部分还原吗?我们来看看联机丛书的说明:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

SQL Server English installation can be changed to Chinese by following the following steps: download the corresponding language pack; stop the SQL Server service; install the language pack; change the instance language; change the user interface language; restart the application.
