HSQL:尝试创建外键约束时出错

时间:2012-04-05 15:35:20

标签: hsqldb

以下是我的表创建DDL:

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 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 个答案:

答案 0 :(得分:1)

唯一索引不是唯一约束。您不需要唯一索引,但必须创建唯一约束。

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