簡體   English   中英

添加外鍵時出現MySQL#1064錯誤

[英]Mysql #1064 error while adding foreign key

我在嘗試通過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

我收到錯誤消息是因為沒有在外鍵字段上創建索引。

不知道為什么,但是必須在其上創建必須是主鍵,唯一鍵或索引的外鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM