如何在 Apache Derby 中声明外键?

How to declare foreign keys in Apache Derby?

我正在尝试制作 R.D.B。目前,但我似乎无法让外键工作。当 运行ning 程序时,创建了两个没有外键的 table(Word 和 PDF),然后它在索引 table 处出现 运行 时间错误:

Table 'INDEX' contains a constraint definition with column 'WORDID' which is not in the table. Derby shut down normally

这是我的代码:

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());

此语法:

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

表示您希望 table Index 中的 WordID 列是对 table [=15] 中的 WordID 列的引用=].

但是您没有在 table Index 中定义名为 WordID 的列,如消息所述。您的 Index table 只有三列:IndexIDIndexPageIndexOccurrences.

你可能想要这样的东西

WordID integer,

在你的定义中 table Index.