简体   繁体   中英

What are some naming conventions, if any, to follow when naming foreign keys in MySQL?

I encountered an error while updating MySQL database. The error is Can't write; duplicate key in table 'XXX' Can't write; duplicate key in table 'XXX' .

What I did to remove the error was to rename the foreign key based on the table names involved. I do not know why it worked. Are there certain naming conventions to follow when naming foreign keys in MySQL?

NO there are no such hardcoded naming conventions when it comes to naming a foreign key. However most of the time it is preferred to name it like FK_XXXXXXXXXX.

As far as your error is concerned it looks like your table is having a unique/primary key constraint on your column which is restricting you to insert duplicate value in that column. You need to check that.( Without the actual table schema, query and table values it is only possible to guess. :) )

Your question is pretty vague. So, my answer will have to involve guesswork. Like Rahul mentioned, there are no hardcoded naming conventions. As to why you have to change your foreign key name to get things working, my guess is that you may have multiple foreign keys with the same name.

Check out this question on the error that you encountered.

Error 1022 - Can't write; duplicate key in table

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