新聞中心
DBCC CHECKIDENT (Transact-SQL)
Checks the current identity value for the specified table in SQL Server 2016 and, if it is needed, changes the identity value. You can also use DBCC CHECKIDENT to manually set a new current identity value for the identity column.
Permissions
站在用戶的角度思考問題,與客戶深入溝通,找到驛城網(wǎng)站設計與驛城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設計制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)絡空間、企業(yè)郵箱。業(yè)務覆蓋驛城地區(qū)。
Caller must own the schema that contains the table, or be a member of the sysadminfixed server role, the db_ownerfixed database role, or the db_ddladminfixed database role.
Examples
A. Resetting the current identity value, if it is needed
The following example resets the current identity value, if it is needed, of the specified table in the AdventureWorks2012database.
USE AdventureWorks2012; GO DBCC CHECKIDENT ('Person.AddressType'); GO
B. Reporting the current identity value
The following example reports the current identity value in the specified table in the AdventureWorks2012database, and does not correct the identity value if it is incorrect.
USE AdventureWorks2012; GO DBCC CHECKIDENT ('Person.AddressType', NORESEED); GO
C. Forcing the current identity value to a new value
The following example forces the current identity value in the AddressTypeID
column in the AddressType
table to a value of 10. Because the table has existing rows, the next row inserted will use 11 as the value, that is, the new current increment value defined for the column value plus 1.
USE AdventureWorks2012;
GO
DBCC CHECKIDENT ('Person.AddressType', RESEED, 10);
GO
https://msdn.microsoft.com/en-IN/library/ms176057.aspx
SQL Server 重置Identity標識列的值(INT爆了)
http://www.cnblogs.com/gaizai/archive/2013/04/23/3038318.html
一、背景
SQL Server數(shù)據(jù)庫中表A中Id字段的定義是:[Id] [int] IDENTITY(1,1),隨著數(shù)據(jù)的不斷增長,Id值已經(jīng)接近2147483647(int的取值范圍為:-2 147 483 648 到 2 147 483 647)了,雖然已經(jīng)對舊數(shù)據(jù)進行歸檔,但是這個表需要保留最近的1億數(shù)據(jù),有什么方法解決Id值就快爆的問題呢?
解決上面的問題有兩個辦法:一個是修改表結構,把Id的int數(shù)據(jù)類型修改為bigint;第二個是重置Id(Identity標識列)的值,使它重新增長。
當前標識值:current identity value,用于記錄和保存最后一次系統(tǒng)分配的Id值;下次分配Id就是:當前標識值+標識增量(通常為+1,也可以自行設置);
當前列值:current column value,這Id值到目前為止的最大值;
二、重置過程
(一) 下面就測試重置Identity標識列,首先使用下面的SQL創(chuàng)建測試表:
--創(chuàng)建測試表CREATE TABLE [dbo].[Test_Identity]( [IdentityId] [int] IDENTITY(1,1) NOT NULL, [Name] [nchar](10) NULL, CONSTRAINT [PK_testid] PRIMARY KEY CLUSTERED ( [IdentityId] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
(二) 顯示插入Id值,插入后表[Test_Identity]的記錄如Figure1所示,接著再隱式插入Id值,插入后表[Test_Identity]的記錄如Figure2所示。
--顯示插入Id值SET IDENTITY_INSERT [Test_Identity] ONINSERT INTO [Test_Identity](IdentityId,Name)SELECT 1000,'name1'SET IDENTITY_INSERT [Test_Identity] OFF--隱式插入Id值INSERT INTO [Test_Identity](Name)SELECT 'name2'
(Figure1:數(shù)據(jù)記錄)
(Figure2:數(shù)據(jù)記錄)
(三) DBCC CHECKIDENT('table_name', NORESEED)不重置當前標識值。DBCC CHECKIDENT 返回一個報表,它指明當前標識值和應有的標識值。執(zhí)行下面的SQL語句,返回的信息表示:當前標識值'1001',當前列值'1001',如Figure2所示。
--查詢標識值DBCC CHECKIDENT('Test_Identity', NORESEED)/*檢查標識信息: 當前標識值'1001',當前列值'1001'。 DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/
(四) 再隱式插入Id值,插入后表[Test_Identity]的記錄如Figure3所示。所以執(zhí)行上面的SQL語句是不會重置當前標識值的,可以放心執(zhí)行。
--隱式插入Id值INSERT INTO [Test_Identity](Name)SELECT 'name3'
(Figure3:數(shù)據(jù)記錄)
--查詢標識值DBCC CHECKIDENT('Test_Identity', NORESEED)/*檢查標識信息: 當前標識值'1002',當前列值'1002'。 DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/
(五) DBCC CHECKIDENT ('table_name') 或DBCC CHECKIDENT ('table_name', RESEED) 如果表的當前標識值小于列中存儲的最大標識值,則使用標識列中的最大值對其進行重置。
因為上面返回結果是:當前標識值'1002',當前列值'1002',所以執(zhí)行下面的SQL語句是沒有影響的,什么時候才有影響呢?參考:(當在Figure4狀態(tài)下執(zhí)行下面的SQL命令,結果就會如Figure7所示)
--重置標識值DBCC CHECKIDENT('Test_Identity', RESEED)/*檢查標識信息: 當前標識值'1002',當前列值'1002'。 DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/
(六) DBCC CHECKIDENT('table_name', RESEED, new_reseed_value)當前值設置為 new_reseed_value。如果自創(chuàng)建表后沒有將行插入該表,則在執(zhí)行 DBCC CHECKIDENT 后插入的第一行將使用 new_reseed_value 作為標識。否則,下一個插入的行將使用 new_reseed_value + 1。如果 new_reseed_value 的值小于標識列中的最大值,以后引用該表時將產(chǎn)生 2627 號錯誤信息。
要理解上面的描述,可以進行下面的測試:
1) 重新設置當前值設置為new_reseed_value = 995,執(zhí)行下面的SQL語句返回的信息如下所示;
--重置標識值DBCC CHECKIDENT('Test_Identity', RESEED, 995)/*檢查標識信息: 當前標識值'1002',當前列值'995'。 DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/
2) 繼續(xù)往[Test_Identity]表插入數(shù)據(jù),執(zhí)行下面的SQL語句插入后的結果如Figure4所示;插入的Id值為new_reseed_value + 1 = 996;
--隱式插入Id值INSERT INTO [Test_Identity](Name)SELECT 'name4'
(Figure4:數(shù)據(jù)記錄)
3) 查看現(xiàn)在的標識值,與上面的進行對比,你就可以理解【當前標識值】與【當前列值】的意義了;
--查詢標識值DBCC CHECKIDENT('Test_Identity', NORESEED)/*檢查標識信息: 當前標識值'996',當前列值'1002'。 DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/
4) 繼續(xù)往[Test_Identity]表插入數(shù)據(jù),執(zhí)行3次后表的數(shù)據(jù)如Figure5所示;
--隱式插入Id值INSERT INTO [Test_Identity](Name)SELECT 'name5'
(Figure5:數(shù)據(jù)記錄)
5) 如果現(xiàn)在繼續(xù)往[Test_Identity]表插入數(shù)據(jù)會發(fā)生什么事情呢?將產(chǎn)生 2627 號錯誤信息,如下面的錯誤信息;
消息2627,級別14,狀態(tài)1,第2 行
違反了PRIMARY KEY 約束'PK_testid'。不能在對象'dbo.Test_Identity' 中插入重復鍵。
語句已終止。
6) 下面來測試創(chuàng)建表后沒有插入行,如果這個時候執(zhí)行重置標識值會發(fā)生什么事情?清空[Test_Identity]表,再重新設置標識值,返回的信息如下面所示;
--清空表truncate table [Test_Identity]--重置標識值DBCC CHECKIDENT('Test_Identity', RESEED, 995)/*檢查標識信息: 當前標識值'NULL',當前列值'995'。 DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/
7) 這個時候往[Test_Identity]表插入數(shù)據(jù),數(shù)據(jù)就如Figure6所示,這說明了:“如果自創(chuàng)建表后沒有將行插入該表,則在執(zhí)行 DBCC CHECKIDENT 后插入的第一行將使用 new_reseed_value 作為標識。”
--隱式插入Id值INSERT INTO [Test_Identity](Name)SELECT 'name5'
(Figure6:數(shù)據(jù)記錄)
(Figure7:數(shù)據(jù)記錄)
8) 假如我們刪除了IdentityId為1000和1001的記錄,這個時候繼續(xù)插入數(shù)據(jù),會重新生成1000和10001值嗎?效果如Figure10所示(重新覆蓋了);
--刪除和delete from [Test_Identity] where IdentityId=1000delete from [Test_Identity] where IdentityId=1001
(Figure8:數(shù)據(jù)記錄)
--重置標識值DBCC CHECKIDENT('Test_Identity', RESEED, 996)--隱式插入Id值INSERT INTO [Test_Identity](Name)SELECT 'name6'
(Figure9:數(shù)據(jù)記錄)
(Figure10:數(shù)據(jù)記錄)
(七) 總結:到這里,我們已經(jīng)可以解決Id值就快爆的問題了,因為我們舊的數(shù)據(jù)會定時歸檔,所以不會出現(xiàn)2627錯誤信息;而另外一個場景是當出現(xiàn)Figure5的時候,可以執(zhí)行DBCC CHECKIDENT('Test_Identity', RESEED),設置為當前列最大值為標識值,防止出現(xiàn)2627錯誤信息。
三、補充說明
在MySQL中,也有類似Identity的功能:
`IDs` int(11) unsigned NOT NULL AUTO_INCREMENT
在創(chuàng)建表的時候,會有一個選項AUTO_INCREMENT=17422061,直接可以設置起始值,還可以設置步長:
SHOW VARIABLES LIKE 'auto_inc%';
起始值:auto_increment_offset
步長:auto_increment_increment
SET @auto_increment_increment=10;
SELECT LAST_INSERT_ID();
網(wǎng)頁名稱:SQL基礎知識--identifyseedoverflow
文章分享:http://ef60e0e.cn/article/jhspdd.html