介绍 (Introduction)

MS SQL Server 2005 and later versions include the Database Snapshot feature to have snapshot of the database for reports, as a copy in different periods.

MS SQL Server 2005和更高版本包含“数据库快照”功能,以具有用于报告的数据库快照,作为不同时期的副本。

The Database Snapshot can be created multiple times and it can only be created using the T-SQL.

数据库快照可以创建多次,并且只能使用T-SQL创建。

In this article, we will show how to create a Database Snapshot, how to see the snapshot created in the SQL Server Management Studio (SSMS), how to recover objects dropped or data removed using the snapshot.

在本文中,我们将展示如何创建数据库快照,如何查看在SQL Server Management Studio(SSMS)中创建的快照,如何恢复使用快照删除的对象或删除的数据。

Finally, we will learn how to create Snapshots automatically for reporting purposes.

最后,我们将学习如何为报告目的自动创建快照。

要求 (Requirements)

  • SQL Server Enterprise or Evaluation Edition is required需要SQL Server Enterprise或评估版
  • We are using SQL Server 2014, but earlier versions can be used我们正在使用SQL Server 2014,但是可以使用早期版本
  • The Adventureworks Database is required需要Adventureworks数据库
  • The Adventurewoks database has to be onlineAdventurewoks数据库必须在线

入门 (Getting started)

To create a database snapshot, we need to use the T-SQL. It is the only way to do it. You cannot create a Database Snapshot in the SSMS.

要创建数据库快照,我们需要使用T-SQL。 这是唯一的方法。 您不能在SSMS中创建数据库快照。

The syntax is the following:

语法如下:


CREATE DATABASE AdventureWorks_snapshot ON( NAME = AdventureWorks2012_Data, --Name of the snapshot fileFILENAME ='C:\script2\AdventureWorks_data_1800.ss' )--It is a Snapshot of the adventureworks2012 databaseAS SNAPSHOT OF [AdventureWorks2012];GO

As you can see, the syntax is similar to a normal database creation except for two things:

