如何在Apache Derby中声明外键?

问题描述

我正在尝试制定R.D.B.目前,但是我似乎无法使外键正常工作。运行该程序时,将创建两个没有外键的表(Word和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());

解决方法

此语法:

typeof

表示您希望表constraint WordID_FK FOREIGN KEY (WordID) REFERENCES Words(WordID) 中的列WordID是对表Index中列WordID的引用。

但是,如消息所示,您未在表Words中定义名为WordID的列。您的Index表只有三列:IndexIndexIDIndexPage

您可能想拥有类似的东西

IndexOccurrences

在表WordID integer,的定义中。