0

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`)) 
ind_m
  • 1
  • 1
  • possible duplicate of [Cannot add or update a child row: a foreign key constraint fails](http://stackoverflow.com/questions/5005388/cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails) – Brian Driscoll Feb 25 '14 at 17:32
  • @BrianDriscoll you mean duplicate of id_pasien? – ind_m Feb 26 '14 at 13:51

1 Answers1

0

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.

Rob Bailey
  • 1,747
  • 15
  • 18