简体   繁体   English

标识列是否从指定的种子开始

[英]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 . 参考下面所附的来自SQL Server Management Studio的图像,我已经有一个包含记录的表,最后一条记录的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 . 我希望将此列自动递增作为主键(就像在进行一些未知填充之前所做的那样),显然下一条记录的id of 8

在此处输入图片说明

Will specifying the seed as 7 mean the next record will be given an id of 7? 将种子指定为7意味着下一个记录的ID将为7吗? Or will it be (7 + incrementValue) giving 8? 还是(7 +增量值)等于8?

SQL Server is intelligent enough, any value you give from 1 to 8 it will start from 8 only. SQL Server非常智能,您从1到8给出的任何值都只会从8开始。

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. 例如,如果将“ Identity Seed为1,并在表中插入一条记录,SQL将仅将新的“ Identity计算为8。

If you specify > 8 in that case only it will consider the seed value. 如果在这种情况下指定> 8,则仅考虑种子值。

Note: When you are using SSMS to achieve this, internally table will be dropped and a new table will be created with proper identity. 注意:当您使用SSMS实现此目的时,将在内部删除表,并使用正确的身份创建一个新表。

If you want to do this using an sql query, you have to follow the instruction mentioned in this answer 如果要使用sql查询来执行此操作,则必须遵循此答案中提到的说明

Suppose your table is large,then you have to query to find max id 假设您的桌子很大,那么您必须查询以找到最大ID

Simply do this, 简单地做到这一点,

DBCC CHECKIDENT ('TableName', RESEED, 1) 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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