-------------------------------------------创建数据库DemoDB
create database DemoDB
on primary
(name='DemoDB_data',filename='c:\SQLData\DemoDB_data.mdf',size=5MB,maxsize=10MB)
log on
(name='DemoDB_log',filename='c:\SQLData\Demodb_log.ldf',size=2MB,maxsize=10MB)
go
--------------------------------------------在DemoDB创建数据表T1和T2
use DemoDB
create table T1(id int,name char(8),address char(13))
go
create table T2(id int,name char(8),address char(13))
go
--------------------------------------------在DemoDB数据库的T1和T2插入数据
use DemoDB
Insert into T1 values(1,'jacky','suzhou')
Insert into T1 values(2,'Hellen','shanghai')
Insert into T2 values(1,'Tom','beijing')
Insert into T2 values(2,'Alice','hangzhou')
go
--------------------------------------------为DemoDB数据库创建数据库快照DemoDB_dbsnapshot_200510201600
create database DemoDB_dbsnapshot_200510201600
on
(name='DemoDB_data',filename='c:\SQLsnapshot\DemoDB_dbsnapshot_200510201600.mdf')
as snapshot of DemoDB
go
---------------------------------------------在数据库快照和数据库中查询T1和T2表
use DemoDB_dbsnapshot_200510201600
select * from dbo.T1
select * from dbo.T2
go
use DemoDB
select * from dbo.T1
select * from dbo.T2
go
--------------------------------------------------
use DemoDB
update T1
set name='Tony' where id=1 --在DemoDB中更新数据
go
delete from T1 where id=2 --在DemoDB中删除数据
go
drop Table T2 --删除T2表
go
--------------------------------------------------在数据库快照和数据库中查询T1和T2表
use DemoDB_dbsnapshot_200510201600
select * from T1
select * from T2
go
use DemoDB
select * from T1
select * from T2
go
-------------------------------------------------使用数据库快照还原在DemoDB数据库的T1表误删除和更新的数据
update DemoDB.dbo.T1
set name=(select name from DemoDB_dbsnapshot_200510201600.dbo.T1 where id=1) where name=1
go
insert into DemoDB.dbo.T1
select * from DemoDB_dbsnapshot_200510201600.dbo.T1 where id=2
go
-----------------------------------------------------------使用数据库快照还原在DemoDB数据库误删除的T2表
use DemoDB
--复制进剪贴板中的创建T2的语句
go
insert into DemoDB.dbo.T2 select * from DemoDB_dbsnapshot_200510201600.dbo.T2
go
-----------------------------------------------------------在数据库快照和数据库中查询T1和T2表
use DemoDB
select * from T1
select * from T2
go
use DemoDB_dbsnapshot_200510201600
select * from T1
select * from T2
go
-----------------------------------------------------------
注:如果需要周期创建快照,可以创建作业
-----------------------------------------------------------在DemoDB中更新数据
use DemoDB
update T1 set name='Funny' where id=1
go
-----------------------------------------------------------数据库快照和数据库中查询T1和T2表
select * from Demodb.dbo.T1
select * from DemoDB_dbsnapshot_200510201600.dbo.T1
select * from DemoDB_dbsnapshot_200510201620.dbo.T1
-----------------------------------------------------------在DemoDB中更新数据
use DemoDB
update T1 set name='Bob' where id=1
go
-----------------------------------------------------------数据库快照和数据库中查询T1和T2表
select * from Demodb.dbo.T1
select * from DemoDB_dbsnapshot_200510201600.dbo.T1
select * from DemoDB_dbsnapshot_200510201620.dbo.T1
----------------------------------------------------------------------------------------------------------------------------------------
/*使用数据库快照还原整个数据库*/
-------------------------------------------删除第一次数据库快照
drop database DemoDB_dbsnapshot_200510201600
-------------------------------------------使用数据库快照恢复DemoDB数据库
restore Database DemoDB from Database_snapshot='DemoDB_dbsnapshot_200510201620'
-------------------------------------------
select * from DemoDB.dbo.T1
select * from DemoDB_dbsnapshot_200510201620.dbo.T1
-------------------------------------------
drop database DemoDB_dbsnapshot_200510201620 --删除数据库快照
drop Database DemoDB --删除数据库
![](http://www.cnblogs.com/images/cnblogs_com/glj1203/LOGO.jpg)