简体   繁体   English

如何从本地 sql 表中删除 ID 列或将其更改为 AUTO INCREMENT?

[英]How can I remove the ID column from a local sql table or change it to AUTO INCREMENT?

I want to get rid of the ID column I added to a local sql table.我想摆脱我添加到本地 sql 表中的 ID 列。

When I deleted the column from the designer and tried to Update, I got this:当我从设计器中删除该列并尝试更新时,我得到了这个:

在此处输入图像描述

Another option would be to make the ID column AUTO INCREMENT by changing it to "[Id] INT NOT NULL AUTO INCREMENT,", but I also got an error when I added that to the table definition and selected Update.另一种选择是通过将 ID 列更改为“[Id] INT NOT NULL AUTO INCREMENT”来使 ID 列 AUTO INCREMENT,但是当我将其添加到表定义并选择更新时也出现错误。

Even when I change the Table back to what it is (add the ID column back) like so:即使我将表更改回原来的样子(添加 ID 列),如下所示:

CREATE TABLE [dbo].[WordsToIgnore] (
    [Id]           INT        NOT NULL,
    [WordToIgnore] NCHAR (50) NOT NULL,
    [Source]       NCHAR (50) NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

...I still get the err msg when I try to Update... ...当我尝试更新时,我仍然收到错误消息...

We need to define Column as given below.我们需要定义 Column 如下所示。 MSDN REFERENCE MSDN 参考

<column_definition>::= column_name <data_type> [ FILESTREAM ] [ COLLATE collation_name ] [ SPARSE ] [ MASKED WITH ( FUNCTION = ' mask_function ') ] [ CONSTRAINT constraint_name [ DEFAULT constant_expression ] ] [ IDENTITY [ ( seed,increment ) ] [ NOT FOR REPLICATION ] [ GENERATED ALWAYS AS ROW { START | END } [ HIDDEN ] ] [ NULL | NOT NULL ] [ ROWGUIDCOL ] [ ENCRYPTED WITH ( COLUMN_ENCRYPTION_KEY = key_name, ENCRYPTION_TYPE = { DETERMINISTIC | RANDOMIZED }, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256' ) ] [ <column_constraint> [, ...n ] ] [ <column_index> ]

So, here it is:所以,这里是:

CREATE TABLE [dbo].[WordsToIgnore] (
    Id INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_WordsToIgnore PRIMARY KEY CLUSTERED,
    [WordToIgnore] NCHAR (50) NOT NULL,
    [Source]       NCHAR (50) NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

Or you can define constraint in a separate line或者您可以在单独的行中定义约束

CREATE TABLE [dbo].[WordsToIgnore] (
    Id INT IDENTITY(1,1) NOT NULL ,
    [WordToIgnore] NCHAR (50) NOT NULL,
    [Source]       NCHAR (50) NOT NULL,
    CONSTRAINT PK_WordsToIgnore PRIMARY KEY CLUSTERED ([Id] ASC)
);

Based on this , the following should do the trick:基于,以下应该可以解决问题:

[Id] INT NOT NULL IDENTITY(1,1) PRIMARY KEY

(IOW, intrude the "IDENTITY(1,1)" jazz between "NULL" and "PRIMARY KEY" (IOW,在“NULL”和“PRIMARY KEY”之间插入“IDENTITY(1,1)”爵士乐

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

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