简体   繁体   中英

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

I want import data from excel to db. I don't understand how to resolve this problem. I will be happy if you find out why this problem happens. Here is my SQL:

CREATE TABLE IF NOT EXISTS `berobat` (
`id_berobat` int(11) NOT NULL AUTO_INCREMENT,
`tgl_berobat` date NOT NULL,
`id_pasien` int(11) NOT NULL,
`id_puskesmas` int(11) NOT NULL,
`id_penyakit` int(11) NOT NULL,
PRIMARY KEY (`id_berobat`),
KEY `id_pasien` (`id_pasien`),
KEY `id_puskesmas` (`id_puskesmas`),
KEY `id_penyakit` (`id_penyakit`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `berobat`
--

Mysql said :

#1452 - Cannot add or update a child row: a foreign key constraint fails (`dinkes`.`berobat`, CONSTRAINT `berobat_ibfk_1` FOREIGN KEY (`id_pasien`) REFERENCES `pasien` (`id_pasien`)) 

Your excel spreadsheet must have the rows related to the 'pasien' table come before the related rows in the 'berobat' table. Said another way, if you try to insert a row into the 'berobat' table that contains a value for the field 'id_pasien' that is not already stored in the 'pasien' table, then you will get this error.

So, you should be able to just sort your excel spreadsheet to have the 'pasien' table's rows on top, truncate your database tables to make sure they are clean and ready for a re-import, and then try again.

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