简体   繁体   English

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

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

I've read a number of posts about this error, but none of the solutions have managed to solve the problem (assuming I've tried them correctly). 我已经阅读了许多有关此错误的文章,但是没有一个解决方案能够解决该问题(假设我已经正确尝试了它们)。

This is the code that causes the error: 这是导致错误的代码:

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
);

So according to the error there is a problem with my foreign key declaration. 因此,根据错误,我的外键声明有问题。 However I had already run this code on the machine with no problems, and it ran perfectly on my Linux Machine as well (I'm currently working under Windows 7). 但是我已经在机器上运行了这段代码,没有任何问题,并且它也可以在我的Linux机器上完美运行(我目前在Windows 7下工作)。

Here is the output of SHOW ENGINE INNODB STATUS : 这是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.

I also tried running my script on a fresh database, but no go. 我还尝试在一个新的数据库上运行脚本,但没有成功。

Here is the output from show create table season : 这是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 |

Since season.id is unsigned, event.season_id also needs to be unsigned: 由于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
);

For problems with "Can't create table 'X' (errno: 150)", check out this page . 对于“无法创建表'X'(errno:150)”的问题,请查看此页面

The most important thing he points out, is to login to your mysql server from the command line immediately after it happens and type: 他指出的最重要的事情是,在发生这种情况后立即从命令行登录到您的mysql服务器并键入:

SHOW ENGINE INNODB STATUS; 显示引擎的INNODB状态;

That will spew out all kinds of crap, but most importantly you should see a section titled "LATEST FOREIGN KEY ERROR", where you'll see the actual problem, saying something like this: 这会吐出各种各样的废话,但是最重要的是,您应该看到标题为“最新外来键错误”的部分,在该部分中您将看到实际的问题,并说出这样的话:


LATEST FOREIGN KEY ERROR 最新外键错误

121114 16:22:57 Error in foreign key constraint of table dgweb/company: there is no index in referenced table which would contain the columns as the first columns, or the data types in the referenced table do not match the ones in table. 121114 16:22:57表dgweb / company的外键约束错误:被引用表中没有索引,该索引将包含列作为第一列,或者被引用表中的数据类型与表中的数据类型不匹配。 Constraint: , CONSTRAINT "fk_company_wf_reporting_info" FOREIGN KEY ("wf_reporting_info") REFERENCES > "wf_reporting_info" ("wf_reporting_info_id") The index in the foreign key in table is "fk_company_wf_reporting_info" See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html for correct foreign key definition. 约束:,约束“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,以获取正确的外键定义。

Then you'll know what the heck is wrong :-). 然后,您将知道到底是什么错了:-)。

Since you haven't shown the output from show create table season , I can't be sure what your problem is. 由于您尚未显示show create table season的输出,因此我不确定您的问题是什么。

However, the MySQL docs have a checklist: 但是, MySQL文档有一个清单:

  • Corresponding columns [...] must have similar internal data types. 相应的列必须具有相似的内部数据类型。 [..] The size and sign of integer types must be the same [..] 整数类型的大小和符号必须相同

  • InnoDB requires indexes on foreign keys and referenced keys [...]. InnoDB需要外键和引用键上的索引。

  • [...] in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order. [...]在引用表中,必须有一个索引,其中引用列以相同的顺序列为第一列。

(emphasis mine). (强调我的)。

Please make sure your tables meet these criteria; 请确保您的桌子符合这些条件; if it still fails, then read the rest of the criteria on the docs page. 如果仍然失败,请阅读docs页面上的其余标准。

This : 这个 :

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

Needs to be exact same type as this: 需要与此完全相同的类型:

season_id    smallint NOT NULL,

so change it to 所以改成它

smallint(5)

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

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