MySql 错误 150 - 外键

发布于 2024-07-19 15:29:39 字数 361 浏览 11 评论 0原文

当我执行以下两个查询时(我已将它们精简为绝对必要的):

mysql> CREATE TABLE foo(id INT PRIMARY KEY);
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE bar ( id INT, ref INT, FOREIGN KEY (ref) REFERENCES foo(id)) ENGINE InnoDB;

我收到以下错误: 错误 1005 (HY000): 无法创建表 './test/bar.frm' (errno: 150)

我的错误到底在哪里? 我盯着这个看了半个小时也没找到他。

When I execute the follow two queries (I have stripped them down to absolutely necessary):

mysql> CREATE TABLE foo(id INT PRIMARY KEY);
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE bar ( id INT, ref INT, FOREIGN KEY (ref) REFERENCES foo(id)) ENGINE InnoDB;

I get the following error:
ERROR 1005 (HY000): Can't create table './test/bar.frm' (errno: 150)

Where the **** is my error? I haven't found him while staring at this for half an hour.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

明月夜 2024-07-26 15:29:39

来自 FOREIGN KEY 约束

如果您重新创建一个表
丢弃了,它必须有一个定义
符合外键
引用它的约束。 它必须
具有正确的列名称和类型,
并且它必须有索引
引用的键,如前所述。 如果
这些不满足,MySQL返回
错误号 1005 并指错误
错误消息中的 150。

我的怀疑是因为您没有将 foo 创建为 InnoDB,因为其他一切看起来都正常。

编辑:来自同一页面-

两个表都必须是 InnoDB 表,并且不能是 TEMPORARY 表。

From FOREIGN KEY Constraints

If you re-create a table that was
dropped, it must have a definition
that conforms to the foreign key
constraints referencing it. It must
have the right column names and types,
and it must have indexes on the
referenced keys, as stated earlier. If
these are not satisfied, MySQL returns
error number 1005 and refers to error
150 in the error message.

My suspicion is that it's because you didn't create foo as InnoDB, as everything else looks OK.

Edit: from the same page -

Both tables must be InnoDB tables and they must not be TEMPORARY tables.

羁客 2024-07-26 15:29:39

您可以使用命令 SHOW ENGINE INNODB STATUS 来获取有关错误的更多具体信息。

它将为您提供一个包含大量文本的 Status 列的结果。

查找名为“最新外键错误”的部分,例如,它可能如下所示:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
190215 11:51:26 Error in foreign key constraint of table `mydb1`.`contacts`:
Create  table `mydb1`.`contacts` with foreign key constraint failed. You have defined a SET NULL condition but column 'domain_id' is defined as NOT NULL in ' FOREIGN KEY (domain_id) REFERENCES domains (id) ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT contacts_teams_id_fk FOREIGN KEY (team_id) REFERENCES teams (id) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT' near ' ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT contacts_teams_id_fk FOREIGN KEY (team_id) REFERENCES teams (id) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT'.

You can use the command SHOW ENGINE INNODB STATUS to get more specific information about the error.

It will give you a result with a Status column containing a lot of text.

Look for the section called LATEST FOREIGN KEY ERROR which could for example look like this:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
190215 11:51:26 Error in foreign key constraint of table `mydb1`.`contacts`:
Create  table `mydb1`.`contacts` with foreign key constraint failed. You have defined a SET NULL condition but column 'domain_id' is defined as NOT NULL in ' FOREIGN KEY (domain_id) REFERENCES domains (id) ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT contacts_teams_id_fk FOREIGN KEY (team_id) REFERENCES teams (id) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT' near ' ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT contacts_teams_id_fk FOREIGN KEY (team_id) REFERENCES teams (id) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT'.
波浪屿的海角声 2024-07-26 15:29:39

要创建外键,

  1. 主列和引用列必须具有相同的定义。
  2. 两个表引擎都必须是 InnoDB。

您可以使用此命令更改表的引擎,请在执行此命令之前进行备份。

alter table [表名] ENGINE=InnoDB;

To create a foreign key ,

  1. both the main column and the reference column must have same definition.
  2. both tables engine must be InnoDB.

You can alter the engine of table using this command , please take the backup before executing this command.

