MySQL使用提供errno:150的外键创建表 [英] MySQL Creating tables with Foreign Keys giving errno: 150

查看:230
本文介绍了MySQL使用提供errno:150的外键创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用两个外键在MySQL中创建一个表,它引用另外两个表中的主键,但是我得到一个errno:150错误,它不会创建表。
$ b

下面是所有3个表的SQL:

$ p codeREATE TABLE role_groups
``role_group_id` int(11)NOT NULL`AUTO_INCREMENT`,
`name` varchar(20),
`description` varchar(200),
PRIMARY KEY(`role_group_id`)
)ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS`roles`(
`role_id` int(11)NOT NULL AUTO_INCREMENT,
`name` varchar(50),
`description `varchar(200),
PRIMARY KEY(`role_id`)
)ENGINE = InnoDB;

create table role_map(
`role_map_id` int not null`auto_increment`,
`role_id` int not null,
`role_group_id` int not null,
主键(`role_map_id`),
外键(`role_id`)引用角色(`role_id`),
外键(`role_group_id`)引用role_groups(`role_group_id`)
)引擎= InnoDB;

任何帮助将不胜感激。

>解决方案

我有同样的问题与 ALTER TABLE ADD FOREIGN KEY



一小时后,我发现这些条件必须得到满足,才能得到错误150:


  1. 这两个表必须有相同的引擎,并且引擎必须支持外键。例如, ENGINE = InnoDB 。其他存储引擎默默地忽略外键定义,因此它们不会返回错误或警告,但不会保存FK约束。
  2. 父表中的引用列必须是最左边的列的关键。最好如果密钥是 PRIMARY KEY UNIQUE KEY
  3. FK定义必须以与PK定义相同的顺序引用PK列。例如,如果FK REFERENCES Parent(a,b,c)那么不能按照(a,c ,b)

  4. 父表中的PK列必须与FK列中的FK列子表。例如,如果Parent表中的PK列是 UNSIGNED ,那么一定要定义 UNSIGNED 子表字段。

    例外:字符串的长度可能不同。例如, VARCHAR(10)可以引用 VARCHAR(20),反之亦然。 >

  5. 任何字符串类型的FK列必须与相应的PK列具有相同的字符集和归类。
  6. 数据已经在Child表中,FK列中的每个值都必须与Parent表PK列中的值相匹配。使用如下查询来检查:

      SELECT COUNT(*)FROM Child LEFT OUTER JOIN Parent ON Child.FK = Parent.PK 
    WHERE Parent.PK IS NULL;

    这必须返回零(0)不匹配的值。显然,这个查询是一个通用的例子。您必须替换您的表名称和列名称。


  7. 父表和子表都不能是 TEMPORARY
  8. 父表和子表都不能是 PARTITIONED 表。

  9. ol>

    希望这有助于。


    I am trying to create a table in MySQL with two foreign keys, which reference the primary keys in 2 other tables, but I am getting an errno: 150 error and it will not create the table.

    Here is the SQL for all 3 tables:

    CREATE TABLE role_groups (
      `role_group_id` int(11) NOT NULL `AUTO_INCREMENT`,
      `name` varchar(20),
      `description` varchar(200),
      PRIMARY KEY (`role_group_id`)
    ) ENGINE=InnoDB;
    
    CREATE TABLE IF NOT EXISTS `roles` (
      `role_id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(50),
      `description` varchar(200),
      PRIMARY KEY (`role_id`)
    ) ENGINE=InnoDB;
    
    create table role_map (
      `role_map_id` int not null `auto_increment`,
      `role_id` int not null,
      `role_group_id` int not null,
      primary key(`role_map_id`),
      foreign key(`role_id`) references roles(`role_id`),
      foreign key(`role_group_id`) references role_groups(`role_group_id`)
    ) engine=InnoDB;
    

    Any help would be greatly appreciated.

    解决方案

    I had the same problem with ALTER TABLE ADD FOREIGN KEY.

    After an hour, I found that these conditions must be satisfied to not get error 150:

    1. The two tables must have the same engine, and the engine must support foreign keys. For example, ENGINE=InnoDB. Other storage engines silently ignore foreign key definitions, so they return no error or warning, but the FK constraint is not saved.
    2. The referenced columns in the Parent table must be the left-most columns of a key. Best if the key is PRIMARY KEY or UNIQUE KEY.
    3. The FK definition must reference the PK column(s) in the same order as the PK definition. For example, if the FK REFERENCES Parent(a,b,c) then the Parent's PK must not be defined on columns in order (a,c,b).
    4. The PK column(s) in the Parent table must be the same data type as the FK column(s) in the Child table. For example, if a PK column in the Parent table is UNSIGNED, be sure to define UNSIGNED for the corresponding column in the Child table field.

      Exception: length of strings may be different. For example, VARCHAR(10) can reference VARCHAR(20) or vice versa.

    5. Any string-type FK column(s) must have the same character set and collation as the corresponding PK column(s).
    6. If there is data already in the Child table, every value in the FK column(s) must match a value in the Parent table PK column(s). Check this with a query like:

      SELECT COUNT(*) FROM Child LEFT OUTER JOIN Parent ON Child.FK = Parent.PK 
      WHERE Parent.PK IS NULL;
      

      This must return zero (0) unmatched values. Obviously, this query is an generic example; you must substitute your table names and column names.

    7. Neither the Parent table nor the Child table can be a TEMPORARY table.
    8. Neither the Parent table nor the Child table can be a PARTITIONED table.

    Hope this helps.

    这篇关于MySQL使用提供errno:150的外键创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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