mysql, results varying when I add a column to the select - mysql

Mysql version:
'innodb_version', '5.7.33'
'protocol_version', '10'
'slave_type_conversions', ''
'tls_version', 'TLSv1,TLSv1.1,TLSv1.2'
'version', '5.7.33-log'
'version_comment', 'Source distribution'
'version_compile_machine', 'x86_64'
'version_compile_os', 'Linux'
I have something very odd going on with a production mysql database that I can not suss out & I may struggle to explain. When I copy the database over to somewhere else everything is fine.
There are 1.5 million rows in method_profile & so far this is the only row I know to have this issue.
The problem is displayed in two ways. 1) why do I get back an invalid id of '-2147476736' & why do I get no rows back when I ad deleted_timestamp to the SQL.
This first select returns a row as I expect:
select mp.id, mp.screening_profile_id, mp.deleted, mp.deleted_timestamp
from method_profile mp
where mp.id = 481031;
So then using the screening_profile_id value I run the following SQL:
select mp.id, mp.screening_profile_id, mp.deleted from method_profile mp
where mp.screening_profile_id = 420062 ;
That id does not exist as shown by:
select mp.id from method_profile mp where mp.id = -2147476736;
If I change the second select to add another column 'mp.deleted_timestamp' I then get no row back at all:
select mp.id, mp.screening_profile_id, mp.deleted, mp.deleted_timestamp from method_profile mp
where mp.screening_profile_id = 420062 ;
The CREATE statement for the table is as follows:
CREATE TABLE `method_profile` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '5.1',
`primary_method_id` int(11) DEFAULT NULL,
`secondary_method_id` int(11) DEFAULT NULL,
`instrument_id` int(11) DEFAULT NULL,
`kit_code_id` int(11) DEFAULT NULL COMMENT '5.1 - allow NULL as option see http://youtrack/issue/PTPORTAL2-100',
`temperature_id` int(11) DEFAULT NULL,
`name` varchar(180) DEFAULT NULL,
`slope` decimal(10,3) DEFAULT NULL COMMENT '7.7',
`intercept` decimal(10,3) DEFAULT NULL COMMENT '7.7',
`deleted` bit(1) NOT NULL DEFAULT b'0',
`screening_profile_id` int(11) NOT NULL,
`round_id_valid_from` int(11) DEFAULT NULL,
`round_id_valid_to` int(11) DEFAULT NULL,
`measurement_unit_id` int(11) DEFAULT NULL,
`instrument_ref` varchar(20) DEFAULT NULL,
`generated_primary_method_id` int(11) GENERATED ALWAYS AS (ifnull(`primary_method_id`,0)) VIRTUAL NOT NULL,
`generated_secondary_method_id` int(11) GENERATED ALWAYS AS (ifnull(`secondary_method_id`,0)) VIRTUAL NOT NULL,
`generated_instrument_id` int(11) GENERATED ALWAYS AS (ifnull(`instrument_id`,0)) VIRTUAL NOT NULL,
`generated_instrument_ref` varchar(20) GENERATED ALWAYS AS (ifnull(`instrument_ref`,'')) VIRTUAL NOT NULL,
`generated_round_id_valid_from` int(11) GENERATED ALWAYS AS (ifnull(`round_id_valid_from`,0)) VIRTUAL NOT NULL,
`generated_round_id_valid_to` int(11) GENERATED ALWAYS AS (ifnull(`round_id_valid_to`,0)) VIRTUAL NOT NULL,
`generated_kit_code_id` int(11) GENERATED ALWAYS AS (ifnull(`kit_code_id`,0)) VIRTUAL NOT NULL,
`generated_temperature_id` int(11) GENERATED ALWAYS AS (ifnull(`temperature_id`,0)) VIRTUAL NOT NULL,
`generated_name` varchar(180) GENERATED ALWAYS AS (ifnull(`name`,'')) VIRTUAL NOT NULL,
`generated_slope` decimal(10,3) GENERATED ALWAYS AS (ifnull(`slope`,0.0)) VIRTUAL NOT NULL,
`generated_intercept` decimal(10,3) GENERATED ALWAYS AS (ifnull(`intercept`,0.0)) VIRTUAL NOT NULL,
`generated_measurement_unit_id` int(11) GENERATED ALWAYS AS (ifnull(`measurement_unit_id`,0)) VIRTUAL NOT NULL,
`inserted_at` datetime DEFAULT CURRENT_TIMESTAMP,
`last_updated` datetime DEFAULT CURRENT_TIMESTAMP,
`deleted_timestamp` timestamp NULL DEFAULT NULL,
`generated_deleted_timestamp` timestamp GENERATED ALWAYS AS (ifnull(`deleted_timestamp`,'0000-00-00 00:00:00')) VIRTUAL NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `method_profile_instrument_ref_uniq_idx` (`screening_profile_id`,`generated_primary_method_id`,`generated_secondary_method_id`,`generated_instrument_id`,`generated_instrument_ref`,`generated_round_id_valid_from`,`generated_round_id_valid_to`,`generated_kit_code_id`,`generated_temperature_id`,`generated_name`,`generated_slope`,`generated_intercept`,`generated_measurement_unit_id`,`deleted`,`generated_deleted_timestamp`),
KEY `fk_screening_profile_Kit_code1_idx` (`kit_code_id`),
KEY `fk_screening_profile_temperature1_idx` (`temperature_id`),
KEY `fk_screening_profile_method1_idx` (`primary_method_id`),
KEY `fk_screening_profile_method2_idx` (`secondary_method_id`),
KEY `fk_screening_profile_instrument1_idx` (`instrument_id`),
KEY `fk_method_profile_screening_profile` (`screening_profile_id`),
KEY `fk_method_profile_round_id_valid_from` (`round_id_valid_from`),
KEY `fk_method_profile_round_id_valid_to` (`round_id_valid_to`),
KEY `fk_method_profile_measurement_unit_id` (`measurement_unit_id`),
CONSTRAINT `fk_method_profile_instrument` FOREIGN KEY (`instrument_id`) REFERENCES `instrument` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_kit_code` FOREIGN KEY (`kit_code_id`) REFERENCES `kit_code` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_measurement_unit_id` FOREIGN KEY (`measurement_unit_id`) REFERENCES `measurement_units` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_primary_method` FOREIGN KEY (`primary_method_id`) REFERENCES `method` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_round_id_valid_from` FOREIGN KEY (`round_id_valid_from`) REFERENCES `round` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_round_id_valid_to` FOREIGN KEY (`round_id_valid_to`) REFERENCES `round` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_screening_profile` FOREIGN KEY (`screening_profile_id`) REFERENCES `screening_profile` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_secondary_method` FOREIGN KEY (`secondary_method_id`) REFERENCES `method` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_temperature` FOREIGN KEY (`temperature_id`) REFERENCES `temperature` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1614629 DEFAULT CHARSET=utf8mb4;

