![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
ALTER TABLE `desa` ADD FOREIGN KEY ( `kec_no` ) REFERENCES `umkm`.`kecamatan` (`kec_no`) Error message & fails to add foreign key : #1452 - Cannot add or update a child row: a foreign key constraint fails (`umkm`.<result 2 when explaining filename '#sql-bf4_10f'>, CONSTRAINT `#sql-bf4_10f_ibfk_1` FOREIGN KEY (`kec_no`) REFERENCES `kecamatan` (`kec_no`)) What causes the above error? |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
Lennart Jonsson wrote: On 2011-07-23 05:18, zaqi syah wrote: [...] ALTER TABLE `desa` ADD FOREIGN KEY ( `kec_no` ) REFERENCES `umkm`.`kecamatan` (`kec_no`) Error message& fails to add foreign key : #1452 - Cannot add or update a child row: a foreign key constraint fails (`umkm`.<result 2 when explaining filename '#sql-bf4_10f'>, CONSTRAINT `#sql-bf4_10f_ibfk_1` FOREIGN KEY (`kec_no`) REFERENCES `kecamatan` (`kec_no`)) What causes the above error? You can only reference unique or primary key constraints in the foreign key clause. If kecamatan is unique you can add a constraint like: ALTER TABLE dagang.kecamatan ADD CONSTRAINT ... UNIQUE ( kec_no ); and then add your foreign key [...] My (kec_no) is not unique & how to handle this? |
#5
| |||
| |||
|
|
My (kec_no) is not unique & how to handle this? |
#6
| |||
| |||
|
#7
| |||
| |||
|
#8
| |||
| |||
|
|
oh sorry my problem not solved, I try to add foreign key and error message : #1452 - Cannot add or update a child row: a foreign key constraint fails (`umkm`.<result 2 when explaining filename '#sql-fbc_267'>, CONSTRAINT `#sql-fbc_267_ibfk_1` FOREIGN KEY (`id_kecamatan`) REFERENCES `kecamatan` (`id_kecamatan`)) |
#9
| |||
| |||
|
|
oh sorry my problem not solved, I create database first & I've fixed database structure, to be as below: CREATE TABLE IF NOT EXISTS `kecamatan` ( `id_propinsi` varchar(2) DEFAULT NULL, `id_kabupaten` varchar(4) DEFAULT NULL, `id_kecamatan` varchar(7) NOT NULL, `nama_kecamatan` varchar(50) DEFAULT NULL, PRIMARY KEY (`id_kecamatan`), KEY `id_kabupaten` (`id_kabupaten`) ) ENGINE=InnoDB CREATE TABLE IF NOT EXISTS `desa` ( `id_propinsi` varchar(2) DEFAULT NULL, `id_kabupaten` varchar(4) DEFAULT NULL, `id_kecamatan` varchar(7) DEFAULT NULL, `id_desa` varchar(10) NOT NULL, `nama_desa` varchar(100) DEFAULT NULL, PRIMARY KEY (`id_desa`), KEY `id_kecamatan` (`id_kecamatan`) ) ENGINE=InnoDB Next I try to add foreign key and error message : #1452 - Cannot add or update a child row: a foreign key constraint fails (`umkm`.<result 2 when explaining filename '#sql-fbc_267'>, CONSTRAINT `#sql-fbc_267_ibfk_1` FOREIGN KEY (`id_kecamatan`) REFERENCES `kecamatan` (`id_kecamatan`)) |
#10
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |