外键出错

时间:2010-11-15 11:24:02

标签: java mysql jdbc mysql-error-1005

我在Java程序中创建mySQL表时遇到问题。我不断得到无法创建表...错误:150

这是我的代码:

     String URL="jdbc:mysql://192.168.1.128:3306";
            Connection con=(Connection) DriverManager.getConnection(URL,user,pass);
            Statement stmt=(Statement) con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            Statement stmt1=(Statement) con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            String mySQL_new_table=("CREATE TABLE IF NOT EXISTS dbtest.T_AJPES_TR " + "(" + "row_count INT PRIMARY KEY AUTO_INCREMENT,"
                + "rn CHAR(15),sSpre CHAR(5),reg CHAR(5),eno VARCHAR(10),davcna VARCHAR(15),Ime VARCHAR(75),Priimek VARCHAR(75),LOG_ID INT,INDEX L_ID (LOG_ID),FOREIGN KEY(LOG_ID) references T_AJPES_TR_LOG(ID_LOG) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = INNODB;");
            String mySQL_log = ("CREATE TABLE IF NOT EXISTS dbtest.T_AJPES_TR_LOG" + "(ID_LOG INT PRIMARY KEY AUTO_INCREMENT, Date_import VARCHAR(45),File_import VARCHAR(75)) ENGINE = INNODB;");
            stmt.executeUpdate(mySQL_new_table);
            stmt1.executeUpdate(mySQL_log);
            ResultSet uprs=stmt.executeQuery("SELECT * FROM dbtest.T_AJPES_TR");
            ResultSet uprs1=stmt1.executeQuery("SELECT * FROM dbtest.T_AJPES_TR_LOG");

我用Google搜索了许多创建外键的教程,但仍然存在问题。那么我做错了什么?

1 个答案:

答案 0 :(得分:1)

您可能遇到外键约束问题,有关详细信息,请参阅http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

首先要检查的是你对T_AJPES_TR_LOG(ID_LOG)的LOG_ID引用是否正确