天天看点

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.

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

#1楼

参考:https://stackoom.com/question/k7Uc/SQL-Server-Management-Studio-中的自动递增主键

#2楼

You have to expand the Identity section to expose increment and seed.

您必须展开“身份”部分以显示增量和种子。
SQL Server Management Studio 2012中的自动递增主键

Edit: I assumed that you'd have an integer datatype, not char(10).

编辑:我假设您将有一个整数数据类型,而不是char(10)。

Which is reasonable I'd say and valid when I posted this answer

发布此答案时,我会说合理且有效

#3楼

Make sure that the Key column's datatype is

int

and then setting identity manually, as image shows

确保Key列的数据类型为

int

,然后手动设置身份,如图所示
SQL Server Management Studio 2012中的自动递增主键

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

#4楼

When you're creating the table, you can create an

IDENTITY

column as follows:

创建表时,可以创建一个

IDENTITY

列,如下所示:
CREATE TABLE (
  ID_column INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
  ...
);
           

The

IDENTITY

property will auto-increment the column up from number 1. (Note that the data type of the column has to be an integer.) If you want to add this to an existing column, use an

ALTER TABLE

command.

IDENTITY

属性将使列从1开始自动递增。(请注意,列的数据类型必须为整数。)如果要将其添加到现有列,请使用

ALTER TABLE

命令。

Edit:

编辑:

Tested a bit, and I can't find a way to change the Identity properties via the Column Properties window for various tables.

测试了一下,我找不到通过各种表的“列属性”窗口更改标识属性的方法。

I guess if you want to make a column an identity column, you HAVE to use an

ALTER TABLE

command.

我想如果您想将一列设置为标识列,则必须使用

ALTER TABLE

命令。

#5楼

When you're using Data Type: int you can select the row which you want to get autoincremented and go to the column properties tag.

使用数据类型:int时,可以选择要自动递增的行,然后转到列属性标签。

There you can set the identity to 'yes'.

在那里,您可以将身份设置为“是”。

The starting value for autoincrement can also be edited there.

自动增量的起始值也可以在此处进行编辑。

Hope I could help ;)

希望我能帮忙;)

#6楼

Expand your database, expand your table right click on your table and select design from dropdown.

展开数据库,展开表,右键单击表,然后从下拉列表中选择设计 。
SQL Server Management Studio 2012中的自动递增主键

Now go Column properties below of it scroll down and find Identity Specification , expand it and you will find Is Identity make it Yes.

现在,转到其下面的“ 列”属性 ,向下滚动并找到“ 身份规范” ,将其展开,您将发现“身份是否为Yes”。

Now choose Identity Increment right below of it give the value you want to increment in it.

现在,在它下面的“ 身份增量 ”中选择要增加的值。
SQL Server Management Studio 2012中的自动递增主键

继续阅读