简体   繁体   中英

How to declare foreign keys in Apache Derby?

I am trying to make a RDB 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());

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM