SQL Server Management Studio 2012 中的自动递增主键

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

问题描述

如何自动递增 SQL Server 数据库表中的主键?我浏览了论坛,但不知道如何执行此操作.

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 to do this.

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

I've looked at the properties but can't see an option. I saw 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,然后手动设置identity,如图

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 中的自动递增主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)