我正在尝试在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}}
谁能告诉我我做错了什么?
提前致谢。
答案 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`