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

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

我想删除添加到本地的 ID 列 sql table。

当我从设计器中删除列并尝试更新时,我得到了这个:

另一种选择是通过将 ID 列更改为“[Id] INT NOT NULL AUTO INCREMENT”使 ID 列自动递增,但是当我将其添加到 table 定义时我也遇到了错误并选择更新。

即使我将 Table 改回原样(添加 ID 列),如下所示:

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

...我尝试更新时仍然收到错误消息...

基于this,以下应该可以解决问题:

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

(IOW,在 "NULL" 和 "PRIMARY KEY"

之间闯入 "IDENTITY(1,1)" 爵士乐

我们需要如下定义列。 MSDN REFERENCE

<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> ]

所以,这里是:

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)
);

或者您可以在单独的行中定义约束

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)
);