PostgreSQL相当于SQL Server的IDENTITY(1、2)

时间:2019-01-17 16:40:12

标签: sql postgresql auto-increment

具有此示例表:

create table testingCase (
id integer not null GENERATED ALWAYS AS IDENTITY,
constraint pk_testingCase primary key (id),
description varchar(60)
);

例如,我希望SQL ServerIDENTITY (1, 2)的ID为 AUTO INCREMENTED (自动增加2)(例如)。

如何使用PostgreSQL来实现?

1 个答案:

答案 0 :(得分:2)

使用CREATE SEQUENCE.

中的顺序选项
create table testing_case (
    id integer not null generated always as identity (increment by 2),
    constraint pk_testing_case primary key (id),
    description varchar(60)
);

insert into testing_case (description) 
values ('a'), ('b'), ('c')
returning *

 id | description 
----+-------------
  1 | a
  3 | b
  5 | c
(3 rows)