2012-04-05 86 views
0

以下時的錯誤是我的表的創建DDL:HSQL:試圖建立外鍵約束

create table REFDATA.CONFIG_COLLECTION (
COLLECTION_ID  NUMBER(22,7)      not null, 
COLLECTION_CD  VARCHAR2(12)     not null, 
COLLECTION_TYPE  VARCHAR2(12)     not null, 
COLLECTION_DESC  VARCHAR2(255), 
primary key (COLLECTION_ID) 
) 
create unique index REFDATA.CONFIG_COLLECTION_AK on REFDATA.CONFIG_COLLECTION (COLLECTION_CD); 

然後我有以下ALTER創建一個外鍵:

alter table REFDATA.CONFIG_COLLECTION_MEMBER 
add foreign key (COLLECTION_CD) 
references REFDATA.CONFIG_COLLECTION (COLLECTION_CD); 

我得到的當加載HSQLDB時出現以下錯誤:

Caused by: java.sql.SQLException: Primary or unique constraint required on main table: CONFIG_COLLECTION in statement [ 

alter table REFDATA.CONFIG_COLLECTION_MEMBER 
    add foreign key (COLLECTION_CD) 
     references REFDATA.CONFIG_COLLECTION (COLLECTION_CD)] 
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source) 
    at org.hsqldb.jdbc.jdbcStatement.fetchResult(Unknown Source) 
    at org.hsqldb.jdbc.jdbcStatement.execute(Unknown Source) 
    at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264) 
    at org.springframework.jdbc.core.JdbcTemplate$1ExecuteStatementCallback.doInStatement(JdbcTemplate.java:420) 
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:395) 
    ... 46 more 

這裏有什麼問題?它看起來像我有正確指定的唯一約束。這是HSQLDB的1.8版本。

回答

1

唯一索引不是唯一約束。您不需要唯一的索引,但必須創建一個唯一的約束。

ALTER TABLE REFDATA.CONFIG_COLLECTION ADD CONSTRAINT CONFIG_COLLECTION_AK UNIQUE(COLLECTION_CD);