如您所见,该语法与普通数据库的创建相似,除了两点:

  1. We use the word AS SNAPSHOT OF DATABASE_NAME to specify the name of the database that requires a snapshot.我们使用单词AS SNAPSHOT OF DATABASE_NAME来指定需要快照的数据库的名称。
  2. By default, it is better to specify the extension of the snapshot datafile as .ss (which means SnapShot.默认情况下,最好将快照数据文件的扩展名指定为.ss(即SnapShot。

If everything is OK, you will be able to see the snapshot created in the SSMS:

如果一切正常,您将能够看到在SSMS中创建的快照:

Figure 1

图1

The snapshots have read-only tables. If you try to update or delete the data you will not be able to do it. You will receive a read only message:

快照具有只读表。 如果您尝试更新或删除数据,则将无法执行。 您将收到一条只读消息:

Figure 2

图2

You can also use the T-SQL to try to update tables in the database snapshot with the same results:

您也可以使用T-SQL尝试以相同的结果更新数据库快照中的表:

Figure 3

图3

The database snapshots files have a similar size that a normal database, but a smaller size on disk. This is because each time that the original database changes, the snapshot grows. This reduces a little bit the database performance because of the synchronization.

数据库快照文件的大小与普通数据库相似,但磁盘上的文件较小。 这是因为原始数据库每次更改时,快照都会增长。 由于同步,这会降低一点数据库性能。

Figure 4

图4

The file size of the Snapshot Database is just 3.31 MB. The Adventureworks2012 database has the same size and size in disk as shown in the picture 5:

快照数据库的文件大小仅为3.31 MB。 Adventureworks2012数据库的磁盘大小和大小相同,如图5所示:

Figure 5

图5

The size on this of the source database is equal to 205 MB.

源数据库的大小等于205 MB。

使用快照恢复对象 (Using snapshots to recover objects)

If by mistake (or any other circumstance) a user drops a stored procedure, a view or a table or any object, you can recover the database object using the snapshot.

如果用户由于错误(或任何其他情况)删除了存储过程,视图或表或任何对象,则可以使用快照恢复数据库对象。

例 (Example)

Imagine that an evil person drops the dbo.uspGetBillOfMaterials stored procedure in the Database

想象一个邪恶的人在数据库中删除了dbo.uspGetBillOfMaterials存储过程


Drop procedure dbo.uspGetBillOfMaterials

Now, imagine that you are a smart person and you recover the stored procedure using the stored procedure from the Database Snapshot. To do this, generate the CREATE PROCEDURE from the Snapshot Database:

现在,假设您是一个聪明的人,并且使用数据库快照中的存储过程来恢复存储过程。 为此,请从快照数据库生成CREATE PROCEDURE:

Figure 6

图6

In the code generated, just replace the first part like this:

Replace

在生成的代码中,只需像这样替换第一部分:

更换

USE  [AdventureWorks_snapshot]
USE [AdventureWorks2012]

Once replaced press F5 to execute the script:

替换后,按F5键执行脚本:

Figure 7

图7

If you follow all the steps, you will have your stored procedure restored. The same concept is applicable to any Database Object. You can easily recreate specific objects from the Snapshot Database to the source Database.

如果执行所有步骤,将还原存储过程。 相同的概念适用于任何数据库对象。 您可以轻松地将特定对象从快照数据库重新创建到源数据库。

从表中还原数据 (Restore the data from a table)

In this new demo, we will truncate all the data from the table Person.Password and restore the information from the Database Snapshot snapshot.

在这个新的演示中,我们将截断表Person.Password中的所有数据,并从数据库快照快照中还原信息。

First, truncate all the data from a table:

首先,截断表中的所有数据:

truncate table [Person].[Password]

Secondly, restore the information from the Snapshot Database table to the source Database:

其次,将信息从“快照数据库”表还原到源数据库:


USE [AdventureWorks2012]
GO
INSERT INTO [Person].Password
Select*
From [AdventureWorks_snapshot].[Person].Password

We just inserted the information from the snapshot table to the empty Person.Table table from the Adventureworks2012 database.

我们只是将快照表中的信息插入到Adventureworks2012数据库中的空Person.Table表中。

从快照还原整个数据库 (Restore the entire database from the snapshot)

If all the objects and data were dropped, it would possible to recover all the information from the snapshot to the original database.

如果删除了所有对象和数据,则可以将所有信息从快照恢复到原始数据库。

In this new example, we will delete all the views from the AdventureWorks database and recover all the information from the snapshot.

在这个新示例中,我们将从AdventureWorks数据库中删除所有视图,并从快照中恢复所有信息。

  1. First we will have all the views of the AdventureWorks database:

    首先,我们将拥有AdventureWorks数据库的所有视图:

    Figure 8

    图8

  2. Then we will drop all the views:

    然后,我们将删除所有视图:

    
    DECLARE @sql VARCHAR(MAX)='';
    SELECT @sql=@sql+'DROP VIEW ['+name +'];' FROM sys.views;
    EXEC(@sql);

    As you can see in the Figure 9, all the views were removed:

    如您在图9中看到的,所有视图都被删除:

    Figure 9

    图9

  3. Now, revert your database. You may have problems to restore the database because there are multiple connection available. If that is the case, set the AdventureWorks database in a single user mode to close the other connections.

    现在,还原数据库。 由于存在多个可用连接,因此您可能在还原数据库时遇到问题。 如果是这种情况,请在单用户模式下设置AdventureWorks数据库以关闭其他连接。

    
    USE master;
    GO
    ALTER DATABASE AdventureWorks2012
    SET SINGLE_USER
    WITH ROLLBACK IMMEDIATE;
    GO
  4. Finally, restore the AdventureWorks Database from the snapshot:

    最后,从快照还原AdventureWorks数据库:

    
    RESTORE DATABASE AdventureWorks2012 fromDATABASE_SNAPSHOT = 'AdventureWorks_snapshot';

If everything is OK, you will be able to see your dropped views again:

如果一切正常,您将能够再次看到您的拖放视图:

Figure 10

图10

每月创建数据库快照 (Create Database Snapshots every month)

Finally, we will show how to create Database snapshots every month.

最后,我们将展示如何每月创建数据库快照。

Here you have the T-SQL code to create a Database with the current month number:

在这里,您具有使用当前月份号创建数据库的T-SQL代码:


DECLARE @SQL VARCHAR(MAX)='';--GET THE MONTH NUMBER IN THE @MONTH VARIABLEDECLARE @MONTH VARCHAR(2)= MONTH(GETDATE())--CONCATENATE THE SNAPSHOT DATABASE AND THE MONTH NUMBERSELECT @SQL ='CREATE DATABASE ADVENTUREWORKS_SNAPSHOT_'+@MONTH+' ON (NAME=AdventureWorks2012_Data, FILENAME=''C:\scripts\adventure.ss'')AS SNAPSHOT OF Adventureworks2012'EXECUTE (@SQL)

The code creates a Database Snapshot with the current number of the month.

该代码使用当前的月份数创建一个数据库快照。

Figure 11

图11

To create snapshots of the database, it is necessary to schedule a Job each month:

要创建数据库的快照,有必要每月计划一次作业:

Figure 12

图12

Specify a Name and optionally a description of the job:

指定名称和可选的作业描述:

Figure 13

图13

In the steps page create a new page. Specify a Name and select the T-SQL Type (which is the first option by default):

在步骤页面中创建一个新页面。 指定名称并选择T-SQL类型(默认情况下是第一个选项):

Figure 14

图14

Finally, schedule the job every month:

最后,每月安排工作:

Figure 15

图15

As you can see, it is very simple to automate tasks and work with Snapshot Databases.

如您所见,自动化任务和使用快照数据库非常简单。

结论 (Conclusion)

As you can see, Snapshots is a simple way to create copies of your information to partially restore the information. The Snapshots cannot replace the traditional backups, because it depends on the source database. If the source database is corrupted, the Snapshot will not be able to restore the database because there is a dependency between them. However, you can restore data and objects from the snapshot. This is very useful if the database is big and we want to restore some few objects or some specific rows.

如您所见,快照是创建信息副本以部分还原信息的简单方法。 快照不能替换传统备份,因为它取决于源数据库。 如果源数据库已损坏,则快照将无法还原数据库,因为它们之间存在依赖性。 但是,您可以从快照还原数据和对象。 如果数据库很大并且我们要还原一些对象或某些特定的行,这将非常有用。

翻译自: https://www.sqlshack.com/sql-server-database-snapshots/

SQL Server数据库快照相关推荐

  1. 数据库附加出错显示服务器,SQL Server数据库附加失败的解决办法

    SQL Server数据库附加失败的解决办法 本文为大家分享了SQL Server数据库附加失败的具体解决方法,供大家参考,具体内容如下 1.错误3415 问题:附加数据库的时候,发现总是附加失败,查 ...

  2. 人人都是 DBA(V)SQL Server 数据库文件

    SQL Server 数据库安装后会包含 4 个默认系统数据库:master, model, msdb, tempdb. SELECT [name],database_id,suser_sname(o ...

  3. SQL Server数据库同步问题分享[未完,待续](一)

    SQL Server数据库同步问题分享[未完,待续](一) SQL Server数据库同步问题分享(二)---操作步骤[未完,待续] SQL Server数据库同步问题分享(三)---创建订阅 合并发 ...

  4. SqlPackage.exe –使用bacpac和PowerShell或Batch技术自动执行SQL Server数据库还原

    Data is the key to your organization's future, but if it's outdated, irrelevant, or hidden then it's ...

  5. 使用SQL Server事务复制将SQL Server数据库迁移到Azure SQL数据库

    In this guide, we'll discuss more about migrating a SQL Server database to Azure SQL Database using ...

  6. 如何对两个大型SQL Server数据库中的数据进行快速估计比较,以查看它们是否相等

    Bringing impactful analysis into a data always comes with challenges. In many cases, we rely on auto ...

  7. sql azure 语法_如何:Azure中SQL Server文件快照备份

    sql azure 语法 After receiving new additions to backup and restore capabilities of SQL Servers like fi ...

  8. 了解SQL Server数据库恢复模型

    A recovery model is a database configuration option that determines the type of backup that one coul ...

  9. Microsoft SQL Server数据库部署过程

    介绍 (Introduction) Database deployments are critical tasks that can affect negative in on performance ...

最新文章

  1. iterm2 主题_【超级实用】Iterm2 + ohmyzsh 打造强大的终端编辑器
  2. hdfs 数据迁移_基于JindoFS+OSS构建高效数据湖
  3. 理解JavaScript中的事件
  4. 使用js技术使字体闪烁
  5. 新闻系统查询思路和步骤
  6. 服务器网盘系统怎么装,云服务器上怎么安装操作系统
  7. 当当花160买400的书,确定不囤一波?
  8. 梅特勒托利多xk3124电子秤说明书_托利多电子秤详细说明书
  9. 深度学习21天——卷积神经网络(CNN):实现mnist手写数字识别(第1天)
  10. C语言程序设计学习笔记:P3-判断
  11. 初始Ext.net(二)
  12. office online server2016详细安装步骤及问题总结
  13. 海驾学车过程全揭秘——第六篇:辛苦的学车全过程
  14. Unicode以及字符集转换
  15. 选拔赛proA:经营小卖部
  16. 4.19 亿条 Facebook 用户账号及电话号码被泄露【智能快讯】
  17. 微信小程序开发商城详细步骤
  18. marquee做文字滚动、图片轮播
  19. 冲压模具设计及制造概述,值得了解学习
  20. 中考计算机考试作文,面临中考的考试作文五篇

热门文章

  1. cefsharp 网页另存为图片_如何将PDF转换为JPG图片?这些转换方法一学就会
  2. 电力安全工作规程发电厂和变电站电气部分_一招告诉你,何为电力系统
  3. python3.14_leetcode-python3-14. 最长公共前缀
  4. 读取无线手柄数据_全透外形,优秀手感,双平台通吃:倍思Switch无线手柄
  5. 电脑剪贴板在哪里打开_如何把在公司电脑上复制的内容,粘贴到家里的电脑?超好用!...
  6. 加州大学欧文计算机工程硕士,UCI加州大学尔湾分校软件工程硕士Master of Software Engineering...
  7. Luogu1515 青蛙的约会
  8. 睡觉前后爆笑的情侣小两口~媳妇不要闹了~
  9. 数据库---三大设计范式
  10. 好好学习 天天编程—C语言之我的第一个hello world(二)