alter table [table name] ENGINE=InnoDB;

柏拉图鍀咏恒 2024-07-26 15:29:39

我遇到了同样的问题,对于那些也遇到此问题的人:

检查引用表的表名

我忘记了表名末尾的“s”,

例如表 Client --> 客户

:)

I had the same problem, for those who are having this also:

check the table name of the referenced table

I had forgotten the 's' at the end of my table name

eg table Client --> Clients

:)

彼岸花ソ最美的依靠 2024-07-26 15:29:39

除了许多其他导致 MySql 错误 150(使用 InnoDB 时)的原因之外,可能的原因之一是包含作为外部引用的列名的表的创建语句中未定义的 KEY相关表中的键。

假设主表的创建语句是 -

CREATE TABLE 'master_table' (
'id' int(10) NOT NULL AUTO_INCRMENT,
'record_id' char(10) NOT NULL,
'名称' varchar(50) NOT NULL DEFAULT '',
'地址' varchar(200) NOT NULL DEFAULT '',
主键('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

以及relative_table 表的创建语法,其中外键约束是从主表设置的 -

CREATE TABLE 'relative_table' (
'id' int(10) NOT NULL AUTO_INCRMENT,
'工资' int(10) NOT NULL DEFAULT '',
 '等级' char(2) NOT NULL DEFAULT '',
'record_id' char(10) 默认为 NULL,
主键('id'),
约束“fk_slave_master”外键(“record_id”)引用“master”(“record_id”)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

如果使用 InnoDB,这个脚本肯定会以 MySql 错误 150 结束。

为了解决这个问题,我们需要为master_table表中的record_id列添加一个KEY,然后在relative_table<中引用/code> 用作外键的表。

最后,master_table 的创建语句将是 -

CREATE TABLE 'master_table' (
'id' int(10) NOT NULL AUTO_INCRMENT,
'record_id' char(10) NOT NULL,
'名称' varchar(50) NOT NULL DEFAULT '',
'地址' varchar(200) NOT NULL DEFAULT '',
主键('id'),
KEY 'record_id' ('record_id')
) ENGINE=InnoDB 默认字符集=utf8;

Apart form many other reasons to end up with MySql Error 150 (while using InnoDB), One of the probable reason, is the undefined KEY in the create statement of the table containing the column name referenced as a foreign key in the relative table.

Let's say the create statement of master table is -

CREATE TABLE 'master_table' (
 'id' int(10) NOT NULL AUTO_INCREMENT,
 'record_id' char(10) NOT NULL,
 'name' varchar(50) NOT NULL DEFAULT '',
 'address' varchar(200) NOT NULL DEFAULT '',
 PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

and the create syntax for the relative_table table where the foreign key constraint is set from primary table -

CREATE TABLE 'relative_table' (
 'id' int(10) NOT NULL AUTO_INCREMENT,
 'salary' int(10) NOT NULL DEFAULT '',
 'grade' char(2) NOT NULL DEFAULT '',
 'record_id' char(10) DEFAULT NULL,
 PRIMARY KEY ('id'),
 CONSTRAINT 'fk_slave_master' FOREIGN KEY ('record_id') REFERENCES 'master' ('record_id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

This script is definitely going to end with MySql Error 150 if using InnoDB.

To solve this, we need to add a KEY for the The column record_id in the master_table table and then reference in the relative_table table to be used as a foreign_key.

Finally, the create statement for the master_table, will be -

CREATE TABLE 'master_table' (
 'id' int(10) NOT NULL AUTO_INCREMENT,
 'record_id' char(10) NOT NULL,
 'name' varchar(50) NOT NULL DEFAULT '',
 'address' varchar(200) NOT NULL DEFAULT '',
 PRIMARY KEY ('id'),
 KEY 'record_id' ('record_id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

小红帽 2024-07-26 15:29:39

我遇到了同样的问题,原因是列的“排序规则”不同。 一个是latin1,另一个是utf8

I had very same problem and the reason was the "collation" of columns was different. One was latin1 while the other was utf8

你的心境我的脸 2024-07-26 15:29:39

如果您在“references”关键字后没有给出正确的列名称,也可能会发生这种情况。

This may also happen if you have not given correct column name after "references" keyword.

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