如何在 SQL Server 数据库中添加 auto_increment 主键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6777734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:24:16  来源:igfitidea点击:

How do I add a auto_increment primary key in SQL Server database?

sqlsql-serverauto-increment

提问by dcp3450

I have a table set up that currently has no primary key. All I need to do is add a primary key, no null, auto_increment.

我设置了一个当前没有主键的表。我需要做的就是添加一个primary key, no null, auto_increment.

I'm working with a Microsoft SQL Serverdatabase. I understand that it can't be done in a single command but every command I try keeps returning syntax errors.

我正在使用Microsoft SQL Server数据库。我知道它不能在单个命令中完成,但我尝试的每个命令都不断返回语法错误。

edit ---------------

编辑 - - - - - - - -

I have created the primary key and even set it as not null. However, I can't set up the auto_increment.

我已经创建了主键,甚至将其设置为非空。但是,我无法设置auto_increment.

I've tried:

我试过了:

ALTER TABLE tableName MODIFY id NVARCHAR(20) auto_increment
ALTER TABLE tableName ALTER COLUMN id NVARCHAR(20) auto_increment
ALTER TABLE tableName MODIFY id NVARCHAR(20) auto_increment
ALTER TABLE tableName ALTER COLUMN id NVARCHAR(20) auto_increment

I'm using NVARCHARbecause it wouldn't let me set NOT NULLunder int

我正在使用,NVARCHAR因为它不会让我NOT NULL在 int 下设置

回答by gbn

It can be done in a single command. You need to set the IDENTITY property for "auto number":

它可以在单个命令中完成。您需要为“自动编号”设置 IDENTITY 属性:

ALTER TABLE MyTable ADD mytableID int NOT NULL IDENTITY (1,1) PRIMARY KEY

More precisely, to set a named table level constraint:

更准确地说,要设置命名表级约束:

ALTER TABLE MyTable
   ADD MytableID int NOT NULL IDENTITY (1,1),
   CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED (MyTableID)

See ALTER TABLE and IDENTITYon MSDN

请参阅MSDN 上的ALTER TABLE 和 IDENTITY

回答by Andreas ?gren

If the table already contains data and you want to change one of the columns to identity:

如果表已经包含数据并且您想将其中一列更改为标识:

First create a new table that has the same columns and specify the primary key-kolumn:

首先创建一个具有相同列的新表并指定主键-kolumn:

create table TempTable
(
    Id int not null identity(1, 1) primary key
    --, Other columns...
)

Then copy all rows from the original table to the new table using a standard insert-statement.

然后使用标准insert语句将原始表中的所有行复制到新表中。

Then drop the original table.

然后删除原始表。

And finally rename TempTableto whatever you want using sp_rename:

最后重命名TempTable为您想要使用的任何内容sp_rename

http://msdn.microsoft.com/en-us/library/ms188351.aspx

http://msdn.microsoft.com/en-us/library/ms188351.aspx

回答by ChrisCamp

You can also perform this action via SQL Server Management Studio.

您还可以通过 SQL Server Management Studio 执行此操作。

Right click on your selected table -> Modify

Right click on the field you want to set as PK --> Set Primary Key

Under Column Properties set "Identity Specification" to Yes, then specify the starting value and increment value.

右键单击您选择的表 -> 修改

右键单击要设置为 PK 的字段 --> 设置主键

在列属性下将“身份规范”设置为是,然后指定起始值和增量值。

Then in the future if you want to be able to just script this kind of thing out you can right click on the table you just modified and select

然后在将来,如果您希望能够将这种事情编写出来,您可以右键单击您刚刚修改的表并选择

"SCRIPT TABLE AS" --> CREATE TO

“脚本表为”--> 创建到

so that you can see for yourself the correct syntax to perform this action.

这样您就可以亲眼看到执行此操作的正确语法。

回答by gustavo herrera

If you have the column it's very easy.

如果你有专栏,那就很容易了。

Using the designer, you could set the column as an identity (1,1): right click on the table → design → in part left (right click) → properties → in identity columns, select #column.

使用设计器,您可以将列设置为标识 (1,1):右键单击表 → 设计 → 部分左键(右键单击)→ 属性 → 在标识列中,选择 #column。



Properties:

属性

enter image description here

在此处输入图片说明

Identity column:

身份栏

enter image description here

在此处输入图片说明

回答by Chandan Deb

In SQL Server 2008:

在 SQL Server 2008 中:

  • Right click on table
  • Go to design
  • Select numeric datatype
  • Add Name to the new column
  • Make Identity Specification to 'YES'
  • 右键单击表格
  • 去设计
  • 选择数字数据类型
  • 将名称添加到新列
  • 使身份规范为“是”

回答by Wayne Salazar

you can try this... ALTER TABLE Your_Table ADD table_ID int NOT NULL PRIMARY KEY auto_increment;

你可以试试这个... ALTER TABLE Your_Table ADD table_ID int NOT NULL PRIMARY KEY auto_increment;