Mysql Can';我想不出是什么';外键约束语句有什么问题

Mysql Can';我想不出是什么';外键约束语句有什么问题,mysql,database-design,foreign-keys,mysql-error-1005,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)

这是来自
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语句


为什么我不能从菜单项对菜单(id)进行外键引用?

您的数据类型不匹配:

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

create table items (
    id mediumint unsigned not null auto_increment, -- MEDIUMINT
但是
order\u items.id\u order
引用了
items.id
,因此它们应该是相同的

尝试将订单\项目id \订单更改为中整数:

order_items (
    id_order        mediumint unsigned not null,

谢谢,我刚才也听到了。一定很晚了,因为我在发帖前已经盯着这个问题看了整整30分钟了。