Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sequence

ddl

h2

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?

like image 336
user1154644 Avatar asked Sep 30 '13 16:09

user1154644


1 Answers

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.

like image 161
Thomas Mueller Avatar answered Oct 01 '22 23:10

Thomas Mueller