重置SQLSERVER表的自增列,讓自增列重新計數

2021-10-24 12:16:44 字數 360 閱讀 8983

sql的自增列挺好用,只是開發過程中一旦刪除資料,標識列就不連續了 寫起來 也很鬱悶,所以查閱了一下標識列重置的方法 發現可以分為三種:

--- 刪除原表資料,並重置自增列

truncate table tablename  --truncate方式也可以重置自增字段

--重置表的自增欄位,保留資料

dbcc checkident (tablename,reseed,0)

-- 設定允許顯式插入自增列

set identity_insert tablename  on

-- 當然插入完畢記得要設定不允許顯式插入自增列

set identity_insert tablename  off

sql server 重置自增

delete tb where id 100 dbcc checkident tb reseed,1 dbcc checkident tb reseed 其結果將 tb 表的自增種子設定為當前表裡標識列的最大值 再次增加資料就會繼續那個數字了 實測 建立測試環境 if object id tb is...

mysql重置所有表自增id

建立儲存過程之前需判斷該儲存過程是否已存在,若存在則刪除 drop procedure ifexists init sql string 建立儲存過程 create procedure init sql string begin 定義變數 declare s int default0 declare...

重置SQLSERVER表的自增列,讓自增列重新計數

sql的自增列挺好用,只是開發過程中一旦刪除資料,標識列就不連續了 寫起來 也很鬱悶,所以查閱了一下標識列重置的方法 發現可以分為三種 刪除原表資料,並重置自增列 truncate table tablename truncate方式也可以重置自增字段 重置表的自增欄位,保留資料 dbcc chec...