Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

sql-server - SQL Server Management Studio 2012中的自动递增主键(Auto increment primary key in SQL Server Management Studio 2012)

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.

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

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.

(我看过属性,但看不到选项,我看到了一个答案,您可以转到Identity规范属性,并将其设置为是,并将Identity increment设置为1,但是该部分显示为灰色,我可以不要将“否”更改为“是”。)

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

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

  ask by Ledgemonkey translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

在此处输入图片说明

Or just run this code

(或者只是运行此代码)

-- 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)

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

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

image reference fifo's

(图像参考fifo)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...