java - Compound primary key with autoincrement part -


i'm using hsqldb db engine. want create table has compound primary key. use code:

create table sample_table ( hash varchar(20),  id integer, data varchar(256), primary key(hash, id) ) 

but need id autoincremental. mean, when want have this:

hash id data


abc 1 data_string_1 abc 2 data_string_2 efg 1 some_string_1 abc 3 some_data_again efg 2 last_string

if use code:

create table sample_table ( hash varchar(20),  id integer identity, data varchar(256), primary key(hash, id) ) 

(so make id autoincremental primary key) exception thrown:

java.sql.sqlsyntaxerrorexception: primary key exist

what can solve problem? me, using multiple queries count (id) , increment count , insert new value not good, want hsqldb automatically

identity implies primary key.

but can use sequence generate id without need declare identity column:

create sequence sample_sequence; create table sample_table  (    hash  varchar(20),    id    integer generated default sequence sample_sequence,    data  varchar(256),    primary key(hash, id)  ); 

although have admit fail see use-case behind this. if auto-increment id, can wind duplicate values hash column combination of both has unique, not 1 of them.

could want id be identity (thus pk) , unique constraint on hash column - serving alternate key?

edit (based on fred's comment)

as alternative can use shorter version:

create table sample_table  (    hash  varchar(20),    id    integer generated default identity,    data  varchar(256),    primary key(hash, id)  ); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -