如何在ApacheDerby中声明外键?

wko9yo5t  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(296)

我正在试着做一个r.d.b.,但外键好像没法用。运行程序时,会创建两个没有外键(words和pdf)的表,然后索引表中会出现运行时错误:
表“index”包含列“wordid”不在表中的约束定义。德比正常关机
这是我的密码:

new String createSQL3 = "create table Index (" 
        + " IndexID integer not null generated always as"
        + " identity (start with 1, increment by 1),"
        + " IndexPage integer not null, IndexOccurences integer not null,"
        + " constraint IndexID_PK primary key (IndexID),"
        + " constraint WordID_FK FOREIGN KEY (WordID) REFERENCES Words(WordID),"
        + " constraint PDFID_FK FOREIGN KEY (PDFID) REFERENCES PDFs(PDFID))";

        statement.execute(createSQL3);
        System.out.println("Table Index created successfully");

        connection.commit();

    } catch (SQLException EX) {
        System.out.println(EX.getMessage());
p4tfgftt

p4tfgftt1#

此语法:

constraint WordID_FK FOREIGN KEY (WordID) REFERENCES Words(WordID)

说你想要这个专栏 WordID 在table上 Index 作为对列的引用 WordID 在table上 Words .
但您没有定义名为 WordID 在table上 Index ,正如留言所说。你的 Index 表只有三列: IndexID , IndexPage ,和 IndexOccurrences .
你可能想喝点

WordID integer,

在你对表的定义中 Index .

相关问题