简体   繁体   中英

PostgreSQL equivalent of SQL Server's IDENTITY(1, 2)

Having this sample table:

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

I want the id to be AUTO INCREMENTED by 2 (for example), in SQL Server that'd be IDENTITY (1, 2) .

How can this be achieved utilizing PostgreSQL?

Use sequence options like in 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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