使用触发器更新表

时间:2017-08-30 14:47:27

标签: mysql triggers

我有两个与programformation相关的表格。以下是脚本:

CREATE TABLE IF NOT EXISTS `camstaf`.`program` (
  `id_program` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `terms` TEXT NULL,
  `objectives` TEXT NULL,
  `type` VARCHAR(45) NULL DEFAULT 'type: séminaire ou certification',
  `period_min` DATE NULL,
  `period_max` DATE NULL,
  `duration` VARCHAR(45) NULL,
  `budget` TEXT NULL,
  `precision_budget` TEXT NULL,
  `achieved` TINYINT(1) UNSIGNED NULL,
  PRIMARY KEY (`id_program`))
ENGINE = InnoDB ;

CREATE TABLE IF NOT EXISTS `camstaf`.`formation` (
  `id_formation` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `terms` TEXT NULL,
  `objectifs` TEXT CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL,
  `type` VARCHAR(45) NULL,
  `date_start` DATE NULL,
  `date_end` DATE NULL,
  `direct_costs` INT UNSIGNED NULL,
  `ind_costs` INT UNSIGNED NULL,
  `precision_couts` TEXT NULL,
  `scheduled_training` INT UNSIGNED NULL,
  `status` TINYINT(1) NULL,
  PRIMARY KEY (`id_formation`),
  INDEX `fk_fk_formation_match_program_idx` (`scheduled_training` ASC),
  CONSTRAINT `fk_fk_formation_match_program`
    FOREIGN KEY (`scheduled_training`)
    REFERENCES `camstaf`.`program` (`id_program`)
    ON DELETE SET NULL
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8

我希望使用此触发器更新表program

CREATE DEFINER = CURRENT_USER TRIGGER `camstaf`.`formation_AFTER_INSERT` AFTER INSERT ON `formation` FOR EACH ROW
BEGIN
    IF NEW.scheduled_training != NULL THEN
        UPDATE program
        SET achieved = TRUE
        WHERE id_program = NEW.scheduled_training AND achieved = FALSE;
    END IF;
END

但它不起作用。我无法找到错误。有什么想法吗?!

0 个答案:

没有答案