“id_”是什么?和“id”和“id”导致错误 1005: 无法创建表“xxx.xxx” (错误号:121)
我是 MySQL 新手,正在创建一个复杂的 EER 图。创建后,我对模型进行“正向工程...”,并遇到了可怕的错误:错误 1005:无法创建表“xxx.xxx”(错误号:121)。我创建了另一个具有 1:M 关系的两个表的简化模型,但使用“id_”作为主键和外键名称。那行得通。
i.e.
TABLE_A
id_TABLE_A INT
TABLE_B
id_TABLE_B INT
id_TABLE_A INT
其中,TABLE_A [1]--<[M] TABLE_B 之间存在一对多关系,并且 TABLE_B.id_TABLE_A 是外键
查看我复杂的 EER 图,我注意到我使用了没有下划线的“id”主键和外键名称。我在“id”后面插入了下划线并对模型进行了正向工程,并且没有任何错误。因此,这里有两个简单的示例模型,一个带有“id_”,另一个带有“id”。 “id_”不会导致错误 1005,而“id”会导致错误 1005。 有人知道为什么 MySQL 会发生这种异常吗?
========== =================================================== =================== 好模型:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
DROP TABLE IF EXISTS `mydb`.`TABLE_A` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_A` (
`id_TABLE_A` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`id_TABLE_A`) ,
UNIQUE INDEX `id_TABLE_A_UNIQUE` (`id_TABLE_A` ASC) )
ENGINE = InnoDB;
DROP TABLE IF EXISTS `mydb`.`TABLE_B` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_B` (
`id_TABLE_B` INT NOT NULL AUTO_INCREMENT ,
`id_TABLE_A` INT NOT NULL ,
PRIMARY KEY (`id_TABLE_B`) ,
UNIQUE INDEX `id_TABLE_B_UNIQUE` (`id_TABLE_B` ASC) ,
INDEX `id_TABLE_A` (`id_TABLE_A` ASC) ,
CONSTRAINT `id_TABLE_A`
FOREIGN KEY (`id_TABLE_A` )
REFERENCES `mydb`.`TABLE_A` (`id_TABLE_A` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
结果:
Build is successful.
================================= =================================================
<强>坏模型:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
DROP TABLE IF EXISTS `mydb`.`TABLE_A` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_A` (
`idTABLE_A` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`idTABLE_A`) ,
UNIQUE INDEX `idTABLE_A_UNIQUE` (`idTABLE_A` ASC) )
ENGINE = InnoDB;
DROP TABLE IF EXISTS `mydb`.`TABLE_B` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_B` (
`idTABLE_B` INT NOT NULL AUTO_INCREMENT ,
`idTABLE_A` INT NOT NULL ,
PRIMARY KEY (`idTABLE_B`) ,
UNIQUE INDEX `idTABLE_B_UNIQUE` (`idTABLE_B` ASC) ,
INDEX `idTABLE_A` (`idTABLE_A` ASC) ,
CONSTRAINT `idTABLE_A`
FOREIGN KEY (`idTABLE_A` )
REFERENCES `mydb`.`TABLE_A` (`idTABLE_A` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
结果:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'mydb.table_b' (errno: 121)
CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_B` (
`idTABLE_B` INT NOT NULL AUTO_INCREMENT ,
`idTABLE_A` INT NOT NULL ,
PRIMARY KEY (`idTABLE_B`) ,
UNIQUE INDEX `idTABLE_B_UNIQUE` (`idTABLE_B` ASC) ,
INDEX `idTABLE_A` (`idTABLE_A` ASC) ,
CONSTRAINT `idTABLE_A`
FOREIGN KEY (`idTABLE_A` )
REFERENCES `mydb`.`TABLE_A` (`idTABLE_A` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 8 succeeded, 1 failed
I am new to MySQL and was creating a complex EER diagram. After creation, I "Forward Engineer..." the model and hit the dreaded ERROR: Error 1005: Can't create table 'xxx.xxx' (errno: 121). I created another simplified model of two tables with a 1:M relationship, but used "id_" for the primary and foreign key names. That worked.
i.e.
TABLE_A
id_TABLE_A INT
TABLE_B
id_TABLE_B INT
id_TABLE_A INT
where, there is a one to many relationship between TABLE_A [1]--<[M] TABLE_B, and TABLE_B.id_TABLE_A is the foreign key
Looking at my complex EER diagram, I noticed I used "id" with no underscore for the primary and foreign key names. I inserted the underscore after "id" and Forward Engineer'ed the model and it worked with no errors. So, here are two simple example models, one with the "id_" and the other with "id". The "id_" DOES NOT cause the Error 1005 and the "id" causes the Error 1005. Anyone with an idea as to why this anomaly happens with MySQL?
===============================================================================
Good Model:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
DROP TABLE IF EXISTS `mydb`.`TABLE_A` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_A` (
`id_TABLE_A` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`id_TABLE_A`) ,
UNIQUE INDEX `id_TABLE_A_UNIQUE` (`id_TABLE_A` ASC) )
ENGINE = InnoDB;
DROP TABLE IF EXISTS `mydb`.`TABLE_B` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_B` (
`id_TABLE_B` INT NOT NULL AUTO_INCREMENT ,
`id_TABLE_A` INT NOT NULL ,
PRIMARY KEY (`id_TABLE_B`) ,
UNIQUE INDEX `id_TABLE_B_UNIQUE` (`id_TABLE_B` ASC) ,
INDEX `id_TABLE_A` (`id_TABLE_A` ASC) ,
CONSTRAINT `id_TABLE_A`
FOREIGN KEY (`id_TABLE_A` )
REFERENCES `mydb`.`TABLE_A` (`id_TABLE_A` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
RESULT:
Build is successful.
===============================================================================
Bad Model:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
DROP TABLE IF EXISTS `mydb`.`TABLE_A` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_A` (
`idTABLE_A` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`idTABLE_A`) ,
UNIQUE INDEX `idTABLE_A_UNIQUE` (`idTABLE_A` ASC) )
ENGINE = InnoDB;
DROP TABLE IF EXISTS `mydb`.`TABLE_B` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_B` (
`idTABLE_B` INT NOT NULL AUTO_INCREMENT ,
`idTABLE_A` INT NOT NULL ,
PRIMARY KEY (`idTABLE_B`) ,
UNIQUE INDEX `idTABLE_B_UNIQUE` (`idTABLE_B` ASC) ,
INDEX `idTABLE_A` (`idTABLE_A` ASC) ,
CONSTRAINT `idTABLE_A`
FOREIGN KEY (`idTABLE_A` )
REFERENCES `mydb`.`TABLE_A` (`idTABLE_A` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
RESULT:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'mydb.table_b' (errno: 121)
CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_B` (
`idTABLE_B` INT NOT NULL AUTO_INCREMENT ,
`idTABLE_A` INT NOT NULL ,
PRIMARY KEY (`idTABLE_B`) ,
UNIQUE INDEX `idTABLE_B_UNIQUE` (`idTABLE_B` ASC) ,
INDEX `idTABLE_A` (`idTABLE_A` ASC) ,
CONSTRAINT `idTABLE_A`
FOREIGN KEY (`idTABLE_A` )
REFERENCES `mydb`.`TABLE_A` (`idTABLE_A` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 8 succeeded, 1 failed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的约束名称与另一个名称冲突,通常会发生这种情况。请参阅 http://dev.mysql.com/ 的评论doc/refman/5.0/en/innodb-error-codes.html 和 http://forums.mysql.com/read.php?22,33999,76181#msg-76181
但是这里...... MySQL错误?
This would normally happen if your constraint name is conflicting with another name. See comments at http://dev.mysql.com/doc/refman/5.0/en/innodb-error-codes.html and http://forums.mysql.com/read.php?22,33999,76181#msg-76181
But here... A MySQL bug?