MySQL CREATE TABLE语句上的外键错误(错误:150) [英] Foreign Key Error on MySQL CREATE TABLE statement (err: 150)

查看:67
本文介绍了MySQL CREATE TABLE语句上的外键错误(错误:150)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我已经在一对非常简单的create table语句上尝试了所有可能的方法.

I feel I've tried everything possible on a very simple pair of create table statements.

类型匹配,我尝试使用ENGINE = InnoDB,依此类推,却很困惑为什么出现外键错误.

The types match, I tried using ENGINE = InnoDB, etc and am stumped why I'm getting the foreign key error.

我离开SQL已经有一段时间了,所以这可能很简单.

I've been away from SQL for some time, so this is probably an easy one.

mysql> CREATE TABLE foo_ent(yyy_no VARCHAR(80),
    -> zoo VARCHAR(80),
    -> PRIMARY KEY (yyy_no));
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE cat_ent(some_item INTEGER,
    -> yyy_no VARCHAR(80),
    -> apple DECIMAL(6,2),
    -> PRIMARY KEY (some_item),
    -> FOREIGN KEY (yyy_no) REFERENCES foo_ent);
ERROR 1005 (HY000): Can't create table 'test.cat_ent' (errno: 15
0)

很抱歉,变量名不正确,可以覆盖公司的内容.

Sorry about the poor variable names, safe to over-write company stuff.

推荐答案

您不引用字段,仅引用表,这是不正确的.

You don't reference to a field, only a table, which is incorrect.

...
foreign key (yyy_no) references foo_ent(yyy_no)

然后根据您的错误号,MySQL文档也指出;

And according to your error number, the MySQL documentation also states;

如果您重新创建一个表, 删除,它必须有一个定义 符合外键 引用它的约束.它必须 具有正确的列名称和类型, 并且必须在 引用的键,如前所述.如果 这些都不满意,MySQL返回 错误号1005,表示错误 错误消息中显示150.

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.

http://dev.mysql. com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

这篇关于MySQL CREATE TABLE语句上的外键错误(错误:150)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