外键约束错误

时间:2017-12-09 20:35:52

标签: mysql

当我创建我的桌子时,我一直得到一个ERROR1215。我已将问题缩小到两个属性"得分"和" criteriaNum"。我无法弄清楚为什么会造成问题。

CREATE TABLE IF NOT EXISTS `sys`.`rubric` (
  `programName` VARCHAR(100) NOT NULL,
  `criteriaNum` INT UNSIGNED NOT NULL,
  `score` INT UNSIGNED NOT NULL,
  `criteria` TINYTEXT NOT NULL,
  `description` TINYTEXT NOT NULL,
  PRIMARY KEY (`programName`, `criteriaNum`,`score`),
  CONSTRAINT `rubric_programName`
    FOREIGN KEY (`programName`)
    REFERENCES `sys`.`degree_program` (`programName`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
    COMMENT 'Each entry in the rubric table corresponds to a criteria,score pair.';

CREATE TABLE IF NOT EXISTS`sys`.`evaluator_scores` (
  `evaluatorName` VARCHAR(100) NOT NULL,
  `aid` INT NOT NULL,
  `programName` VARCHAR(100) NOT NULL,
  `criteriaNum` INT UNSIGNED NOT NULL,
  `score` INT UNSIGNED NOT NULL,
  PRIMARY KEY (`aid`, `evaluatorName`, `programName`, `criteriaNum`, `score`),
  CONSTRAINT `score_aid`
    FOREIGN KEY (`aid`)
    REFERENCES `sys`.`application` (`aid`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `score_evalName`
    FOREIGN KEY (`evaluatorName`)
    REFERENCES `sys`.`evaluations` (`evaluatorName`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `score_programName`
    FOREIGN KEY (`programName`)
    REFERENCES `sys`.`rubric` (`programName`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `score_criteriaNum`
    FOREIGN KEY (`criteriaNum`)
    REFERENCES `sys`.`rubric` (`criteriaNum`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `score_score`
    FOREIGN KEY (`score`)
    REFERENCES `sys`.`rubric` (`score`)
    ON DELETE CASCADE
    ON UPDATE CASCADE);

如果我删除了score和criteriaNum约束,它会构建evaluateator_scores。否则我会收到错误。有谁能找到问题?

1 个答案:

答案 0 :(得分:0)

外键的概念是父表必须具有相应的记录才能在子项中包含该键。在父表中,主键是(programName,criteriaNum,score)。但是在子表中,您尝试为rubric表创建三个外键约束,每个列对应一个列。

如果将这三个约束组合成一个并匹配主键列,它应该可以工作。