ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails for existing tables ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails for existing tables sql sql

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails for existing tables


The issue is not the command, but the data. There's a value in the child table that does not exist in the parent table.

Try using something like this to figure out which data is causing the issue:

SELECT n_id FROM c_int WHERE n_id NOT IN (SELECT n_id FROM cdb)


it seems there are some records in c_int table which their n_id values are not available on cdn table.

I suggest two solution

first one disable constraint checking

SET FOREIGN_KEY_CHECKS = 0;ALTER TABLE c_int ADD FOREIGN KEY (n_id) REFERENCES cdb (n_id);

Second remove or update those data

delete FROM c_int WHERE n_id NOT IN (SELECT n_id FROM cdb)


Please check the collation and charset of both the fields.cdb(n_id) and column from parent table