创建表时“关系中缺少列”

时间:2019-10-02 18:19:56

标签: java sql ms-access jdbc ucanaccess

我尝试创建三个表(CUSTOMERS,VEHICLES和RENTALS),第三个表(RENTALS)具有引用前两个表的两个主键(CUSTOMERS和RENTALS)的外键。创建此第三张表时,出现错误关系中缺少列(Rel = CUSTOMERS [[]]-> RENTALS [[]])

这是我的代码

     private void createTables() throws SQLException {
        Statement statement = conn.createStatement();
        statement.executeUpdate("CREATE TABLE CUSTOMERS(custNumber AUTOINCREMENT PRIMARY KEY, " +
            "firstName VARCHAR(155) NOT NULL, surname VARCHAR(155) NOT NULL, idNum INTEGER NOT NULL, phoneNum INTEGER NOT NULL, canRent BIT NOT NULL)");
        statement.executeUpdate("CREATE TABLE VEHICLES(vehNumber AUTOINCREMENT PRIMARY KEY, make VARCHAR(155) NOT NULL, " +
            "category VARCHAR(155) NOT NULL, rentalPrice FLOAT NOT NULL, availableForRent BIT NOT NULL)");
        statement.executeUpdate("CREATE TABLE RENTALS(rentalNumber AUTOINCREMENT PRIMARY KEY, dateRental VARCHAR(155) NOT NULL, dateReturned VARCHAR(155) NOT NULL, " +
            "pricePerDay FLOAT NOT NULL, totalRental FLOAT NOT NULL, custNumber INTEGER FOREIGN KEY REFERENCES CUSTOMERS(custNumber), " +
            "vehNumber INTEGER FOREIGN KEY REFERENCES VEHICLES(vehNumber))");

        System.out.println("Database populated");
     }

这是错误 enter image description here

非常感谢您的帮助,我环顾四周,但没有发现任何帮助。

1 个答案:

答案 0 :(得分:3)

在Access中,“自动编号”字段(DDL:AUTOINCREMENTCOUNTER)是“长整数”。

在UCanAccess DDL中,INTEGER创建一个“整数”(16位)字段,而LONG创建一个“长整数”(32位)字段。

您需要将外键列声明为LONG,而不是INTEGER