简体   繁体   中英

Mysql #1064 error while adding foreign key

I'm getting following error while i try to add foreign key to already created table by alter query as follows. There is two table usertype which contain usertypeid field as primary key and account table which has usertypeid which i want to be a foreign key.

I'm using the following query, and as I know the query is correct, but don't know why i'm getting this error.

alter table account add constraint fk_usertypeid foreign key usertypeid references usertype(usertypeid) on update cascade on delete cascade;

Following error occured while i try to execute query

 #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

I was getting error because index is not created on the field which is foreign key.

Don't know why but the foreign key required to be primary key, unique or a index must be created on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM