首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MySQL错误: 1005无法创建表'myTable‘(错误号: 150)

MySQL错误: 1005无法创建表'myTable‘(错误号: 150)
EN

Stack Overflow用户
提问于 2012-03-01 00:59:11
回答 4查看 4.5K关注 0票数 2

我已经阅读了很多关于这个错误的帖子,但没有一个解决方案能够解决这个问题(假设我已经正确地尝试过了)。

以下是导致错误的代码:

代码语言:javascript
复制
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机器上也运行得很好(我目前在Windows7下工作)。

下面是SHOW ENGINE INNODB STATUS的输出

代码语言:javascript
复制
------------------------
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的输出

代码语言:javascript
复制
| 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 |
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-03-01 01:43:08

由于season.id未签名,因此event.season_id也需要未签名:

代码语言:javascript
复制
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
);
票数 4
EN

Stack Overflow用户

发布于 2012-11-15 06:32:28

有关"Can't create table 'X‘(errno: 150)“的问题,请查看this page

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

显示引擎INNODB状态;

这会抛出各种各样的废话,但最重要的是,你应该看到一个标题为“最新的外键错误”的部分,在那里你会看到实际的问题,说这样的话:

最新的外键错误

121114 16:22:57表dgweb/company的外键约束错误:被引用表中没有将列作为第一列的索引,或者被引用表中的数据类型与表中的数据类型不匹配。Constraint:,CONSTRAINT "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/en/innodb-foreign-key-constraints.html

然后你就会知道哪里出了问题:-)。

票数 2
EN

Stack Overflow用户

发布于 2012-03-01 01:37:14

因为您还没有显示show create table season的输出,所以我不能确定您的问题是什么。

但是,MySQL docs有一个核对表:

  • 对应的列...必须具有类似的内部数据类型。。。整数类型的大小和符号必须是same
  • InnoDB 索引外键和引用的键...
  • ...在被引用的表中,必须有一个索引,其中被引用的列以相同的顺序作为第一列列出。

(强调我的)。

请确保您的表满足这些标准;如果仍然失败,请阅读文档页面上的其余标准。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9503334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档