数据库
首页 > 数据库> > MySQL创建表和外键错误

MySQL创建表和外键错误

作者:互联网

我不是数据库专家,但我已经对此进行了一些修改,似乎无法解决这个问题.我已经阅读了相关的文档页面(http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html),但看不到语法可能存在的问题.

ERROR 1064 (42000) at line 84: 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 'FOREIGN KEY (user_id) REFERENCES user(id)
    ON DELETE RESTRICT ON UPDATE C' at line 5

这是相关的SQL-order(id)和user(id)在各自的表中自动递增int(10)字段.

DROP TABLE IF EXISTS `user_orders`;
CREATE TABLE `user_orders` (
`user_id` int(10) unsigned NOT NULL default '0',
`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 RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (order_id) REFERENCES order(id)
    ON DELETE RESTRICT ON UPDATE CASCADE,
) AUTO_INCREMENT=50;

帮助将不胜感激-特别是如果您可以解释我错过的事情.

解决方法:

您的PK之后缺少逗号:

PRIMARY KEY (`user_orders_user_id`, `user_orders_order_id`), -- <-------
FOREIGN KEY (user_id) REFERENCES user(id) ...

如果要强制使用外键,则需要使用InnoDB引擎:

) AUTO_INCREMENT=50 ENGINE=InnoDB;

标签:mysql-error-1064,mysql
来源: https://codeday.me/bug/20191102/1988565.html