在MySQL中添加外键失败(错误150) [英] Adding Foreign Key Fails in MySQL (errno 150)

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

问题描述

我已经阅读了许多关于在尝试添加外键copnstraint时接收MySQL errno 150的文章,但是我还没有找到解决方案。我希望我没有做一些愚蠢的事情。我做了一个简单的测试用例。
$ b $ ol
这两个表都是InnoDB。

  • 这两个表都是UTF- 8

  • 两列都是int(11)unsigned(使color_id NOT NULL 编辑:我错了,这是解决方案



    这是我的两个表:

    $ 小部件

      CREATE TABLE`widgets`(
    `id` int(11)unsigned NOT NULL AUTO_INCREMENT,$ b $```varchar(100) NOT NULL DEFAULT'',
    `color_id` int(11)DEFAULT NULL,
    PRIMARY KEY(`id`)
    )ENGINE = InnoDB DEFAULT CHARSET = utf8;

    颜色

      CREATE TABLE`colors`(
    `id` int(11)unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar 10)NOT NULL DEFAULT'',
    PRIMARY KEY(`id`)
    )ENGINE = InnoDB DEFAULT CHARSET = utf8;

    我刚创建了这些表格,没有内容。当我尝试添加外键约束来将widgets.color_id链接到colors.id时,会发生这种情况:

      mysql> ALTER TABLE`widgets` ADD FOREIGN KEY(`color_id`)REFERENCES`color`(`id`); 

    错误1005(HY000):无法创建表'production。#sql-7b1_2dd7'(errno:150)

    我只是补充说,我还没有能够使用我的GUI选择工具 - Sequel Pro在OSX上。当试图创建一个外键关系时,我得到了同样的错误信息。
    $ b

    SHOW ENGINE INNODB STATUS 返回:
    $ b $ pre $ 130531 17:23:06表生产外键约束错误/#sql-7b1_2c80:
    FOREIGN KEY (`color_id`)REFERENCES`colors`(`id`):无法在
    中找到索引,其中被引用的列出现在第一列中,
    或表中的列类型以及引用表不符合约束。

    我在做些什么荒谬可笑的事情?

    解决方案

    color_id 不是无符号并且有一个 DEFAULT NULL 。列类型必须相同。


    I've read many other posts about receiving MySQL errno 150 when trying to add a foreign key copnstraint, however I haven't found a solution yet. I hope I'm not doing something stupid. I made a simple test case.

    1. Both tables are InnoDB.
    2. Both tables are UTF-8.
    3. Both columns are int(11) unsigned (making color_id NOT NULL doesn't make a difference). (EDIT: I WAS WRONG ABOUT THIS, AND THIS WAS THE SOLUTION)

      Here are my two tables:

    Table widgets:

    CREATE TABLE `widgets` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL DEFAULT '',
    `color_id` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    Table colors:

    CREATE TABLE `colors` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(10) NOT NULL DEFAULT '',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    I just created these tables, there is no content. When I try to add a foreign key constraint to link widgets.color_id to colors.id, this happens:

    mysql> ALTER TABLE `widgets` ADD FOREIGN KEY (`color_id`) REFERENCES `color` (`id`);
    
    ERROR 1005 (HY000): Can't create table 'production.#sql-7b1_2dd7' (errno: 150)
    

    I'll just add that I haven't been able to use my GUI tool of choice -- Sequel Pro on OSX -- either. I get the same error message when trying to create a foreign key relationship.

    SHOW ENGINE INNODB STATUS returns this:

    130531 17:23:06 Error in foreign key constraint of table production/#sql-7b1_2c80: 
    FOREIGN KEY (`color_id`) REFERENCES `colors` (`id`): 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.
    

    Am I doing something ridiculously stupid??

    解决方案

    color_id is not unsigned and has a DEFAULT NULL. The column types must be identical.

    这篇关于在MySQL中添加外键失败(错误150)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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