Solution to error 1452: cannot add or update a child row: a foreign key constraint failures in MySQL

When executing a SQL query, I suddenly reported 1452, which made me totally confused.
In fact, the main reason for the error of 1452 is that there is no data in the main table corresponding to the child table with foreign keys.
For example:
Class list:
The

id

name

1

advanced classes

2

regular class
Student table:

id

name

class_id

1

* *

1

2

and

2

3

fifty

3
As a result, there is no data for the class with class_ID 3 for king 5 in the table.
So no matter how you do the update statement on king five he’s going to report an error.
 
The real reason:
 
I copied a “class list” from the official library, and then I copied a “student list”, and then I made a mistake in the operation of the student list.
Because after the class table was copied and before the student table was copied, new classes and students were added on the line. Then the data I copied here was only students, and no class was finished…
It’s embarrassing.
So don’t mess around…
 
Solution 1:
Clear both tables and redo them.
Solution 2:
Use SQL to delete data that has no data in the main table corresponding to the foreign key.
Take the above example: Delete Wang Wu.

Read More: