简体   繁体   English

如何在SQL Server中添加标识列?

[英]How to add Identity Column in SQL server?

在此处输入图片说明

Here is my table and Id is unique constraint...Now I want to add Identity Column in ID .Can anyone help me how to do it with TSQL. 这是我的表,Id是唯一的约束...现在我想在ID中添加“身份列”。任何人都可以帮助我如何使用TSQL。 This is existing table. 这是现有表。

You can only have 1 Identity Column per Table. 每个表只能有1个标识列。 So if you don't have one already, Just alter the table and add the Column. 因此,如果您还没有一个,只需更改表并添加列。 Like this 像这样

ALTER TABLE YourTableName
ADD IdCol INT IDENTITY(1,1)

You can rename your previous ID column to keep those values if you need them in your application 您可以重命名以前的ID列以保留这些值(如果需要在应用程序中使用它们)

EXEC sp_rename 'TableName.id', 'oldid', 'COLUMN';

Then add new ID column with Unique constraint as follows 然后添加具有唯一性约束的新ID列,如下所示

Alter table TableName Add id int identity(1,1) unique not null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM