mysql - 无法弄清楚外键约束语句有什么问题

标签 mysql database-design foreign-keys mysql-error-1005

这是 show engine innodb status; 当我尝试创建表时的错误消息:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
110628 16:56:07 Error in foreign key constraint of table test/menu_items:
foreign key(id_menu)
                references menus(id)
                on update cascade
                on delete cascade,
        foreign key(id_item)
                references items(id)
                on update cascade
                on delete cascade,
        primary key(id_menu, id_item)
) engine=InnoDB:
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.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

这些是相关的 SQL 语句:

create table if not exists menus (                                                                                            
    id              mediumint unsigned not null auto_increment,
    id_restaurant   mediumint unsigned not null,
    name            varchar(50) not null,
    description     varchar(255) default null,
    foreign key(id_restaurant)
        references restaurants(id)
        on update cascade
        on delete cascade,
    primary key(id)
) engine=InnoDB;

create table if not exists items (
    id              mediumint unsigned not null auto_increment,
    id_restaurant   mediumint unsigned not null,
    name            varchar(50),
    description     text,
    type            enum('appetizer','salad','soup','entree','dessert','drink','other'),
    price           decimal(4,2),
    foreign key(id_restaurant)
        references restaurants(id)
        on update cascade
        on delete cascade,
    primary key(id)
) engine=InnoDB;

create table if not exists order_items (
    id_order        bigint unsigned not null,
    id_item         mediumint unsigned not null,
    item_name       varchar(50),
    item_description    text,
    item_price      decimal(4,2),
    notes           varchar(1024),
    quantity        smallint unsigned,
    foreign key(id_order)
        references orders(id)
        on update cascade
        on delete cascade,
    foreign key(id_item)
        references items(id)
        on update cascade
        on delete cascade,
    primary key(id_order, id_item)
) engine=InnoDB;

删除 menu_items.id_menu 和相应的外键/主键可以正确解析 SQL 语句。

为什么我不能从 menu_items 中引用 menus(id) 的外键?

最佳答案

你的数据类型不匹配:

create table order_items (
    id_order bigint unsigned not null, -- BIGINT

create table items (
    id mediumint unsigned not null auto_increment, -- MEDIUMINT

但是 order_items.id_order 引用了 items.id,所以它们应该相同。

尝试将 order_items id_order 更改为中等 int:

order_items (
    id_order        mediumint unsigned not null,

关于mysql - 无法弄清楚外键约束语句有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6503484/

相关文章:

php - 如何将wordpress mysql md5用户密码字段传输到另一个mysql表中

mysql - 如何选择零结果mysql?

mysql - 我应该将大量数据放入另一个表以节省性能吗

mysql - 外键约束格式不正确[同表]

mysql - 如何复制最后插入的行并将其粘贴到 SQL 中的其他表中?

Mysql - 从另一个表数据更新列而不减少行

mysql - 权限系统最有效/最简单的方法?

sql-server - 如何在Azure中备份和维护SQL Server表?

Django 管理更改列表过滤/链接到其他模型

mysql - 如何在同一查询中使用外键计算表中的行数?