mysql error 1215 Cannot add foreign key constraint is reported when creating a foreign key

Today, when dealing with the foreign key of MySQL processing table, I found that this error was reported. Here is a summary of several aspects of this error;

1. The type of the created foreign key field and the associated field do not match, or the constraints are inconsistent (eg: unsigned)

2. The length of the created foreign key field and the associated field are inconsistent

3. The foreign key name created is duplicated with the foreign key name created previously

4. The name of the associated table is inconsistent with the name you want to associate

eg:

5. The engine should be the same, both InnoDB;

6. The encoding should be the same, such as utf8;

7. The foreign key should refer to the primary key field in the reference table (that is, if the id field in the picture above is the primary key)

The above-mentioned problems generally exist. If there are more other problems, continue to supplement.

 

 

Guess you like

Origin blog.csdn.net/weixin_42575020/article/details/113248787