user9435526
user9435526

Reputation:

MySQL error code (Cannot add or update child row..)

Here is one of my tables:

CREATE TABLE IF NOT EXISTS `mydb`.`Branch` (
`branchID` CHAR(5) NOT NULL,
`branchAddress` VARCHAR(40) NULL,
`branchPhoneNo` VARCHAR(20) NULL,
`productCode` CHAR(5) NULL,
PRIMARY KEY (`branchID`))
ENGINE = InnoDB;

and here are the inserts for that table:

START TRANSACTION;
USE `mydb`;
INSERT INTO `mydb`.`Branch` (`branchID`, `branchAddress`, `branchPhoneNo`, 
`productCode`) VALUES ('BR198', 'Waterford', '051786796', 'P1234');
INSERT INTO `mydb`.`Branch` (`branchID`, `branchAddress`, `branchPhoneNo`, 
`productCode`) VALUES ('BR221', 'Cork', '021475859', 'P5678');
INSERT INTO `mydb`.`Branch` (`branchID`, `branchAddress`, `branchPhoneNo`, 
`productCode`) VALUES ('BR330', 'Waterford', '051846784', 'P1122');
INSERT INTO `mydb`.`Branch` (`branchID`, `branchAddress`, `branchPhoneNo`, 
`productCode`) VALUES ('BR38', 'Dublin', '01784767', 'P3344');
INSERT INTO `mydb`.`Branch` (`branchID`, `branchAddress`, `branchPhoneNo`, 
`productCode`) VALUES ('BR001', 'Longford', '041875589', 'P5566');

COMMIT;

And here is my second table:

CREATE TABLE IF NOT EXISTS `mydb`.`Employee` (
`empNo` CHAR(5) NOT NULL,
`name` VARCHAR(25) NULL,
`department` VARCHAR(10) NULL,
`Branch_branchID` CHAR(5) NOT NULL,
`Manager_managerID` CHAR(5) NOT NULL,
PRIMARY KEY (`empNo`),
INDEX `fk_Employee_Branch1_idx` (`Branch_branchID` ASC),
INDEX `fk_Employee_Manager1_idx` (`Manager_managerID` ASC),
CONSTRAINT `fk_Employee_Branch1`
FOREIGN KEY (`Branch_branchID`)
REFERENCES `mydb`.`Branch` (`branchID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Employee_Manager1`
FOREIGN KEY (`Manager_managerID`)
REFERENCES `mydb`.`Manager` (`managerID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

And here are the inserts for that table as well:

START TRANSACTION;
USE `mydb`;
INSERT INTO `mydb`.`Employee` (`empNo`, `name`, `department`, 
`Branch_branchID`, `Manager_managerID`) VALUES ('EM092', 'Dami Kerry', 
'Garden', 'BR198', 'M3321');
INSERT INTO `mydb`.`Employee` (`empNo`, `name`, `department`, 
`Branch_branchID`, `Manager_managerID`) VALUES ('EM782', 'Donna Kinsella', 
'Living', 'BR221', 'M7789');
INSERT INTO `mydb`.`Employee` (`empNo`, `name`, `department`, 
`Branch_branchID`, `Manager_managerID`) VALUES ('EM109', 'Georgina Hughes', 
'Bathroom', 'BR330', 'M9034');
INSERT INTO `mydb`.`Employee` (`empNo`, `name`, `department`, 
`Branch_branchID`, `Manager_managerID`) VALUES ('EM728', 'Joe Bloggs', 
'Kitchen', 'BR938', 'M2945');
INSERT INTO `mydb`.`Employee` (`empNo`, `name`, `department`, 
`Branch_branchID`, `Manager_managerID`) VALUES ('EM211', 'Michael Carroll', 
'Bedroom', 'BR001', 'M0921');

COMMIT;

Here is the error message I keep getting:

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (mydb.employee, CONSTRAINT fk_Employee_Branch1 FOREIGN KEY (Branch_branchID) REFERENCES branch (branchID) ON DELETE NO ACTION ON UPDATE NO ACTION)

I am pretty much new to MySql as you can probably tell. Please, any bit of help/advise would be very much appreciated!

Thanks in advance!

Upvotes: 0

Views: 88

Answers (1)

Joshua Joel Cleveland
Joshua Joel Cleveland

Reputation: 537

check the queries.

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

the error appears when you try to delete a record that has dependency on another table, or when you try to add a record with a non-existent dependency on another table.

I saw the insert queries and found the Pk "BR38" in table "Branch" is not equal to the Fk "BR938" you trying to insert in Employee:

INSERT INTO `mydb`.`Branch` (`branchID`, `branchAddress`, `branchPhoneNo`, 
`productCode`) VALUES ('BR38', 'Dublin', '01784767', 'P3344');

INSERT INTO `mydb`.`Employee` (`empNo`, `name`, `department`, 
`Branch_branchID`, `Manager_managerID`) VALUES ('EM728', 'Joe Bloggs', 
'Kitchen', 'BR938', 'M2945');

Correct the FK in the query and Let me know if it worked.

Upvotes: 0

Related Questions