无法添加或更新子行:外键约束使表empleado和contrato失败

r7s23pms  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(199)

执行插入查询时出现以下错误:

Cannot add or update a child row: a foreign key constraint fails (`unimonito`.`empleado`, CONSTRAINT `empleado_ibfk_1` FOREIGN KEY (`cargo`) REFERENCES `cargo` (`idCargo`) ON DELETE CASCADE ON UPDATE CASCADE)

我的table是:

CREATE TABLE `contrato` (
  `idContrato` int(3) NOT NULL,
  `tipoContrato` int(3) NOT NULL,
  `descripcion` varchar(50) NOT NULL,
  `fechaInicio` date NOT NULL,
  `fechaFin` date NOT NULL,
  `cedulaEmpleado` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `empleado` (
  `idEmpleado` int(3) NOT NULL,
  `nombreEmpleado` varchar(50) NOT NULL,
  `cedula` int(10) NOT NULL,
  `telefono` int(10) NOT NULL,
  `cargo` int(3) NOT NULL,
  `taller` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `franquicia` (
  `idFranquicia` int(3) NOT NULL,
  `nombreFranquicia` varchar(50) NOT NULL,
  `ciudad` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `taller` (
  `idTaller` int(3) NOT NULL,
  `nombreTaller` varchar(50) NOT NULL,
  `Franquicia` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `contrato`
  ADD PRIMARY KEY (`idContrato`),
  ADD KEY `contrato_ibfk_1` (`cedulaEmpleado`),
  ADD KEY `contrato_ibfk_2` (`tipoContrato`);

ALTER TABLE `empleado`
  ADD PRIMARY KEY (`cedula`),
  ADD UNIQUE KEY `idEmpleado` (`idEmpleado`),
  ADD KEY `empleado_ibfk_1` (`cargo`),
  ADD KEY `taller` (`taller`);
ALTER TABLE `franquicia`
  ADD PRIMARY KEY (`idFranquicia`),
  ADD KEY `ciudad` (`ciudad`);
ALTER TABLE `taller`
  ADD PRIMARY KEY (`idTaller`),
  ADD KEY `Franquicia` (`Franquicia`);
ALTER TABLE `contrato`
  ADD CONSTRAINT `contrato_ibfk_1` FOREIGN KEY (`cedulaEmpleado`) REFERENCES `empleado` (`cedula`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `contrato_ibfk_2` FOREIGN KEY (`tipoContrato`) REFERENCES `tipocontrato` (`idContrato`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `empleado`
  ADD CONSTRAINT `empleado_ibfk_1` FOREIGN KEY (`cargo`) REFERENCES `cargo` (`idCargo`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `empleado_ibfk_2` FOREIGN KEY (`taller`) REFERENCES `taller` (`idTaller`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `franquicia`
  ADD CONSTRAINT `franquicia_ibfk_1` FOREIGN KEY (`ciudad`) REFERENCES `ciudad` (`idCiudad`);
ALTER TABLE `taller`
  ADD CONSTRAINT `taller_ibfk_1` FOREIGN KEY (`Franquicia`) REFERENCES `franquicia` (`idFranquicia`);

当我在phpmyadmin面板中插入一行时,查询将成功执行,但是如果我在我的项目上执行同样的操作,则会出现上面的错误。
问题是什么?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题