1

当我尝试通过如下更改查询将外键添加到已创建的表时,出现以下错误。有两个表 usertype 包含 usertypeid 字段作为主键和 account 表,其中 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
4

1 回答 1

1

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

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

于 2013-08-19T08:25:33.383 回答