英文:
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
表格只有三个列:IndexID
、IndexPage
和IndexOccurrences
。
你可能希望在定义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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论