更改表格给出错误#1064

时间:2013-10-08 14:12:26

标签: php mysql phpmyadmin

我正在尝试在phpmyadmin中创建两个表:Users和Keys,架构如下:

用户

id int auto_incerement primary key

name varchar(50) not null

user_id int

keys varchar(50) not null

现在,我正在运行以下查询,以使user_id Keys中的id引用Users中的ALTER TABLE Keys FOREIGN KEY(user_id) REFERENCES Users(id) ON UPDATE CASCADE ON DELETE CASCADE

#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 'Keys FOREIGN KEY(user_id) REFERENCES Users(id) ON UPDATE CASCADE ON DELETE CA' at line 1

但执行后,我收到以下错误:

{{1}}

谁能告诉我我做错了什么?

提前致谢。

3 个答案:

答案 0 :(得分:2)

Keys是一个reserved word,必须包含在反引号中:

ALTER TABLE `Keys`
ADD FOREIGN KEY (user_id) REFERENCES `Users` (`id`)
ON UPDATE CASCADE
ON DELETE CASCADE

最佳做法是在反引号中包含所有与系统相关的单词。

答案 1 :(得分:2)

KEYS是保留关键字。另见:http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

你需要用反引号包装它们:

ALTER TABLE `Keys`
...

答案 2 :(得分:2)

您需要使用back tick.

包含密钥
ALTER TABLE `Keys`