简体   繁体   中英

Does identity column start at the specified seed

Referring to the image attached below which is from SQL Server Management Studio, I have a table with records in it already, and the last record has an id of 7 . I wish to have this column auto increment as the primary key (as it did before some unknown stuff-up), obviously with the next record having an id of 8 .

在此处输入图片说明

Will specifying the seed as 7 mean the next record will be given an id of 7? Or will it be (7 + incrementValue) giving 8?

SQL Server is intelligent enough, any value you give from 1 to 8 it will start from 8 only.

For example if you give the Identity Seed as 1, and insert a record to the table, SQL will calculate the new Identity as 8 only.

If you specify > 8 in that case only it will consider the seed value.

Note: When you are using SSMS to achieve this, internally table will be dropped and a new table will be created with proper identity.

If you want to do this using an sql query, you have to follow the instruction mentioned in this answer

Suppose your table is large,then you have to query to find max id

Simply do this,

DBCC CHECKIDENT ('TableName', RESEED, 1) 

我认为SQL Server将迫使您重新创建具有新标识的表,并且您将不得不将数据移到该表中,该表将从1开始并以1递增。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM