Question

I'm having problems with creating mySQL table within Java program. I constantly get Can't create table... errno: 150

Here is my code:

     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");

I googled many tutorials for creating foreign key and I still have problems. So what I'm doing wrong?

Was it helpful?

Solution

You've likely got an issue with foreign key constraints, see http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html for more details.

First one to check is whether your LOG_ID reference to T_AJPES_TR_LOG(ID_LOG) is correct

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top