中间表上的 CREATE TABLE 问题
使其正常工作时遇到问题。 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.id
和 Order.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此行上不应该有一个逗号:
此外,在您的表中没有诸如
user_id
和order_id
之类的字段。事实上,您有user_orders_user_id
和user_orders_order_id
此外,您必须在
'
之间包含order
因为它是保留字正如 ypercube 所建议的。You have a comma that should not be there on this line:
Besides, in your table you have not field such as
user_id
andorder_id
. Infact you haveuser_orders_user_id
anduser_orders_order_id
Besides, you have to include the
order
between the'
because is a reserved word as suggested by ypercube.不要使用保留关键字的字段和表名称,例如
order
。如果必须,请使用`order`
转义它:Do not use names for fields and tables that are reserved keywords, like
order
. Escape it with`order`
if you have to: