完整数据库还原(简单恢复模式)Complete Database Restores (Simple Recovery Model)

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions)

数据库完整还原的目的是还原整个数据库。In a complete database restore, the goal is to restore the whole database. 整个数据库在还原期间处于脱机状态。The whole database is offline for the duration of the restore. 在数据库的任何部分变为联机之前,必须将所有数据恢复到同一点,即数据库的所有部分都处于同一时间点并且不存在未提交的事务。Before any part of the database can come online, all data is recovered to a consistent point in which all parts of the database are at the same point in time and no uncommitted transactions exist.

在简单恢复模式下,数据库不能还原到特定备份中的特定时间点。Under the simple recovery model, the database cannot be restored to a specific point in time within a specific backup.

重要

建议您不要附加或还原来自未知或不可信源的数据库。We recommend that you do not attach or restore databases from unknown or untrusted sources. 这些数据库可能包含执行非预期 Transact-SQLTransact-SQL 代码的恶意代码,或通过修改架构或物理数据库结构导致错误。These databases could contain malicious code that might execute unintended Transact-SQLTransact-SQL code or cause errors by modifying the schema or the physical database structure. 使用来自未知源或不可信源的数据库前,请在非生产服务器上针对数据库运行 DBCC CHECKDB ,然后检查数据库中的代码,例如存储过程或其他用户定义代码。Before you use a database from an unknown or untrusted source, run DBCC CHECKDB on the database on a nonproduction server and also examine the code, such as stored procedures or other user-defined code, in the database.

本主题内容:In this Topic:

备注

有关支持从 SQL ServerSQL Server的早期版本进行备份的信息,请参阅 RESTORE (Transact-SQL)中的“兼容性支持”部分。For information about support for backups from earlier versions of SQL ServerSQL Server, see the "Compatibility Support" section of RESTORE (Transact-SQL).

在简单恢复模式下还原数据库的概述Overview of Database Restore Under the Simple Recovery Model

简单恢复模式下的完整数据库还原只涉及一个或两个 RESTORE 语句,具体取决于是否需要还原差异数据库备份。A full database restore under the simple recovery model involves one or two RESTORE statements, depending on whether you want to restore a differential database backup. 如果只使用完整数据库备份,则只需还原最近的备份,如下图所示。If you are using only a full database backup, just restore the most recent backup, as shown in the following illustration.

仅还原完整数据库备份Restoring only a full database backup

如果还使用差异数据库备份,则应还原最近的完整数据库备份而不恢复数据库,然后还原最近的差异数据库备份并恢复数据库。If you are also using a differential database backup, restore the most recent full database backup without recovering the database, and then restore the most recent differential database backup and recover the database. 下图显示了这一过程。The following illustration shows this process.

还原完整数据库备份和差异数据库备份Restoring full and differential database backups

备注

如果你计划将数据库备份还原到其它服务器实例,请参阅 通过备份和还原来复制数据库If you plan to restore a database backup onto a different server instance, see Copy Databases with Backup and Restore.

基本 TRANSACT-SQL RESTORE 语法Basic Transact-SQL RESTORE Syntax

用于还原完整数据库备份的基本 Transact-SQLTransact-SQLRESTORE 语法是:The basic Transact-SQLTransact-SQLRESTORE syntax for restoring a full database backup is:

RESTORE DATABASE database_name FROM backup_device [ WITH NORECOVERY ]RESTORE DATABASE database_name FROM backup_device [ WITH NORECOVERY ]

备注

如果还打算还原差异数据库备份,则应使用 WITH NORECOVERY。Use WITH NORECOVERY if you plan to also restore a differential database backup.

用于还原数据库备份的 RESTORE 语句的基本语法是:The basic RESTORE syntax for restoring a database backup is:

RESTORE DATABASE database_name FROM backup_device WITH RECOVERYRESTORE DATABASE database_name FROM backup_device WITH RECOVERY

示例 (Transact-SQL)Example (Transact-SQL)

以下示例首先显示如何使用 BACKUP 语句来创建 AdventureWorks2012AdventureWorks2012 数据库的完整数据库备份和差异数据库备份。The following example first shows how to use the BACKUP statement to create a full database backup and a differential database backup of the AdventureWorks2012AdventureWorks2012 database. 然后按顺序还原这些备份。The example then restores these backups in sequence. 将数据库还原到完成差异数据库备份时的状态。The database is restored to its state as of the time that the differential database backup finished.

该示例说明数据库完整还原方案的还原序列中的关键选项。The example shows the critical options in a restore sequence for the complete database restore scenario. 还原顺序 由通过一个或多个还原阶段来移动数据的一个或多个还原操作组成。A restore sequence consists of one or more restore operations that move data through one or more of the phases of restore. 将省略与此目的不相关的语法和详细信息。Syntax and details that are not relevant to this purpose are omitted. 在恢复数据库时,尽管 RECOVERY 选项是默认值,但为清楚起见,仍建议显式指定该选项。When you recover a database, we recommend explicitly specifying the RECOVERY option for clarity, even though it is the default.

备注

此示例以 ALTER DATABASE 语句开头,该语句将恢复模式设置为 SIMPLEThe example starts with an ALTER DATABASE statement that sets the recovery model to SIMPLE.

USE master;  
--Make sure the database is using the simple recovery model.  
ALTER DATABASE AdventureWorks2012 SET RECOVERY SIMPLE;  
GO  
-- Back up the full AdventureWorks2012 database.  
BACKUP DATABASE AdventureWorks2012   
TO DISK = 'Z:\SQLServerBackups\AdventureWorks2012.bak'   
  WITH FORMAT;  
GO  
--Create a differential database backup.  
BACKUP DATABASE AdventureWorks2012   
TO DISK = 'Z:\SQLServerBackups\AdventureWorks2012.bak'  
   WITH DIFFERENTIAL;  
GO  
--Restore the full database backup (from backup set 1).  
RESTORE DATABASE AdventureWorks2012   
FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2012.bak'   
   WITH FILE=1, NORECOVERY;  
--Restore the differential backup (from backup set 2).  
RESTORE DATABASE AdventureWorks2012   
FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2012.bak'   
   WITH FILE=2, RECOVERY;  
GO  

还原完整数据库备份To restore a full database backup

还原差异数据库备份To restore a differential database backup

使用 SQL Server 管理对象 (SMO) 还原备份To restore a backup by using SQL Server Management Objects (SMO)

另请参阅See Also

RESTORE (Transact-SQL) RESTORE (Transact-SQL)
BACKUP (Transact-SQL) BACKUP (Transact-SQL)
sp_addumpdevice (Transact-SQL) sp_addumpdevice (Transact-SQL)
完整数据库备份 (SQL Server) Full Database Backups (SQL Server)
差异备份 (SQL Server) Differential Backups (SQL Server)
备份概述 (SQL Server) Backup Overview (SQL Server)
还原和恢复概述 (SQL Server)Restore and Recovery Overview (SQL Server)