MySQL CREATE TABLE&外键错误

时间:2011-10-27 05:40:20

标签: mysql mysql-error-1064

我不是一个数据库家伙,但我一直在攻击这个,但似乎无法解决这个问题。我已经阅读了相关的文档页面(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;

帮助将不胜感激 - 特别是如果你能解释我错过了什么。

2 个答案:

答案 0 :(得分:5)

PK之后你错过了一个逗号:

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

如果你想强制执行外键,你会想要使用InnoDB引擎:

) AUTO_INCREMENT=50 ENGINE=InnoDB;

答案 1 :(得分:-1)

我相信你不知道自己在做什么。您的代码充满了错误。

user_id int(10)unsigned NOT NULL默认&#39; 0&#39;, order_id int(10)unsigned NOT NULL默认&#39; 0&#39;, PRIMARY KEY(user_orders_user_iduser_orders_order_id

user_orders_user_id来自哪里。为什么加倍。 花点时间阅读你的代码。它应该是 ,PRIMARY KEY(user_idorder_id

FOREIGN KEY(user_id)REFERENCES用户(id)     关于更新级联的删除限制, FOREIGN KEY(order_id)REFERENCES order(id)     关于更新级联的删除限制, )AUTO_INCREMENT = 50;

我建议你采取不同的方法。 首先重新订购逗号,以便轻松切换它们。 然后将#放在你的FK线前面。

    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_id`, `order_id`)
#, FOREIGN KEY (user_id) REFERENCES userS(id)  ON DELETE RESTRICT ON UPDATE CASCADE
# , FOREIGN KEY (order_id) REFERENCES order_id) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB 
# AUTO_INCREMENT=50 
DEFAULT CHARSET=utf8
;

简化。 这至少会运行。 然后一次取消注释1行。

输入

SHOW ENGINE INNODB状态;

每当你得到奇怪的mysql错误。寻找

------------------------
LATEST FOREIGN KEY ERROR
------------------------
160323 10:25:43 Error in foreign key constraint of table DEMO/user_orders:
FOREIGN KEY (user_id) REFERENCES userS(id)  ON DELETE RESTRICT ON UPDATE CASCADE

这导致以下解决方案。

    CREATE TABLE `USERS` 
( `ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT
, `NAME` VARCHAR(10) NOT NULL
, `EMAIL` VARCHAR(20) NOT NULL
, `ACTIVE` BOOLEAN NOT NULL default 0
, CONSTRAINT PK PRIMARY KEY (`ID`) USING BTREE
, UNIQUE KEY `UK_ID` (`ID`) USING BTREE
, UNIQUE KEY `UK_NAME` (`NAME`)
, UNIQUE KEY `UK_EMAIL` (`EMAIL`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

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_id`, `order_id`)
, FOREIGN KEY (user_id) REFERENCES USERS(id)  ON DELETE RESTRICT ON UPDATE CASCADE
# , FOREIGN KEY (order_id) REFERENCES order_id ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB 
# AUTO_INCREMENT=50 
DEFAULT CHARSET=utf8
;