user1154644
user1154644

Reputation: 4609

Creating a table in h2 database using predefined sequence for primary key

I am trying to create a table in an H2 database. How do I specify that the primary key should be generated from a sequence that has been created?

The sequence is called group_seq, and I created it using this statement:

CREATE SEQUENCE GROUP_SEQ;

So when I create the table, how do I specify that I want my primary key col (ID) to use that sequence?

Upvotes: 19

Views: 48373

Answers (1)

Thomas Mueller
Thomas Mueller

Reputation: 50087

If you want to use your own sequence:

create sequence group_seq;
create table test3(id bigint default group_seq.nextval primary key);

And if not:

create table test1(id identity);

or

create table test2(id bigint auto_increment primary key);

All this is documented in the H2 SQL grammar railroad diagrams.

Upvotes: 40

Related Questions