MySql ERROR CODE:1215 Cannot add the foreign key constraint

The reason why mysql cannot add foreign keys appears
1. The data type and data length of the associated columns of the two tables are inconsistent.
For example, vARCHAR (20) is in column A of Table A, and VARCHAR (25) is in column B of table B. In this way, A and B cannot form foreign key association relationship.
 
2, when creating tables, add ENGINE=InnoDB DEFAULT CHARSET= UTf8;
CREATE TABLE `A` (

) ENGINE=InnoDB DEFAULT CHARSET=utf8;
In this way, if the B table and A table to be associated with the foreign key, then create B table also add ENGINE=InnoDB DEFAULT CHARSET= UTf8;

Read More: