1

以下是我的表的创建 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 版。

4

1 回答 1

2

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

ALTER TABLE REFDATA.CONFIG_COLLECTION ADD CONSTRAINT CONFIG_COLLECTION_AK UNIQUE(COLLECTION_CD);
于 2012-04-05T16:15:06.577 回答