简体   繁体   中英

cannot add or update a child row a foreign key constraint fails mysql Error Number: 1452

the berita_ukm table

CREATE TABLE `berita_ukm` (
  `id_berita` int(11) NOT NULL AUTO_INCREMENT,

  `id_admin` int(11) DEFAULT NULL,

  `judul_berita` varchar(45) DEFAULT NULL,

  `content` varchar(225) DEFAULT NULL,

  `tanggal` date DEFAULT NULL,

  PRIMARY KEY (`id_berita`),
  FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`)
)

admin table

CREATE TABLE `berita_ukm` (
  `id_admin` int(11) NOT NULL AUTO_INCREMENT,

  `name` int(11) DEFAULT NULL,

  PRIMARY KEY (`id_admin`),
)

and i found error like this

Error Number: 1452

Cannot add or update a child row: a foreign key constraint fails 
(`tugas_akhir`.`berita_ukm`, CONSTRAINT `berita_ukm_ibfk_1`
FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`))

INSERT INTO `berita_ukm` 
(`id_berita`, `tanggal`, `judul_berita`, `content`) 
VALUES ('34', '3/25/2014', 'putri', 'nfdn')

please help me what to do. thank you

I guess you misplaced the col names in your insert("id_berita" in place of "id_admin" ), since col "id_berita" is auto increment, you don't need to provide value for that col during insert.

INSERT INTO `berita_ukm` 
(`id_admin`, `tanggal`, `judul_berita`, `content`) 
VALUES ('34', '3/25/2014', 'putri', 'nfdn')

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