How to resolve MySQL error "Cannot delete or update a parent row: a foreign key constraint fails"? -
cannot delete or update parent row: foreign key constraint fails (`sponge`.`taxonomy`, constraint `taxonomy_ibfk_1` foreign key (`organism_id`) references `organism` (`organism_id`))
i getting error while deleting entire record.
actually wanted deleted entire record table associated organism_id in sponge db.
my organism table is:-
create table if not exists `organism` ( `organism_id` int(11) not null auto_increment, `experts_id` int(11) not null, `literature_id` int(11) not null, `genus` varchar(255) not null, `species` varchar(255) not null, `scientific_name` varchar(255) not null, `organism_type` varchar(255) not null, `author_org` varchar(255) not null, `found_year` varchar(255) not null, `curated_year` date not null, `curated_status` varchar(10) not null, primary key (`organism_id`) ) engine=innodb default charset=latin1 auto_increment=60 ;
and taxonomy table is:
create table if not exists `taxonomy` ( `taxonomy_id` int(11) not null auto_increment, `organism_id` int(11) not null, `kingdom` varchar(255) not null, `phylum` varchar(255) not null, `class` varchar(255) not null, `order_tax` varchar(255) not null, `family` varchar(255) not null, primary key (`taxonomy_id`), key `organism_id` (`organism_id`) ) engine=innodb default charset=latin1 auto_increment=50 ;
but when using delete query:-
select a.*,u.*,s.*,b.*,c.*,d.*,e.*,i.*,m.* organism join taxonomy u on u.organism_id = a.organism_id join synonym s on s.organism_id = u.organism_id_id join biogeography b on b.organism_id = s.organism_id_id join common_name c on c.organism_id = b.organism_id_id join description d on d.organism_id = c.organism_id_id join ecology e on e.organism_id = d.organism_id_id join economic_importance on i.organism_id = e.organism_id_id join images m on m.organism_id = i.organism_id_id m.organism_id = 59
i getting above error...
how can change delete query remove above #1452 error?
as bob0t said in comments, error caused presence of values linked organism_id
, default prevents deletion of parent row.
you should change definition of taxonomy
table , add following:
create table if not exists `taxonomy` ( `taxonomy_id` int(11) not null auto_increment, `organism_id` int(11) not null, `kingdom` varchar(255) not null, `phylum` varchar(255) not null, `class` varchar(255) not null, `order_tax` varchar(255) not null, `family` varchar(255) not null, primary key (`taxonomy_id`), constraint org_id foreign key (`organism_id`) references organism(`organism_id`) on delete cascade ) engine=innodb default charset=latin1 auto_increment=50 ;
this way, when delete parent row (in table organism
) delete referencing children in table taxonomy
.
edit
for completeness of answer, delete query should simple as
delete `organism` `organism_id` = 59;
where 59
comes question where
clause, can valid id.
Comments
Post a Comment