如何讓sqlserver自增列重新從1開始算起

2022-07-29 18:24:11 字數 668 閱讀 2878

資料庫原有id欄位,是自增列的,後來把資料全刪除後,想讓id自增列從1開始算起

方法1:

1.dbcc checkident('test',reseed,0)

2.insert into test values(55)

select *  from test

顯示結果:

id      msum

1        55

方法2:

set identity_insert 

允許將顯式值插入表的標識列中。

語法:set identity_insert [ database_name . [ schema_name ] . ] table 

set identity_insert dbo.test on

test是表名

注意:運用set identity_insert dbo.test on後,insert into時,必須要把需要插入記錄的字段寫上,如:

insert into test(id,msum)values(1,55)

insert into test(id,msum)values(2,55)

下面的語句的寫法是錯誤的:

insert into test values(55)

insert into test values(1,55)

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

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

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

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

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

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