MySQL Cannot Add Foreign Key Constraint

后端 未结 22 1844
时光取名叫无心
时光取名叫无心 2020-11-22 08:35

So I\'m trying to add Foreign Key constraints to my database as a project requirement and it worked the first time or two on different tables, but I have two tables on which

22条回答
  •  心在旅途
    2020-11-22 08:58

    Confirm that the character encoding and collation for the two tables is the same.

    In my own case, one of the tables was using utf8 and the other was using latin1.

    I had another case where the encoding was the same but the collation different. One utf8_general_ci the other utf8_unicode_ci

    You can run this command to set the encoding and collation for a table.

    ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    

    I hope this helps someone.

提交回复
热议问题