1

I have this table:

CREATE TABLE `user` (
  `idUser` char(13) NOT NULL,
  `contrasena` varchar(50) NOT NULL DEFAULT '',
  `fechaInicio` datetime DEFAULT NULL,
  `emailRegistrado` varchar(100) DEFAULT NULL,
  `tipoUsuario` int(11) NOT NULL,

  PRIMARY KEY (`idUser`),
  KEY `tipoUser` (`tipoUsuario`) USING BTREE,
  CONSTRAINT `tipoUser` FOREIGN KEY (`tipoUsuario`) REFERENCES `tipo_user` (`idTipo`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf16;

And then this table:

CREATE TABLE `alumno` (
  `idAlumno` char(10) NOT NULL DEFAULT '',
  `Nombre` varchar(60) NOT NULL,
  `ApePaterno` varchar(30) NOT NULL,
  `ApeMaterno` varchar(30) NOT NULL,
  `CURP` varchar(18) DEFAULT NULL,
  `Sexo` enum('H','M') NOT NULL,
  `FechaNac` date NOT NULL,
  `Estado_Nac` int(11) DEFAULT NULL,
  `Nacionalidad` int(11) DEFAULT NULL,

  PRIMARY KEY (`idAlumno`),
  KEY `fk_Alumno_Estados1_idx` (`Estado_Nac`) USING BTREE,
  KEY `fk_Alumno_Pais1_idx` (`Nacionalidad`) USING BTREE,
  CONSTRAINT `fk_Alumno_Estados1` FOREIGN KEY (`Estado_Nac`) REFERENCES `estadomexico` (`idEstados`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_Alumno_Pais1` FOREIGN KEY (`Nacionalidad`) REFERENCES `pais` (`idPais`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_Al_User` FOREIGN KEY (`idAlumno`) REFERENCES `user` (`idUser`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf16;

When I try to update a value for a user, MySQL throws the following message:

Cannot delete or update a parent row: a foreign key constraint fails (mydb.empleado, CONSTRAINT fk_Empleado_USer FOREIGN KEY (idEmpleado) REFERENCES user (idUser) ON DELETE CASCADE ON UPDATE CASCADE)

Can anybody please help me?

peterm
  • 91,357
  • 15
  • 148
  • 157
LockonF
  • 11
  • 3
  • Refer this it may helps to you http://stackoverflow.com/questions/3334619/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails – Roopendra Nov 15 '13 at 05:35
  • Yeah, I tried dumping the whole server as per suggestion. The result was the opposite, now it doesn't delete anything in spite of having the foreign keys set. Problem is, it should delete and I'm thinking it's because I'm using a PK from one table as FK from another. – LockonF Nov 25 '13 at 23:07
  • I worked the problem around by updating the database adding an index to the key that referenced the PK from the main table – LockonF Nov 27 '13 at 04:29

0 Answers0