开发者

Error when changing DB engine from MyIsam to innoDB

开发者 https://www.devze.com 2023-03-24 14:28 出处:网络
I\'ve changed my DB engine of my Java webapplication to innoDB from MyIsam. I\'m using a MySql DB. For some reason I now get an error on the creation of my db tables which are adde开发者_运维知识库d t

I've changed my DB engine of my Java webapplication to innoDB from MyIsam. I'm using a MySql DB. For some reason I now get an error on the creation of my db tables which are adde开发者_运维知识库d through a batch.

The error i get is: java.sql.BatchUpdateException.

What are the implications of changing between these two DB engines and how do I correct what I've wronged?

EDIT

The complete stack trace:

java.sql.SQLException: Can't create table 'schedule.course2user' (errno: 150)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2690)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1648)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1567)
    at database.DBCreator.execList(DBCreator.java:203)
    at database.DBCreator.createDatabase(DBCreator.java:215)
    at database.DBCreator.main(DBCreator.java:248)

The DB commands that are not working with innoDB but worked with MyIsam:

CREATE  TABLE IF NOT EXISTS `schedule`.`course` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL UNIQUE,
`description` BLOB ,
`credits` TINYINT(1) DEFAULT '0' ,
`capacity` INT DEFAULT '0' ,
`isRemoved` TINYINT(1)  NULL DEFAULT '0' ,
`groupId` INT  NULL DEFAULT '0' ,
`creatorId` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_creator` (`creatorId` ASC) ,
CONSTRAINT `fk_creator` FOREIGN KEY (`creatorId` )REFERENCES `schedule`.`course` (`id` ) ON DELETE CASCADE ON UPDATE NO ACTION ) 
ENGINE = innoDB DEFAULT CHARACTER SET = utf8;


Solved it finally, And with little time to go with this project.

The problem is that InnoDB unlike MyIsam cannot use an unsigned int as a foreign key.

In my example I had to change the

`creatorId` INT UNSIGNED NOT NULL,

line: to

`creatorId` INT NOT NULL,

pretty simple I just didn't know it acted this way.


A database with the InnoDB-engine requires an index-field per table (which can be a index or a Primary Key or even a Foreign Key).

So, your problem seams to be that there is no defined index (or key) which the engine can use. You should declare a Primary Key for every table you're creating.

The MyISAM-engine doesn't require this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号