Related

MySQL queries working only on windows but no on linux

I have generated a script using Script-Migration and applied the script locally on the database and it worked on MySQL workbench on windows.
But once I tried to apply the same script on a cloned database on a Linux machine it keeps generating the error "Cannot add foreign key".
Edited
CREATE TABLE `NoticesOfSchoolReportForms` (
`Id` varchar(50) NOT NULL,
`EducationOfficeManagerName` varchar(50) NOT NULL,
`AssignmentNumber` int NOT NULL,
`Date` DateTime NOT NULL,
`ModifiedById` varchar(50) NULL,
`CreatedById` varchar(50) NOT NULL,
`ModificationDate` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`CreatedDate` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`Deleted` tinyint(1) NOT NULL DEFAULT FALSE,
`SupervisorId` varchar(50) NOT NULL,
`SchoolId` varchar(50) NOT NULL,
CONSTRAINT `PK_NoticesOfSchoolReportForms` PRIMARY KEY (`Id`),
CONSTRAINT `FK_NoticesOfSchoolReportForms_Schools_SchoolId` FOREIGN KEY (`SchoolId`) REFERENCES `Schools` (`Id`) ON DELETE CASCADE,
CONSTRAINT `FK_NoticesOfSchoolReportForms_SuperVisors_SupervisorId` FOREIGN KEY (`SupervisorId`) REFERENCES `SuperVisors` (`Id`) ON DELETE CASCADE
);

