添加外键时Mysql#1064错误

时间:2013-07-27 17:04:32

标签: mysql foreign-keys

当我尝试通过alter query将外键添加到已创建的表时,我收到以下错误,如下所示。 有两个表usertype包含usertypeid字段作为主键 和帐户表,其中包含我想成为外键的usertypeid。

我正在使用以下查询,因为我知道查询是正确的,但不知道为什么我收到此错误。

alter table account add constraint fk_usertypeid foreign key usertypeid references usertype(usertypeid) 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 'references usertype(usertypeid) on update cascade on delete cascade' at line 1

1 个答案:

答案 0 :(得分:1)

我收到错误,因为没有在外键字段上创建索引。

不知道为什么,但必须在其上创建主键,唯一或索引所需的外键。

相关问题