SQL Server Management Studio 2012中的自动递增主键 [英] Auto increment primary key in SQL Server Management Studio 2012

查看:107
本文介绍了SQL Server Management Studio 2012中的自动递增主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在SQL Server数据库表中auto increment primary key,我已经浏览了整个论坛,但看不到如何.

How do I auto increment the primary key in a SQL Server database table, I've had a look through the forum but can't see how.

我看过属性,但是看不到选项,我看到了一个答案,您可以转到Identity规范属性并将其设置为yes并将Identity increment设置为1,但是该部分变灰,我无法将否"更改为是".

I've looked the the properties but can't see an option, I have seen an answer where you go to the Identity specification property and set it to yes and set the Identity increment to 1, but that section is grayed out and I can't change the no to yes.

必须有一种简单的方法来执行此操作,但我找不到它.

There must be a simple way to do this but I can't find it.

推荐答案

确保Key列的数据类型为int,然后手动设置身份,如图所示

Make sure that the Key column's datatype is int and then setting identity manually, as image shows

只需运行此代码

-- ID is the name of the  [to be] identity column
ALTER TABLE [yourTable] DROP COLUMN ID 
ALTER TABLE [yourTable] ADD ID INT IDENTITY(1,1)

如果ID不是表中的唯一列,则代码将运行

the code will run, if ID is not the only column in the table

图像参考fifo的

这篇关于SQL Server Management Studio 2012中的自动递增主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