Cannot create foreign key out of title columns in MySQL

79 Views Asked by At

I am trying to create a foreign key.

Trying to Create Foreign Key

The columns in both tables are VARCHAR(255) and neither is a PK, however I keep getting this error message:

Foreing Key Error Message

I can’t figure out how to make a fk out of the topics title and the posts’s topic_title.

Thank you in advance for your help, much needed.

Have a very nice day, Ana

1

There are 1 best solutions below

5
Gordon Linoff On

Only define foreign keys to primary keys. That is simply a best practice.

In order to add a foreign key in MySQL, though, you need to declare the column in the referred table as a primary key, as unique, or using an index. I strongly discourage you from using just any key. At the very least it should be unique. But preferably a primary key.