2013-05-15 14 views
2

爲Apache Derby進行下面的SQL語句工作正常:的Apache Derby:指定一個ID爲 「生成的默認身份」 一欄

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 

我怎麼能解決這個問題,我怎麼能有一個uto_increment列,我可以,有時,設置主鍵?

回答