如何在Apache Derby中声明外键?

huangapple go评论54阅读模式
英文:

How to declare foreign keys in Apache Derby?

问题

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

Here is my code:

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

英文:

I am trying to make a R.D.B. at the moment, but I can't seem to get the foreign keys working. When running the program, the two tables without foreign keys (Words and PDFs) are created and then it has a run-time error at the Index table:

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

Here is my code:

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

答案1

得分: 0

这个语法:

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

表示你希望在表格Index中的列WordID成为表格Words中列WordID的引用。

但是,正如信息所述,你并没有在表格Index中定义名为WordID的列。你的Index表格只有三个列:IndexIDIndexPageIndexOccurrences

你可能希望在定义Index表格时加入类似于

WordID integer,

的内容。

英文:

This syntax:

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

says that you want the column WordID in the table Index to be a reference to the column WordID in the table Words.

But you did not define a column named WordID in the table Index, as the message says. Your Index table has only three columns: IndexID, IndexPage, and IndexOccurrences.

You probably want to have something like

WordID integer,

in your definition of table Index.

huangapple
  • 本文由 发表于 2020年9月5日 22:27:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63755005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定