直接在MS-Access中建立参考不是问题。
使用UCanAccess进行操作会导致“ net.ucanaccess.jdbc.UcanaccessSQLException:...”。

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection connection = DriverManager.getConnection("jdbc:ucanaccess://e:/TestDB.accdb;memory=true");
Statement statement = connection.createStatement();
//
String tableToBeReferenced = "PersonsTable";
String tableWithTheReferences = "RelationShipsTable";

try {// Tidy up
 statement.execute("DROP TABLE " + tableWithTheReferences);
} catch (Exception exeption) {}
try {// Tidy up
 statement.execute("DROP TABLE " + tableToBeReferenced);
} catch (Exception exeption) {}

statement.execute("CREATE TABLE " + tableToBeReferenced + "(ID autoincrement NOT NULL PRIMARY KEY,"//
    + "Name VARCHAR(255)"//
    + ")");

statement.execute("CREATE TABLE " + tableWithTheReferences + "(ID LONG NOT NULL PRIMARY KEY,"//
    + "RelationShip VARCHAR(255) NOT NULL DEFAULT 'FRIENDS',"//
    + "Person1Id LONG NOT NULL,"//
    + "Person2Id LONG NOT NULL)");

// reference #1
statement.execute("ALTER TABLE " + tableWithTheReferences + //
    " ADD CONSTRAINT FOREIGN_KEY_1 FOREIGN KEY (Person1Id) REFERENCES " //
    + tableToBeReferenced + "(ID) ON DELETE CASCADE");

// reference #2
statement.execute("ALTER TABLE " + tableWithTheReferences + //
    " ADD CONSTRAINT FOREIGN_KEY_2 FOREIGN KEY (Person2Id) REFERENCES " //
    + tableToBeReferenced + "(ID) ON DELETE CASCADE");


如果我仅创建第一个引用,那么它将起作用。
如果仅创建第二个引用,则它可以工作。

但是,当我尝试构建两个引用时,它将失败。

最佳答案

我可以在UCanAccess 4.0.3下重现该问题。 HSQLDB和Jackcess都没有在相同的两个表之间创建两个独立的FK关系的问题,因此看起来这可能是UCanAccess中的错误。我会将问题报告给UCanAccess开发团队,并用任何新闻更新此答案。

更新:

已解决此问题,并将其包含在UCanAccess 4.0.4版本中。

关于java - 如何通过UCanAccess创建具有两个对另一个表的外键引用的表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49160150/

10-10 03:10