Reputation: 35246
The following sql statements for apache derby works fine:
connect 'jdbc:derby://uri';
create schema TEST02;
set schema TEST02 ;
create table T
(
id INT not null primary key GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
name varchar(50) not null unique
);
insert into T(name) values ('NAME02');
insert into T(name) values ('NAME03');
select * from T;
drop table T ;
drop schema TEST02 RESTRICT;
disconnect;
output:
ij> insert into T(name) values ('NAME02');
1 row inserted/updated/deleted
ij> insert into T(name) values ('NAME03');
1 row inserted/updated/deleted
ij> select * from T;
ID |NAME
--------------------------------------------------------------
1 |NAME02
2 |NAME03
but when I 'know' the id of some records, and I set the id in my INSERT statement:
## here I set the id column
ij> insert into T(id,name) values (1,'NAME01');
1 row inserted/updated/deleted
ij> insert into T(name) values ('NAME02');
ERROR 23505: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL130515100041380' defined on 'T'.
ij> insert into T(name) values ('NAME03');
1 row inserted/updated/deleted
ij> select * from T;
ID |NAME
--------------------------------------------------------------
1 |NAME01
2 |NAME03
2 rows selected
how can I fix this, how can I have an auto_increment column where I can, sometimes, set the primary key ?
Upvotes: 2
Views: 4911
Reputation: 16359
You're looking for ALTER TABLE ... RESTART WITH. Here's the docs:
http://db.apache.org/derby/docs/10.9/ref/rrefsqlj81859.html#rrefsqlj81859__rrefsqlj37860
Upvotes: 1