将数据库引擎从 MyIsam 更改为 innoDB 时出错

发布于 2024-11-27 11:43:37 字数 1687 浏览 0 评论 0原文

我已将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2024-12-04 11:43:37

最终解决了这个问题,而且几乎没有时间来完成这个项目。

问题是 InnoDB 与 MyIsam 不同,不能使用 unsigned int 作为外键。

在我的示例中,我必须更改该

`creatorId` INT UNSIGNED NOT NULL,

行:

`creatorId` INT NOT NULL,

非常简单,我只是不知道它是这样工作的。

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.

记忆之渊 2024-12-04 11:43:37

使用 InnoDB 引擎的数据库需要每个表有一个索引字段(可以是 索引主键 甚至是 >外键)。

因此,您的问题似乎是没有引擎可以使用的已定义索引(或键)。您应该为您创建的每个表声明一个主键

MyISAM 引擎不需要这个。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文