简体   繁体   中英

Export and import data from PHPMYADMIN

I exported data with phpMyAdmin, but when I import the data I get this error:

#1452 - Cannot add or update a child row: a foreign key constraint fails

I can set the data accordingly and then I don't get the error. But is there a better way to do this? Something like disabling some options in phpMyAdmin or adding some query to SQL?

The problem is pma doesnt care about the order for the insert rows. so it happens a table-row is inserted with an FK where the FK row is not yet imported.

To solve this use the checkbox Disable Foreign Key Checks when exporting from PhpMyadmin. Or set it yourself:

SET FOREIGN_KEY_CHECKS=0;

and at the end:

SET FOREIGN_KEY_CHECKS=1;

That error ( Cannot add or update a child row: a foreign key constraint fails ) is referenced in the MySQL FK Doc

To add a reference between 2 tables the condition must fit for existing data.

That means if you say table1.id = table2.id then all ids of table1 and table2 must match together.

To solve that you have to eliminate or fix those rows that don't match.
Example:

table1.id  |  table2.fk
   1       |       1      ok 
   2       |     null     error
   3       |       4      error if id 4 is not in table1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM