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

编程入门 行业动态 更新时间:2024-10-28 00:17:29
本文介绍了向 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.

更多推荐

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

本文发布于:2023-10-26 10:13:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   SQL   SERVER

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!