【问题标题】:How to declare foreign keys in Apache Derby?如何在 Apache Derby 中声明外键?
【发布时间】:2020-09-05 14:27:50
【问题描述】:

我正在尝试制作 R.D.B.目前,但我似乎无法让外键工作。运行程序时,会创建两个没有外键的表(Words 和 PDFs),然后在 Index 表处出现运行时错误:

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

【问题讨论】:

    标签: java foreign-keys derby


    【解决方案1】:

    这个语法:

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

    表示您希望表 Index 中的列 WordID 引用表 Words 中的列 WordID

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

    你可能想要类似的东西

    WordID integer,
    

    在你对表Index的定义中。

    【讨论】:

      猜你喜欢
      • 2014-06-27
      • 1970-01-01
      • 2013-08-20
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多