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

发布于 2024-11-17 06:30:49 字数 2743 浏览 2 评论 0原文

这是当我尝试创建表时 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) 进行外键引用?

This is the error message from show engine innodb status; when I try to create the table:

------------------------
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.

These are the relevant SQL statements:

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;

Removing menu_items.id_menu and corresponding foreign key / primary key allows the SQL statements to be parsed properly.

Why can't I make a foreign key reference to menus(id) from menu_items?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单爱 2024-11-24 06:30:49

您的数据类型不匹配:

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 更改为medium int:

order_items (
    id_order        mediumint unsigned not null,

You've got a mismatch of datatypes:

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

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

but order_items.id_order references items.id, so they should be the same.

Try changing order_items id_order to medium int:

order_items (
    id_order        mediumint unsigned not null,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文