删除父行

时间:2016-11-30 19:14:53

标签: mysql

试图删除" Examiner" (EX:id 2)在我的架构上,但它不让我这样做。

你能帮我解释一下这个约束对删除的作用吗? 我想当我删除审查员时,它将exercice表上的examinerId设置为null。

DELETE FROM `examiner` WHERE `examiner`.`id` = 2 LIMIT 1;


#1451 - Cannot delete or update a parent row: a foreign key constraint fails
(`codeevaluator`.`exercise`, CONSTRAINT `FK_Exercise_ExaminerId` FOREIGN KEY         
(`examinerId`) REFERENCES `examiner` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

enter image description here

这是使用两个表创建模式。

CREATE TABLE IF NOT EXISTS `codeevaluator`.`Examiner` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  `username` VARCHAR(45) NOT NULL,
  `email` VARCHAR(45) NOT NULL,
  `accountId` BIGINT(20) UNSIGNED NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `accountId_UNIQUE` (`accountId` ASC),
  CONSTRAINT `FQ_Examiner_AccountId`
    FOREIGN KEY (`accountId`)
   REFERENCES `codeevaluator`.`Account` (`id`)
    ON DELETE CASCADE)
ENGINE = InnoDB
 DEFAULT CHARACTER SET = utf8;


-- -----------------------------------------------------
-- Table `codeevaluator`.`Exercise`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `codeevaluator`.`Exercise` ;

CREATE TABLE IF NOT EXISTS `codeevaluator`.`Exercise` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `examinerId` BIGINT(20) UNSIGNED,
  `examId` BIGINT(20) UNSIGNED NOT NULL,
  `examname` VARCHAR(45) NOT NULL,
  `question` TEXT NOT NULL,
  `name` VARCHAR(45) NOT NULL,
  `status` VARCHAR(45) NOT NULL DEFAULT 'O',
  `progress` INT(10) UNSIGNED NOT NULL DEFAULT '0',
  `nsubmissions` INT(10) UNSIGNED NOT NULL DEFAULT '0',
  `weight` INT(10) NOT NULL,
  `commandbuild` TEXT NULL DEFAULT NULL,
  `commandrun` TEXT NULL DEFAULT NULL,
  `path` VARCHAR(100) NULL DEFAULT '/',
  PRIMARY KEY (`id`),
  INDEX `FK_Exercise_ExamId_idx` (`examId` ASC),
  INDEX `FK_Exercise_ExaminerId_idx` (`examinerId` ASC),
  CONSTRAINT `FK_Exercise_ExamId`
    FOREIGN KEY (`examId`)
    REFERENCES `codeevaluator`.`Exam` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_Exercise_ExaminerId`
    FOREIGN KEY (`examinerId`)
    REFERENCES `codeevaluator`.`Examiner` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

1 个答案:

答案 0 :(得分:1)

检查员似乎还有练习。约束确保永远不会有一个没有检查者的练习。可能的解决方案是:

  • 也删除练习。这可以手动完成,也可以通过将DB-schema更改为级联删除来完成。

  • 不要删除审查员。检查标记是否不可见。这需要将标志添加为新列,并更改访问数据的应用程序以尊重标志。