溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SQLServer無需Restore恢復(fù)誤刪表(一):恢復(fù)表結(jié)構(gòu)

發(fā)布時間:2020-03-04 05:32:01 來源:網(wǎng)絡(luò) 閱讀:2348 作者:易語隨風(fēng)去 欄目:關(guān)系型數(shù)據(jù)庫

最近在研究《Microsoft SQL Server 2012 Internals》這本書,考慮到如何快速恢復(fù)誤操作數(shù)據(jù),如UPDATE、DELETE、TRUNCATE、DROP等操作。當(dāng)數(shù)據(jù)庫特別大的時候,通過還原數(shù)據(jù)庫恢復(fù)誤操作數(shù)據(jù)就會變得非常吃力。

那么如何在不restore database的情況下,快速進行數(shù)據(jù)恢復(fù)呢。這也將是本文將要提到的內(nèi)容。


首先,簡單了解下DROP TABLE操作的原理

1. 刪除表的DDL

2. 刪除數(shù)據(jù)頁數(shù)據(jù)


通過分析Transaction Log可知,drop table并不會記錄刪除每一行數(shù)據(jù)的日志,drop table最終是通過標(biāo)記該表數(shù)據(jù)頁為可重寫以表示釋放空間(當(dāng)空間不夠時,會format掉這些數(shù)據(jù)頁),

當(dāng)數(shù)據(jù)庫空間不足時,SQL Server可對這部分空間進行數(shù)據(jù)寫入。


因此,我們想要在不restore database情況下恢復(fù)數(shù)據(jù),就得確保drop table后表的數(shù)據(jù)頁沒有被format。一旦數(shù)據(jù)頁被format,那么只能通過restore database方式進行恢復(fù)(目前尚未找到其他的恢復(fù)方法)。


以下為恢復(fù)表結(jié)構(gòu)語句的實例

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. 恢復(fù)被刪除的表結(jié)構(gòu)語句

exec Recover_Dropped_Table_DDL_Porc 'test_drop'

SQLServer無需Restore恢復(fù)誤刪表(一):恢復(fù)表結(jié)構(gòu)


生成恢復(fù)語句如下:

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 
)



向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI