中间表上的 CREATE TABLE 问题

发布于 2024-12-11 23:09:19 字数 1061 浏览 0 评论 0原文

使其正常工作时遇到问题。 MySQL 不断抛出以下错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (order_id) REFERENCES order' at line 6

SQL 是:

DROP TABLE IF EXISTS `user_orders`;
CREATE TABLE `user_orders` (
`user_orders_user_id` int(10) unsigned NOT NULL default '0',
`user_orders_order_id` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`user_orders_user_id`, `user_orders_order_id`),
FOREIGN KEY (user_id) REFERENCES user(id),
    ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (order_id) REFERENCES order(id)
    ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

user_orders 充当 User.idOrder.id 的中间表(即保存一个记录用户的每个订单)。我觉得我错过了一些明显的东西。 User 表没有 FK,而 Order 表有一个到 User.id 的 FK (user_id)(带有 ON DELETE CASCADE ON UPDATE CASCADE) 。

希望我只是累了,这是一个小语法问题......

having an issue getting this to work. MySQL keeps throwing the following:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (order_id) REFERENCES order' at line 6

And the SQL is:

DROP TABLE IF EXISTS `user_orders`;
CREATE TABLE `user_orders` (
`user_orders_user_id` int(10) unsigned NOT NULL default '0',
`user_orders_order_id` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`user_orders_user_id`, `user_orders_order_id`),
FOREIGN KEY (user_id) REFERENCES user(id),
    ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (order_id) REFERENCES order(id)
    ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

user_orders acts as an intermediate table for User.id and Order.id (i.e. holds a record for each order made by a user). I feel like I'm missing something obvious. The User table has no FK's and the Order table has an FK (user_id) to User.id (with ON DELETE CASCADE ON UPDATE CASCADE).

Hopefully I'm just tired and it's a minor syntax issue...

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

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

发布评论

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

评论(2

£烟消云散 2024-12-18 23:09:19

此行上不应该有一个逗号:

FOREIGN KEY (user_id) REFERENCES user(id),

此外,在您的表中没有诸如 user_idorder_id 之类的字段。事实上,您有 user_orders_user_iduser_orders_order_id

此外,您必须在 ' 之间包含 order 因为它是保留字正如 ypercube 所建议的。

You have a comma that should not be there on this line:

FOREIGN KEY (user_id) REFERENCES user(id),

Besides, in your table you have not field such as user_id and order_id. Infact you have user_orders_user_id and user_orders_order_id

Besides, you have to include the order between the ' because is a reserved word as suggested by ypercube.

很酷又爱笑 2024-12-18 23:09:19

不要使用保留关键字的字段和表名称,例如 order。如果必须,请使用 `order` 转义它:

FOREIGN KEY (order_id) REFERENCES `order`(id)

Do not use names for fields and tables that are reserved keywords, like order. Escape it with `order` if you have to:

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