如何从本地sql表中删除id列或将其更改为自动增量?

v9tzhpje  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(340)

我想去掉添加到本地sql表中的id列。
当我从设计器中删除该列并尝试更新时,得到以下结果:

另一个选项是通过将id列更改为“[id]int not null auto increment”,使其自动递增,但当我将其添加到表定义并选择update时,也出现了一个错误。
即使我将表更改回原来的状态(添加id列),如下所示:

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

…当我尝试更新时仍然收到错误消息。。。

zy1mlcev

zy1mlcev1#

我们需要定义列,如下所示。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> ]

所以,这里是:

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

vybvopom2#

基于此,以下几点应该可以做到:

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

(看,在“null”和“primary key”之间插入“identity(1,1)”jazz

相关问题