Search code examples
databaseauto-incrementh2

auto increment ID in H2 database


Is there a way to have an auto_incrementing BIGINT ID for a table. It can be defined like so

id bigint auto_increment

but that has no effect (it does not increment automatically). I would like to insert all fields but the ID field - the ID field should be provided by the DBMS. Or do I need to call something to increment the ID counter?


Solution

  • It works for me. JDBC URL: jdbc:h2:~/temp/test2

    drop table test;
    create table test(id bigint auto_increment, name varchar(255));
    insert into test(name) values('hello');
    insert into test(name) values('world');
    select * from test; 
    

    result:

    ID  NAME  
    1   hello
    2   world