foreign key constraint field set to NULL not working

I want to allow NULL values to a field with foreign key constraint set.
Here's the table schema:
CREATE TABLE `internal_team_head` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`internal_team_id` int(11) NOT NULL,
`users_id` int(11) NOT NULL DEFAULT '0',
`type` enum('lead','project_manager') NOT NULL,
`updated_by` int(6) DEFAULT NULL,
`updated_on` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`,`internal_team_id`,`users_id`,`type`),
KEY `fk_internal_team_has_users_users1_idx` (`users_id`),
KEY `fk_internal_team_has_users_internal_team1_idx` (`internal_team_id`),
CONSTRAINT `fk_internal_team_has_users_internal_team1` FOREIGN KEY (`internal_team_id`) REFERENCES `internal_team` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=69 DEFAULT CHARSET=utf8
Now I want to change users_id to allow NULL values and default to NULL.
ALTER TABLE `internal_team_head` CHANGE `users_id` `users_id` INT(11) NULL DEFAULT NULL;
The Alter query executes successfully but users_id Null attribute is still set to No and default value set to 0.
How do I enforce the changes?
Dropping foreign key constraint first, then running your alter statement should work. You can add the constraint back again after that.

Foreign key constraint fails with Insert Into Select in MySQL

I'm quite new to SQL, and I'm trying to upload data to my tables. For this I have special tables where I upload the data from a CSV file, and then, from this table, I am trying to copy the data to the final table.
But now I have a problem with an intermediate table where I have uploaded my data. The table is:
CREATE TABLE `_work_has_person` (
`work_id` int(11) NOT NULL,
`person_id` int(11) NOT NULL,
`primary_contribution_id` int(11) DEFAULT NULL,
PRIMARY KEY (`work_id`,`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I want to copy the data in
CREATE TABLE `work_has_person` (
`work_id` int(11) NOT NULL,
`person_id` int(11) NOT NULL,
`primary_contribution_id` int(11) NOT NULL,
PRIMARY KEY (`work_id`,`person_id`),
KEY `fk_work_has_person_person1_idx` (`person_id`),
KEY `fk_work_has_person_work1_idx` (`work_id`),
KEY `fk_work_has_person_primary_contribution1_idx` (`primary_contribution_id`),
CONSTRAINT `fk_work_has_person_person1` FOREIGN KEY (`person_id`) REFERENCES `person` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_work_has_person_primary_contribution1` FOREIGN KEY (`primary_contribution_id`) REFERENCES `primary_contribution` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_work_has_person_work1` FOREIGN KEY (`work_id`) REFERENCES `work` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Which is an intermediate table between:
CREATE TABLE `work` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title_work` varchar(250) DEFAULT NULL,
`subtitle_work` varchar(250) DEFAULT NULL,
`date_work` varchar(45) DEFAULT NULL,
`unix_date_work` varchar(100) DEFAULT NULL,
`sinopsis` text,
`ref_bne` varchar(100) DEFAULT NULL,
`ref_alt` longtext,
`language_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_work_language1_idx` (`language_id`),
KEY `title_work` (`title_work`),
CONSTRAINT `fk_work_language1` FOREIGN KEY (`language_id`) REFERENCES `language` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=24610 DEFAULT CHARSET=utf8;
and
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`img_person` varchar(250) DEFAULT NULL,
`born_date` varchar(45) DEFAULT NULL,
`unix_born_date` varchar(100) DEFAULT NULL,
`city_born_date` varchar(100) DEFAULT NULL,
`country_born_date` varchar(100) DEFAULT NULL,
`death_date` varchar(45) DEFAULT NULL,
`unix_death_date` varchar(100) DEFAULT NULL,
`city_death_date` varchar(100) DEFAULT NULL,
`country_death_date` varchar(45) DEFAULT NULL,
`biography` longtext,
`ref_bne` varchar(100) DEFAULT NULL,
`ref_alt` longtext,
PRIMARY KEY (`id`),
UNIQUE KEY `name_UNIQUE` (`name`),
KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=9159 DEFAULT CHARSET=utf8;
But everytime I try to run
INSERT INTO work_has_person (work_id, person_id, primary_contribution_id)
SELECT work_id, person_id, primary_contribution_id
FROM _work_has_person;
It says
Cannot add or update a child row: a foreign key constraint fails (`cdu93hfg93r`.
`work_has_person`, CONSTRAINT `fk_work_has_person_person1` FOREIGN KEY (`person_id`)
REFERENCES `person` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
I am pretty sure that the tables has the neccesary data, but, ¿is there a way to know which data fails? I have seen Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails: but don't understand exactly how to use it here.
A.
It is relatively easy to find out what data is causing the conflict: get all person_ids from _work_has_person that are not in the persons table. You can achieve this via an outer join and filtering for person.id is null in the where clause.
select * from `_work_has_person` whp
left join person p on whp.person_id=p.id
where p.id is null
You can actually remove such data from the results being inserted by including the reverse criterion into the select part of your insert query (an inner join):
INSERT INTO work_has_person (work_id, person_id, primary_contribution_id)
SELECT whp.work_id, whp.person_id, whp.primary_contribution_id
FROM _work_has_person whp
INNER join person p on whp.person_id=p.id

Mysql innerjoin very slow

I have an SQL query that takes over 4 hours to complete.
The query is as follows
UPDATE carepathaction a1
INNER JOIN carepathaction a2
ON a1.sourceoid = a2.oid
INNER JOIN carepathgroup g
ON g.sourceoid = a2.carepathgroupoid
INNER JOIN carepathphase f
ON f.oid = g.carepathphaseoid
SET a1.carepathgroupoid = g.oid
WHERE f.code = 'EDIT'
He has to update 65000 items. Is there some optimalization i can do in this query?
CarePathAction
CREATE TABLE `carepathaction`
(
`oid` INT(11) NOT NULL,
`typeid` CHAR(1) NOT NULL DEFAULT 'X',
`sequencenumber` INT(11) NOT NULL,
`description` VARCHAR(1024) NOT NULL,
`duration` INT(11) NOT NULL comment 'In minutes',
`deadline` INT(11) NOT NULL comment
'# dagen in combinatie met deadlineType (sinds begin traject of sinds begin deze actie)'
,
`deadlinetype` INT(11) NOT NULL comment
'1) = deadline sinds zorgpad\n2) = deadline sinds start deze actie\n3) = deadline sinds zorgpadfase'
,
`repeatperweek` INT(11) DEFAULT NULL,
`numbertorepeat` INT(11) DEFAULT NULL,
`carepathoid` INT(11) DEFAULT NULL,
`usertaskoid` INT(11) DEFAULT NULL,
`templateattachment` VARCHAR(45) DEFAULT NULL,
`partyrolecategoryoid` INT(11) DEFAULT NULL,
`automatic` BIT(1) NOT NULL DEFAULT b'0',
`carepathphaseoid` INT(11) DEFAULT NULL,
`parentoid` INT(11) DEFAULT NULL,
`documentoid` INT(11) DEFAULT NULL,
`epdtemplateoid` INT(11) DEFAULT NULL,
`producttypeoid` INT(11) DEFAULT NULL,
`evaluateby` INT(11) DEFAULT NULL,
`pathlock` BIT(1) NOT NULL DEFAULT b'0',
`carepathgroupoid` INT(11) NOT NULL,
`activitygroupoid` INT(11) DEFAULT NULL,
`title` VARCHAR(45) DEFAULT NULL,
`carepathactionswitchtype` INT(11) DEFAULT NULL,
`modulesdomaincode` VARCHAR(10) DEFAULT NULL,
`startday` INT(11) NOT NULL DEFAULT '0',
`sourceoid` INT(11) DEFAULT NULL,
`templatefilename` VARCHAR(100) DEFAULT NULL,
`istask` BIT(1) NOT NULL DEFAULT b'0',
`typecontact` INT(11) DEFAULT NULL,
`carepathactionepdoid` INT(11) DEFAULT NULL,
`carepathactiontreatmentplanoid` INT(11) DEFAULT NULL,
`clientportalenabled` BIT(1) DEFAULT NULL,
`startafternumberofdaysepddatefield` INT(11) DEFAULT NULL,
`startafterdateepdfieldoid` INT(11) DEFAULT NULL,
`epdtemplatefieldoid` INT(11) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `carepathactionfk1_idx` (`carepathoid`),
KEY `carepathactionfk2_idx` (`carepathphaseoid`),
KEY `carepathactionfk3_idx` (`documentoid`),
KEY `carepathactionfk4_idx` (`epdtemplateoid`),
KEY `carepathactionfk5_idx` (`producttypeoid`),
KEY `carepathactionfk6_idx` (`carepathgroupoid`),
KEY `carepathactionfk6_idx1` (`activitygroupoid`),
KEY `carepathactionfk7_idx` (`parentoid`),
KEY `carepathactionfk8_idx` (`usertaskoid`),
KEY `carepathactionfk10_idx` (`partyrolecategoryoid`),
KEY `carepathactionfk11_idx` (`carepathactionepdoid`),
KEY `carepathactionfk12_idx` (`carepathactiontreatmentplanoid`),
KEY `carepathactionfk13_idx` (`startafterdateepdfieldoid`),
KEY `carepathactionfk14_idx` (`epdtemplatefieldoid`),
CONSTRAINT `carepathactionfk14` FOREIGN KEY (`epdtemplatefieldoid`)
REFERENCES `epdtemplatefield` (`oid`) ON DELETE no action ON UPDATE no
action,
CONSTRAINT `carepathactionfk1` FOREIGN KEY (`carepathoid`) REFERENCES
`carepath` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk10` FOREIGN KEY (`partyrolecategoryoid`)
REFERENCES `partyrolecategory` (`oid`) ON DELETE no action ON UPDATE no
action,
CONSTRAINT `carepathactionfk11` FOREIGN KEY (`carepathactionepdoid`)
REFERENCES `carepathaction` (`oid`) ON DELETE no action ON UPDATE no action
,
CONSTRAINT `carepathactionfk12` FOREIGN KEY (
`carepathactiontreatmentplanoid`) REFERENCES `carepathaction` (`oid`) ON
DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk13` FOREIGN KEY (`startafterdateepdfieldoid`)
REFERENCES `carepathaction` (`oid`) ON DELETE no action ON UPDATE no action
,
CONSTRAINT `carepathactionfk2` FOREIGN KEY (`carepathphaseoid`) REFERENCES
`carepathphase` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk3` FOREIGN KEY (`documentoid`) REFERENCES
`document` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk4` FOREIGN KEY (`epdtemplateoid`) REFERENCES
`epdtemplate` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk5` FOREIGN KEY (`producttypeoid`) REFERENCES
`producttype` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk6` FOREIGN KEY (`carepathgroupoid`) REFERENCES
`carepathgroup` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk7` FOREIGN KEY (`parentoid`) REFERENCES
`carepathaction` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk8` FOREIGN KEY (`usertaskoid`) REFERENCES
`tasktoschedule` (`oid`) ON DELETE no action ON UPDATE no action
)
CarePathGroup
CREATE TABLE `carepathgroup`
(
`oid` INT(11) NOT NULL,
`sequencenumber` INT(11) NOT NULL,
`title` VARCHAR(50) NOT NULL,
`carepathoid` INT(11) DEFAULT NULL,
`carepathphaseoid` INT(11) NOT NULL,
`sourceoid` INT(11) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `carepathgroupfk1_idx` (`carepathoid`),
KEY `carepathgroupfk2_idx` (`carepathphaseoid`),
CONSTRAINT `carepathgroupfk2` FOREIGN KEY (`carepathphaseoid`) REFERENCES
`carepathphase` (`oid`) ON DELETE no action ON UPDATE no action
)
CarePathPhase
CREATE TABLE `carepathphase`
(
`oid` INT(11) NOT NULL,
`code` VARCHAR(6) NOT NULL,
`description` VARCHAR(45) NOT NULL,
`sequencenumber` INT(11) NOT NULL DEFAULT '0',
`carepathoid` INT(11) DEFAULT NULL,
`carepathphasetype` INT(11) NOT NULL DEFAULT '0',
`sourceoid` INT(11) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `carepathphasefk1_idx` (`carepathoid`),
CONSTRAINT `carepathphasefk1` FOREIGN KEY (`carepathoid`) REFERENCES
`carepath` (`oid`) ON DELETE no action ON UPDATE no action
)
explain
1 SIMPLE f ALL PRIMARY 61925 Using where
1 SIMPLE g ref CarePathGroupFK2_idx CarePathGroupFK2_idx 4 amis.f.Oid 1
1 SIMPLE a1 ALL 166582 Using join buffer
1 SIMPLE a2 eq_ref PRIMARY,CarePathActionFK6_idx PRIMARY 4 amis.a1.SourceOid 1 Using where
There are two ALL join types in the explain, these are the ones you need to focus (for details see mysql documentation on explain output).
The create table statements show that:
You do not have index on the carepathphase.code field, which is used in the where criteria, so you should rectify that.
There is no index on carepathaction.sourceoid, which is used in the join, so you should create one there. You may try to create a combined index on oid, sourceoid fields (or the other way around) because mysql may be able to use the combined index to match the rows.
Run explain on the select (if you have mysql v5.6 or newer, then you can run it directly on the update) after adding the indexes and see, if the ALLs change. The 2nd all next to a2 seems to be the real critical stuff to me.

MYSQL: Cannot add or update a child row: a foreign key constraint fails

I am getting the error:
Cannot add or update a child row: a foreign key constraint fails (mydb/requests, CONSTRAINT requests_ibfk_5 FOREIGN KEY (fixture_id) REFERENCES fixtures (fix_id) ON UPDATE CASCADE ON DELETE CASCADE)
I have the following table structure:
CREATE TABLE IF NOT EXISTS `requests` (
`request_id` int(11) unsigned NOT NULL auto_increment,
`fixture_id` int(11) unsigned NOT NULL,
`user_id` int(11) unsigned NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime default NULL,
PRIMARY KEY (`request_id`),
UNIQUE KEY `fixture_id_2` (`fixture_id`,`user_id`),
KEY `user_id` (`user_id`),
KEY `date_added` (`date_added`),
KEY `fixture_id` (`fixture_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=17 ;
CREATE TABLE IF NOT EXISTS `fixtures` (
`id` int(11) unsigned NOT NULL auto_increment,
`fix_id` int(11) unsigned NOT NULL default '0',
`fixture_date` date default NULL,
`kickoff` time default NULL,
`venue` varchar(35) default NULL,
`home_score` tinyint(4) default NULL,
`away_score` tinyint(4) default NULL,
`date_added` datetime default NULL,
`date_modified` datetime default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `fix_id` (`fix_id`),
KEY `fixture_date` (`fixture_date`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=383 ;
ALTER TABLE `requests`
ADD CONSTRAINT `requests_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE,
ADD CONSTRAINT `requests_ibfk_5` FOREIGN KEY (`fixture_id`) REFERENCES `fixtures` (`fix_id`) ON DELETE CASCADE ON UPDATE CASCADE;
If I update a record on the fix_id field the parent table (fixtures), that has a shared id (fixture_id) in the child table (requests) I get the above error.
I cannot see why this integrity constraint is failing. Both tables already have the correct data it should cascade through?
Any help greatly appreciated.
This was all my own error. I had two foreign constraints on the same field in reality. I just needed to take one off.
This error occurs due to the reference of other table, in both tables same id's/data should exist. If not please use where conditions to remove not existing data.
example
update tablename a set = 'field' (select field from othertable b) where a.data = b.data;