1. The type or size of the two fields does not match exactly. For example, if one is an int (10), then the foreign key must also be set to int (10), not int (11), or tinyint. In addition, you must determine whether the two fields are one signed, and the other is unsigned (i.e. unsigned), which must match exactly, more information about signed and unsigned, see:/http www.verysimple.com/blog/?p=57
2. The field attempting to set the foreign key is not indexed, or is not a primary key (primary key). If one of these is not primary key, you must first create an index for it.
3. One or two of these tables is a table of the MyISAM engine. If you want to use a FOREIGN key constraint, the table must be the InnoDB engine (in fact, if two tables are MyISAM engines, this error does not occur at all, but it does not generate foreign keys, only indexes) you need to check the engine type of the table.
4. The name of the foreign key cannot be duplicated. You should check your database to make sure that the name is unique, or you should add a few random characters after the key name to test if this is the reason.
5. You may have set on DELETE set NULL, but the field of the related key is also set to nots null value. You can resolve this by modifying the property value of the Cascade or by setting the field property to allow null.
6. Make sure that your charset and collate options are consistent at the table level and at the field level.
7. You may have set a default value for the foreign key, such as default=0.
8. A syntax error in the ALTER declaration
9. Make sure the Collate collation!
MySQL Create foreign key error (Note database table field sort)