MySQL错误代码(无法添加或更新子行..)

时间:2018-03-02 18:30:15

标签: mysql database foreign-keys mysql-workbench

这是我的一张桌子:

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;

以下是该表的插入内容:

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;

这是我的第二张表:

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;

以下是该表的插入内容:

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;

以下是我不断收到的错误消息:

错误代码:1452。无法添加或更新子行:外键约束失败(mydbemployee,CONSTRAINT fk_Employee_Branch1 FOREIGN KEY(Branch_branchID)参考branchbranchID)在更新时不执行任何操作的行动)

我对MySql很新,你可能会说。请,任何一点帮助/建议将非常感谢!

提前致谢!

1 个答案:

答案 0 :(得分:0)

检查查询。

  

无法添加或更新子行:外键约束失败

当您尝试删除与另一个表有依赖关系的记录时,或者当您尝试添加对另一个表不存在依赖关系的记录时,会出现错误。

我看到插入查询并找到了Pk" BR38"在表格"分支"不等于Fk" BR938"您尝试在员工中插入:

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');

更正查询中的FK并告诉我它是否有效。