SQLServer无需Restore恢复误删表(一):恢复表结构

发布时间:2020-03-04 05:32:01 作者:易语随风去
来源:网络 阅读:2340

最近在研究《Microsoft SQL Server 2012 Internals》这本书,考虑到如何快速恢复误操作数据,如UPDATE、DELETE、TRUNCATE、DROP等操作。当数据库特别大的时候,通过还原数据库恢复误操作数据就会变得非常吃力。

那么如何在不restore database的情况下,快速进行数据恢复呢。这也将是本文将要提到的内容。


首先,简单了解下DROP TABLE操作的原理

1. 删除表的DDL

2. 删除数据页数据


通过分析Transaction Log可知,drop table并不会记录删除每一行数据的日志,drop table最终是通过标记该表数据页为可重写以表示释放空间(当空间不够时,会format掉这些数据页),

当数据库空间不足时,SQL Server可对这部分空间进行数据写入。


因此,我们想要在不restore database情况下恢复数据,就得确保drop table后表的数据页没有被format。一旦数据页被format,那么只能通过restore database方式进行恢复(目前尚未找到其他的恢复方法)。


以下为恢复表结构语句的实例

1. 建表

create table test_drop(
col1  tinyint,
col2  smallint,
col3  int identity(1,1),
col4  bigint,
col5  varchar(20),
col6  char(20),
col7  nvarchar(20),
col8  nchar(20),
col9  datetime,
col10 timestamp,
col11 uniqueidentifier,
col12 sysname,
col13 numeric(10,2),
col14 xml,
col15 money,
col16 text
)


2. 删除表

drop table test_drop


3. 恢复被删除的表结构语句

exec Recover_Dropped_Table_DDL_Porc 'test_drop'

SQLServer无需Restore恢复误删表(一):恢复表结构


生成恢复语句如下:

if object_id('dbo.test_drop') is not null
    print  'dbo.test_drop is existed'
else
create table dbo.test_drop(col1 tinyint null 
,col2 smallint null 
,col3 int identity 
,col4 bigint null 
,col5 varchar(20) collate SQL_Latin1_General_CP1_CI_AS null 
,col6 char(20) collate SQL_Latin1_General_CP1_CI_AS null 
,col7 nvarchar(20) collate SQL_Latin1_General_CP1_CI_AS null 
,col8 nchar(20) collate SQL_Latin1_General_CP1_CI_AS null 
,col9 datetime null 
,col10 timestamp not null 
,col11 uniqueidentifier null 
,col12 sysname collate SQL_Latin1_General_CP1_CI_AS not null 
,col13 numeric(10,2) null 
,col14 xml null 
,col15 money null 
,col16 text collate SQL_Latin1_General_CP1_CI_AS null 
)



推荐阅读:
  1. 服务器故障导致SqlServer数据库损坏的恢复过程
  2. MongoDB误删表恢复

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sql server recover

上一篇:oracle 表空间扩容方法

下一篇:Mongodb基操--将复制集与节点选举玩弄于股掌之中

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》