【问题标题】:apache derby: specifying an ID for a column "GENERATED BY DEFAULT AS IDENTITY"apache derby:为“GENERATED BY DEFAULT AS IDENTITY”列指定一个 ID
【发布时间】:2013-05-09 16:31:07
【问题描述】:

以下 apache derby 的 sql 语句可以正常工作:

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;

输出:

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      

但是当我“知道”某些记录的 id 并在我的 INSERT 语句中设置 id 时:

## 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

我该如何解决这个问题,我怎样才能有一个 auto_increment 列,有时我可以设置主键?

【问题讨论】:

    标签: sql primary-key derby auto-increment javadb


    【解决方案1】:

    您正在寻找 ALTER TABLE ... RESTART WITH。这是文档:

    http://db.apache.org/derby/docs/10.9/ref/rrefsqlj81859.html#rrefsqlj81859__rrefsqlj37860

    【讨论】:

      猜你喜欢
      • 2019-09-25
      • 2018-02-08
      • 2018-05-18
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多