SQL SERVER重置自動編號列 標識列

2021-07-15 03:50:12 字數 1388 閱讀 8518

兩種方法:

一種是用truncate

truncate table name

可以刪除表內所有值並重置標識值

二是用dbcc checkident

dbcc checkident ('table_name', reseed, new_reseed_value)

如dbcc checkident ("bc_pos",reseed,1)即可,但如果表內有資料,則重設的值如果小於最大值可能會有問題,這時可以用 dbcc checkident("bc_pos",reseed)即可自動重設值。

3、判段乙個表是否具有標識列

可以使用 objectproperty 函式確定乙個表是否具有 identity(標識)列,用法:

select objectproperty(object_id('表名'),'tablehasidentity')

如果有,則返回1,否則返回0

4、判斷某列是否是標識列

可使用 columnproperty 函式確定 某列是否具有identity 屬性,用法

select columnproperty( object_id('表名'),'列名','isidentity')

如果該列為標識列,則返回1,否則返回0

4、查詢某錶標識列的列名

sql server中沒有現成的函式實現此功能,實現的sql語句如下

select column_name from information_schema.columns

where table_name='表名' and  columnproperty(      

object_id('表名'),column_name,'isidentity')=1

5、標識列的引用

如果在sql語句中引用標識列,可用關鍵字identitycol代替

例如,若要查詢上例中id等於1的行,

以下兩條查詢語句是等價的

select * from t_test where identitycol=1

select * from t_test where

6、獲取標識列的種子值

可使用函式ident_seed,用法:

select ident_seed ('表名')

7、獲取標識列的遞增量

可使用函式ident_incr ,用法:

select ident_incr('表名')

8、獲取指定表中最後生成的標識值

可使用函式ident_current,用法:

select ident_current('表名') 

注意事項:當包含標識列的表剛剛建立,為經過任何插入操作時,使用ident_current函式得到的值為標識列的種子值,這一點在開發資料庫應用程式的時候尤其應該注意。 

SQL SERVER重置自動編號列 標識列

兩種方法 一種是用truncate truncate table name 可以刪除表內所有值並重置標識值 二是用dbcc checkident dbcc checkident table name reseed,new reseed value 如dbcc checkident bc pos re...

SQL SERVER重置自動編號列 標識列

兩種方法 一種是用truncate truncate table name 可以刪除表內所有值並重置標識值 二是用dbcc checkident dbcc checkident table name reseed,new reseed value 如dbcc checkident bc pos re...

SQLite中重置自動編號列的方法

目前流行的資料庫都提供了自動編號型別,sqlite也不例外。當資料庫中包含自動編號的字段時,sqlite程式設計客棧會自動建立乙個名為 sqlite sequence 的表。這個表包含兩個字段 name 和 seq name欄位記錄了自動編號字段所在的表,seq欄位記錄了當前用到的序號 下一條記錄的...