First of all, we need to clarify that there are many reasons why foreign keys cannot be added, and I have encountered only one of them to share here. It would be great if it could help everyone.
- Right click -> Design Table. Check what your database engine is, because the default engine of the table database I built is MyISAM, so after I add a foreign key, the foreign key I added will not be displayed in the foreign key column.
- Then we will change it to the one shown below, and then add the foreign key:
it will be successful
If you still get an error that foreign keys cannot be added, there may be other reasons. For example, there is data in your data table, you can try to clear the data and try again.