创建具有2个外键作为主键的表时出现MySQL语法错误

时间:2015-06-03 08:33:06

标签: mysql foreign-keys primary-key

当我运行以下查询时,我一直收到1064错误,但我不明白它有什么问题:

CREATE TABLE hashmkb_mangatracker.group_release
(
 group_id int NOT NULL,
 release_id int NOT NULL,
 PRIMARY KEY (group_id, release_id),
 UNIQUE INDEX (release_id, group_id),
 FOREIGN KEY (group_id) REFERENCES group(id),
 FOREIGN KEY (release_id) REFERENCES release(id)
) ENGINE=INNODB;

错误:

#1064 - 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 'group(id),
FOREIGN KEY (release_id) REFERENCES release(id)
) ENGINE=INNODB' at line 7 

感谢帮助。

1 个答案:

答案 0 :(得分:2)

GROUP是MySql中的保留关键字。 RELEASE也是如此。 您将不得不在这些附近添加反引号:

CREATE TABLE hashmkb_mangatracker.group_release
(
 group_id int NOT NULL,
 release_id int NOT NULL,
 PRIMARY KEY (group_id, release_id),
 UNIQUE INDEX (release_id, group_id),
 FOREIGN KEY (group_id) REFERENCES `group`(id),
 FOREIGN KEY (release_id) REFERENCES `release`(id)
) ENGINE=INNODB;

以下是所有保留字的列表:http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html