向 SQL SERVER 2005 中的现有表添加自动递增主键

问题描述:

我有一个包含 8,000 行数据的表格,我将添加更多数据.但我忘了在开头放一个主键.这样每一行都有一个唯一的键.后来我添加了一个主键列.但该列现在为 NULL.

I have a table with 8,000 rows of data and will be adding more. but I forgot to put a primary key in the beginning. so that each row has a unique key. later i added a primary key column. but that column is NULL now.

我希望第一行从 ID 1 开始并一直递增到 ID 8000 处的最后一行.如何使用单个查询更新所有行?

I want the first row to start at ID 1 and increment all the way up to the last row at ID 8000. How do I update all of the rows with a single query?

我使用的是 SQL Server 2005.

i am using SQL Server 2005.

Open Table Design, Add New Column u want 选择该列并在 Properties身份规范 make (是身份) 是的..您可以通过设置 Identity Seed 属性从您想要的位置开始,默认情况下它从 1 开始.

Open Table Design, Add New Column u want Select the column and in Properties In Identity Specification make (Is Identity) Yes.. You can start from where you want by setting the Identity Seed property, by Default it starts from 1.

如果您已经有身份栏,您也可以更新它.
步骤 1:从表格设计中的列中删除标识规范.
第 2 步:使用 Cursor 从 1 开始更新表列.
第 3 步:再次在表格设计中对列应用标识规范
第 4 步:通过查询从您想要的值重置标识列.例如
DBCC CHECKIDENT("TableName",Reseed,8000);
所以下一个标识值将是 8001.

If you have already Identity Column u can also update it.
Step 1: Remove Identity Specification from Column in Table Design.
Step 2: Use Cursor to Update table Column starting from 1.
Step 3: Again apply Identity Specification on Column in Table Design
Step 4: By query reset Identity Column, from the value u want. e.g
DBCC CHECKIDENT("TableName",Reseed,8000);
so the next identity value will be 8001.