Problems to perform a massive load on mysql


It turns out that I have created the following table:

cabtm char (3) not null,
cabdoc char(8) not null,
cabdat date,
cabven char (3) not null,
cabcli char (12) not null,
primary key (cabtm,cabdoc),
index cabecera_fkindex1(cabven),
foreign key(cabven)
references vendedor (codven)
on delete no action,
index cabecera_fkindex2 (cabcli),
foreign key (cabcli)
references cliente (codcli)
on delete no action);

and I need to carry out a massive load of data but it appears to me


ERROR 1452 (23000): Can not add or update to child row: a foreign key   constraint fails ( actividad1 . cabecera , CONSTRAINT    cabecera_ibfk_2 FOREIGN KEY ( cabcli ) REFERENCES cliente   ( codcli ) ON DELETE NO ACTION),

I have already searched and can not find the error, could you help me? Thanks

asked by CONNIE 16.08.2018 в 21:17

1 answer


The errors that you are given refer to trying to create the data of that table WITHOUT having created the data of the referred tables.

To solve it, you have a couple of solutions:

1.- Look for all the INSERT of the CLIENT and SELLER tables, and put them BEFORE the INSERT of the table that gives you problems. The order is important in most cases, because there must be values in some tables BEFORE creating those of others that have dependencies.

2.- Deactivates the validation of the Foreign_keys .     a) execute "SET FOREIGN_KEY_CHECKS = 0;"; to deactivate the validation     b) execute all the insert that you have of your massive load     c) execute "SET FOREIGN_KEY_CHECKS = 1;"; to activate validations again




The second option, although valid, is quite dangerous, if in your massive load you have values in the table that gives you problems, which do not correspond to values of CLIENT or SELLER. This can cause inconsistency in your DB. So, in case you need to use the second option, SECURE that the data in your tables is correct.

answered by 17.08.2018 в 00:19