1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務時間:8:30-17:00
      你可能遇到了下面的問題
      關閉右側工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      SQL基礎知識--identifyseedoverflow

      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 

      SQL 基礎知識 -- identify seed overflow

      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)建測試表:

      SQL 基礎知識 -- identify seed overflow

      --創(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]

      SQL 基礎知識 -- identify seed overflow

      (二) 顯示插入Id值,插入后表[Test_Identity]的記錄如Figure1所示,接著再隱式插入Id值,插入后表[Test_Identity]的記錄如Figure2所示。

      SQL 基礎知識 -- identify seed overflow

      --顯示插入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'

      SQL 基礎知識 -- identify seed overflow

      SQL 基礎知識 -- identify seed overflow

      (Figure1:數(shù)據(jù)記錄)

      SQL 基礎知識 -- identify seed overflow

      (Figure2:數(shù)據(jù)記錄)

      (三) DBCC CHECKIDENT('table_name', NORESEED)不重置當前標識值。DBCC CHECKIDENT 返回一個報表,它指明當前標識值和應有的標識值。執(zhí)行下面的SQL語句,返回的信息表示:當前標識值'1001',當前列值'1001',如Figure2所示。

      SQL 基礎知識 -- identify seed overflow

      --查詢標識值DBCC CHECKIDENT('Test_Identity', NORESEED)/*檢查標識信息: 當前標識值'1001',當前列值'1001'。
      DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/

      SQL 基礎知識 -- identify seed overflow

      (四) 再隱式插入Id值,插入后表[Test_Identity]的記錄如Figure3所示。所以執(zhí)行上面的SQL語句是不會重置當前標識值的,可以放心執(zhí)行。

      --隱式插入Id值INSERT INTO [Test_Identity](Name)SELECT 'name3'

      SQL 基礎知識 -- identify seed overflow

      (Figure3:數(shù)據(jù)記錄)

      SQL 基礎知識 -- identify seed overflow

      --查詢標識值DBCC CHECKIDENT('Test_Identity', NORESEED)/*檢查標識信息: 當前標識值'1002',當前列值'1002'。
      DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/

      SQL 基礎知識 -- identify seed overflow

      (五) DBCC CHECKIDENT ('table_name') 或DBCC CHECKIDENT ('table_name', RESEED) 如果表的當前標識值小于列中存儲的最大標識值,則使用標識列中的最大值對其進行重置。

      因為上面返回結果是:當前標識值'1002',當前列值'1002',所以執(zhí)行下面的SQL語句是沒有影響的,什么時候才有影響呢?參考:(當在Figure4狀態(tài)下執(zhí)行下面的SQL命令,結果就會如Figure7所示

      SQL 基礎知識 -- identify seed overflow

      --重置標識值DBCC CHECKIDENT('Test_Identity', RESEED)/*檢查標識信息: 當前標識值'1002',當前列值'1002'。
      DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/

      SQL 基礎知識 -- identify seed overflow

      (六) 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語句返回的信息如下所示;

      SQL 基礎知識 -- identify seed overflow

      --重置標識值DBCC CHECKIDENT('Test_Identity', RESEED, 995)/*檢查標識信息: 當前標識值'1002',當前列值'995'。
      DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/

      SQL 基礎知識 -- identify seed overflow

      2) 繼續(xù)往[Test_Identity]表插入數(shù)據(jù),執(zhí)行下面的SQL語句插入后的結果如Figure4所示;插入的Id值為new_reseed_value + 1 = 996;

      --隱式插入Id值INSERT INTO [Test_Identity](Name)SELECT 'name4'

      SQL 基礎知識 -- identify seed overflow

      (Figure4:數(shù)據(jù)記錄)

      3) 查看現(xiàn)在的標識值,與上面的進行對比,你就可以理解【當前標識值】與【當前列值】的意義了;

      SQL 基礎知識 -- identify seed overflow

      --查詢標識值DBCC CHECKIDENT('Test_Identity', NORESEED)/*檢查標識信息: 當前標識值'996',當前列值'1002'。
      DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/

      SQL 基礎知識 -- identify seed overflow

      4) 繼續(xù)往[Test_Identity]表插入數(shù)據(jù),執(zhí)行3次后表的數(shù)據(jù)如Figure5所示;

      --隱式插入Id值INSERT INTO [Test_Identity](Name)SELECT 'name5'

      SQL 基礎知識 -- identify seed overflow

      (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]表,再重新設置標識值,返回的信息如下面所示;

      SQL 基礎知識 -- identify seed overflow

      --清空表truncate table [Test_Identity]--重置標識值DBCC CHECKIDENT('Test_Identity', RESEED, 995)/*檢查標識信息: 當前標識值'NULL',當前列值'995'。
      DBCC 執(zhí)行完畢。如果DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。*/

      SQL 基礎知識 -- identify seed overflow

      7) 這個時候往[Test_Identity]表插入數(shù)據(jù),數(shù)據(jù)就如Figure6所示,這說明了:“如果自創(chuàng)建表后沒有將行插入該表,則在執(zhí)行 DBCC CHECKIDENT 后插入的第一行將使用 new_reseed_value 作為標識。

      --隱式插入Id值INSERT INTO [Test_Identity](Name)SELECT 'name5'

      SQL 基礎知識 -- identify seed overflow

      (Figure6:數(shù)據(jù)記錄)

      SQL 基礎知識 -- identify seed overflow

      (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

      SQL 基礎知識 -- identify seed overflow

      (Figure8:數(shù)據(jù)記錄)

      --重置標識值DBCC CHECKIDENT('Test_Identity', RESEED, 996)--隱式插入Id值INSERT INTO [Test_Identity](Name)SELECT 'name6'

      SQL 基礎知識 -- identify seed overflow

      (Figure9:數(shù)據(jù)記錄)

      SQL 基礎知識 -- identify seed overflow

      (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
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        修文县| 宜昌市| 宁城县| 江华| 祁阳县| 通海县| 广州市| 从化市| 甘谷县| 定远县| 金阳县| 沙河市| 马公市| 南和县| 安岳县| 灵石县| 高淳县| 广饶县| 留坝县| 紫云| 石渠县| 乌兰浩特市| 永登县| 惠州市| 青河县| 卢湾区| 芜湖市| 宿迁市| 象山县| 临清市| 弥渡县| 汤阴县| 花垣县| 彝良县| 东安县| 宾川县| 醴陵市| 宣城市| 高密市| 玛多县| 威海市|