繁体   English   中英

MySQL错误:1005无法创建表“ myTable”(错误号:150)

[英]MySQL ERROR: 1005 Can't create table 'myTable' (errno : 150)

我已经阅读了许多有关此错误的文章,但是没有一个解决方案能够解决该问题(假设我已经正确尝试了它们)。

这是导致错误的代码:

CREATE TABLE season
(
  id          smallint unsigned NOT NULL auto_increment,
  title       varchar(25) NOT NULL,

  PRIMARY KEY (id)
);

CREATE INDEX seasonId ON season(id);

DROP TABLE IF EXISTS event;
CREATE TABLE event
(
  id           smallint unsigned NOT NULL auto_increment,
  title        varchar(255) NOT NULL,
  season_id    smallint NOT NULL,

  PRIMARY KEY (id),
  FOREIGN KEY (season_id) REFERENCES season(id)
  ON UPDATE RESTRICT ON DELETE RESTRICT
);

因此,根据错误,我的外键声明有问题。 但是我已经在机器上运行了这段代码,没有任何问题,并且它也可以在我的Linux机器上完美运行(我目前在Windows 7下工作)。

这是SHOW ENGINE INNODB STATUS的输出:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
120229 17:43:28 Error in foreign key constraint of table fcrcontent/event:
FOREIGN KEY (season_id) REFERENCES season(id)
  ON UPDATE RESTRICT ON DELETE RESTRICT
):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

我还尝试在一个新的数据库上运行脚本,但没有成功。

这是show create table season的输出:

| season | CREATE TABLE `season` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(25) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `seasonId` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

由于season.id是未签名的,所以event.season_id也需要是未签名的:

CREATE TABLE event
(
  id           smallint unsigned NOT NULL auto_increment,
  title        varchar(255) NOT NULL,
  season_id    smallint unsigned NOT NULL,

  PRIMARY KEY (id),
  FOREIGN KEY (season_id) REFERENCES season(id)
  ON UPDATE RESTRICT ON DELETE RESTRICT
);

对于“无法创建表'X'(errno:150)”的问题,请查看此页面

他指出的最重要的事情是,在发生这种情况后立即从命令行登录到您的mysql服务器并键入:

显示引擎的INNODB状态;

这会吐出各种各样的废话,但是最重要的是,您应该看到标题为“最新外来键错误”的部分,在该部分中您将看到实际的问题,并说出这样的话:


最新外键错误

121114 16:22:57表dgweb / company的外键约束错误:被引用表中没有索引,该索引将包含列作为第一列,或者被引用表中的数据类型与表中的数据类型不匹配。 约束:,约束“fk_company_wf_reporting_info”外键(“wf_reporting_info”)参考>“wf_reporting_info”(“wf_reporting_info_id”)在表的外键索引是“fk_company_wf_reporting_info”见http://dev.mysql.com/doc/refman /5.5/zh-CN/innodb-foreign-key-constraints.html,以获取正确的外键定义。

然后,您将知道到底是什么错了:-)。

由于您尚未显示show create table season的输出,因此我不确定您的问题是什么。

但是, MySQL文档有一个清单:

  • 相应的列必须具有相似的内部数据类型。 [..] 整数类型的大小和符号必须相同

  • InnoDB需要外键和引用键上的索引。

  • [...]在引用表中,必须有一个索引,其中引用列以相同的顺序列为第一列。

(强调我的)。

请确保您的桌子符合这些条件; 如果仍然失败,请阅读docs页面上的其余标准。

这个 :

`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,

需要与此完全相同的类型:

season_id    smallint NOT NULL,

所以改成它

smallint(5)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM