有一个样例数据表:
create table testingCase (
id integer not null GENERATED ALWAYS AS IDENTITY,
constraint pk_testingCase primary key (id),
description varchar(60)
);
我希望id能够每次自动增加2(例如),在SQL Server
中可以使用IDENTITY (1, 2)
实现。
那么在PostgreSQL中如何实现呢?
有一个样例数据表:
create table testingCase (
id integer not null GENERATED ALWAYS AS IDENTITY,
constraint pk_testingCase primary key (id),
description varchar(60)
);
我希望id能够每次自动增加2(例如),在SQL Server
中可以使用IDENTITY (1, 2)
实现。
那么在PostgreSQL中如何实现呢?
使用类似于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)