将数据库引擎从 MyIsam 更改为 innoDB 时出错
我已将 Java Web 应用程序的数据库引擎从 MyIsam 更改为 innoDB。我正在使用 MySql 数据库。由于某种原因,我现在在创建通过批处理添加的数据库表时遇到错误。
我得到的错误是:java.sql.BatchUpdateException
。
在这两个数据库引擎之间进行更改有何影响?我该如何纠正我的错误?
编辑
完整的堆栈跟踪:
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)
不与 innoDB 一起使用但与 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;
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 added 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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终解决了这个问题,而且几乎没有时间来完成这个项目。
问题是 InnoDB 与 MyIsam 不同,不能使用 unsigned int 作为外键。
在我的示例中,我必须更改该
行:
非常简单,我只是不知道它是这样工作的。
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
line: to
pretty simple I just didn't know it acted this way.
使用
InnoDB
引擎的数据库需要每个表有一个索引字段(可以是索引
或主键
甚至是>外键
)。因此,您的问题似乎是没有引擎可以使用的已定义索引(或键)。您应该为您创建的每个表声明一个
主键
。MyISAM
引擎不需要这个。A database with the
InnoDB
-engine requires an index-field per table (which can be aindex
or aPrimary Key
or even aForeign 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.