H2 Database Primary Key on Create -


if create new tabel in h2 database this...

create table mainincomer(timestamp timestamp, value real);

it means id hidden , auto increment when write tabel, right? - (as far have learnt tutorial , features)

will primary key assigned id automatically? , how important primary key anyway in type of table 2 columns? or should best practice create table primary key assigned timestamp?

create table mainincomer(timestamp timestamp primary key, value real);

thanks, alex

no: table not have id column, it's not listed in ddl create table.

the simplest way know adding such auto incrementing column used primary key id identity. careful it's specific h2 , not portable.

but it's shorter other database :-d

please change ddl full line:

create table mainincomer(id identity, timestamp timestamp, value real); 

you can test inside h2 web console this:

@loop 1000 insert mainincomer (timestamp) values (now()); 

this insert 1000 records , you'll see id defined bigint(19) not null, there primary key constraint assigned , it's autoincrementing nicely.

it's considered bad practice use timestamps primary key. it's hard ensure unique constraints, have event arriving @ same time (sort of, depends on time resolution). plus integer easier index , find back.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -