mysql外键约束3种语句,mysql – 无法弄清楚外键约束语句有什么问题

这是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中对menu(id)进行外键引用?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值