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

时间:2011-05-13 15:50:58

标签: mysql foreign-keys

我有一个包含以下架构的数据库: enter image description here

以下SQL创建相关表:

-- -----------------------------------------------------
-- Table `ninikske_bioldb`.`CodingRegion`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `ninikske_bioldb`.`CodingRegion` (
  `CodRegID` VARCHAR(45) NOT NULL ,
      `CD_Start` INT NOT NULL ,
  `CD_Stop` INT NOT NULL ,
  `ORFs_ORF_ID` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`ORFs_ORF_ID`, `CodRegID`) ,
  INDEX `fk_Exons_ORFs1` (`ORFs_ORF_ID` ASC) ,
  CONSTRAINT `fk_Exons_ORFs1`
    FOREIGN KEY (`ORFs_ORF_ID` )
    REFERENCES `ninikske_bioldb`.`ORFs` (`ORF_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `ninikske_bioldb`.`Experiment`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `ninikske_bioldb`.`Experiment` (
  `Probe_GenomicPos` INT NOT NULL ,
  `SampleName` VARCHAR(45) NOT NULL ,
  `Intensities` FLOAT NOT NULL ,
  `ExperimentName` VARCHAR(45) NOT NULL ,
  `ProbeID` INT NOT NULL ,
  PRIMARY KEY (`Probe_GenomicPos`, `SampleName`, `ProbeID`) )
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `ninikske_bioldb`.`CE`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `ninikske_bioldb`.`CE` (
  `OrfId` VARCHAR(45) NOT NULL ,
  `CodRegId` VARCHAR(45) NOT NULL ,
  `GenPos` INT NOT NULL ,
  `ExpSam` VARCHAR(45) NOT NULL ,
  `ProbeId` INT NOT NULL ,
      PRIMARY KEY (`OrfId`, `CodRegId`, `GenPos`, `ExpSam`, `ProbeId`) ,
  INDEX `fk_CodingRegion_has_Experiment_Experiment1` (`GenPos` ASC, `ExpSam` ASC, `ProbeId` ASC) ,
  INDEX `fk_CodingRegion_has_Experiment_CodingRegion1` (`OrfId` ASC, `CodRegId` ASC) ,
  CONSTRAINT `fk_CodingRegion_has_Experiment_CodingRegion1`
    FOREIGN KEY (`OrfId` , `CodRegId` )
    REFERENCES `ninikske_bioldb`.`CodingRegion` (`ORFs_ORF_ID` , `CodRegID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_CodingRegion_has_Experiment_Experiment1`
    FOREIGN KEY (`GenPos` , `ExpSam` , `ProbeId` )
    REFERENCES `ninikske_bioldb`.`Experiment` (`Probe_GenomicPos` , `SampleName` , `ProbeID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

当我通过Java执行以下INSERT语句时:

String query = "INSERT INTO ninikske_bioldb.CE VALUES('" + cs.getORF_ID().getORF_ID() + "','" + cs.getCodRegID().getCodRegID() + "'," + cs.getExp().getProbePos() + ",'" + cs.getExp().getExpName()+ "', " + cs.getExp().getProbeID() + ");";
Statement stmt = con.createStatement();
stmt.executeUpdate(query);

我收到以下错误:

Cannot add or update a child row: a foreign key constraint fails (`ninikske_bioldb/CE`, CONSTRAINT `fk_CodingRegion_has_Experiment_Experiment1` FOREIGN KEY (`GenPos`, `ExpSam`, `ProbeId`) REFERENCES `Experiment` (`Probe_GenomicPos`, `SampleName`, `ProbeID`) )

我读了几个关于同一错误信息的其他问题,但我似乎无法找到我需要的洞察力。

修改 查询是: INSERT INTO ninikske_bioldb.CE VALUES('AT3G01190.1','cd474',67262,'H20', 1709);

当我检查父表时,我得到以下结果:

mysql> SELECT * FROM CodingRegion WHERE ORFs_ORF_ID = 'AT3G01190.1';
+----------+----------+---------+-------------+
| CodRegID | CD_Start | CD_Stop | ORFs_ORF_ID |
+----------+----------+---------+-------------+
| cd474    |    67243 |   67649 | AT3G01190.1 | 
| cd475    |    67733 |   67892 | AT3G01190.1 | 
| cd476    |    67991 |   68176 | AT3G01190.1 | 
| cd477    |    68272 |   68484 | AT3G01190.1 | 
+----------+----------+---------+-------------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM CodingRegion WHERE CodRegID = 'cd474';     
+----------+----------+---------+-------------+
| CodRegID | CD_Start | CD_Stop | ORFs_ORF_ID |
+----------+----------+---------+-------------+
| cd474    |    67243 |   67649 | AT3G01190.1 | 
+----------+----------+---------+-------------+
1 row in set (0.03 sec)

mysql> SELECT * FROM Experiment WHERE Probe_GenomicPos = '67262';
+------------------+--------------+-------------+----------------+---------+
| Probe_GenomicPos | SampleName   | Intensities | ExperimentName | ProbeID |
+------------------+--------------+-------------+----------------+---------+
|            67262 | 1signalpart1 |     8.94432 | H20            |    1709 | 
+------------------+--------------+-------------+----------------+---------+
1 row in set (0.00 sec)

mysql> SELECT * FROM Experiment WHERE SampleName = 'H20'
Empty set (0.00 sec)


mysql> SELECT * FROM Experiment WHERE ProbeID = '1709';           
+------------------+--------------+-------------+----------------+---------+
| Probe_GenomicPos | SampleName   | Intensities | ExperimentName | ProbeID |
+------------------+--------------+-------------+----------------+---------+
|            67262 | 1signalpart1 |     8.94432 | H20            |    1709 | 
+------------------+--------------+-------------+----------------+---------+
1 row in set (0.00 sec)

提前致谢。

1 个答案:

答案 0 :(得分:7)

失败的约束“fk_CodingRegion_has_Experiment_Experiment1”要求您为这三列(GenPos,ExpSam和ProbeId)输入的值已存在于Probe_GenomicPos,SampleName和,列中的“ninikske_bioldb.Experiment”表中。 ProbeID。 (按此顺序。)

在表ninikske_bioldb.Experiment中查看这三个值。

SELECT * 
FROM ninikske_bioldb.Experiment
WHERE Probe_GenomicPos = ?
  AND SampleName = '?'
  AND ProbeID = ?

有可能

  • 表的主键 实验是错误的(应该包括 ExperimentName而不是 SampleName)或
  • 外键引用错误 (应参考ExperimentName 而不是SampleName)或
  • 函数cs.getExp()。getExpName() 是错误的使用功能。

我认为最后一个是最有可能的。可能应该是getSampleName(),或类似的东西。