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

user1154644 picture user1154644 · Sep 30, 2013 · Viewed 37.8k times · Source

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?

Answer

Thomas Mueller picture Thomas Mueller · Sep 30, 2013

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.