MySql 错误 150 - 外键
当我执行以下两个查询时(我已将它们精简为绝对必要的):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自
FOREIGN KEY
约束我的怀疑是因为您没有将
foo
创建为 InnoDB,因为其他一切看起来都正常。编辑:来自同一页面-
From
FOREIGN KEY
ConstraintsMy suspicion is that it's because you didn't create
foo
as InnoDB, as everything else looks OK.Edit: from the same page -
您可以使用命令 SHOW ENGINE INNODB STATUS 来获取有关错误的更多具体信息。
它将为您提供一个包含大量文本的
Status
列的结果。查找名为“最新外键错误”的部分,例如,它可能如下所示:
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:
要创建外键,
您可以使用此命令更改表的引擎,请在执行此命令之前进行备份。
alter table [表名] ENGINE=InnoDB;
To create a foreign key ,
You can alter the engine of table using this command , please take the backup before executing this command.
alter table [table name] ENGINE=InnoDB;
我遇到了同样的问题,对于那些也遇到此问题的人:
检查引用表的表名
我忘记了表名末尾的“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
:)
除了许多其他导致 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 columnrecord_id
in themaster_table
table and then reference in therelative_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;
我遇到了同样的问题,原因是列的“排序规则”不同。 一个是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
如果您在“references”关键字后没有给出正确的列名称,也可能会发生这种情况。
This may also happen if you have not given correct column name after "references" keyword.