mysql触发器 - 更新值,值=行计数

时间:2013-01-19 05:26:07

标签: mysql triggers

当更新,插入或删除名为leave_taken的表时,我想运行一个mysql触发器来更新表num col leave的值。

如果我将这些行插入leave

INSERT INTO `leave` (`id_leave`, `staff_leave_application_id_staff_leave_application`, `staff_leave_application_staff_id_staff`, `leave_type_id_leave_type`, `date`, `active`, `date_updated`) VALUES
(7, 7, 6, 7, '2013-01-21', 1, '2013-01-18'),
(8, 7, 6, 7, '2013-01-22', 1, '2013-01-18'),
(9, 7, 6, 7, '2013-01-23', 1, '2013-01-18'),
(10, 7, 6, 7, '2013-01-24', 1, '2013-01-18'),
(11, 7, 6, 7, '2013-01-25', 2, '2013-01-18');

然后表leave_taken col num值已更新为5,其中:

staff_leave_application_staff_id_staff = currently updated staff_leave_application_staff_id_staff,
and
leave_type_id_leave_type = currently updated leave_type_id_leave_type

我已将下表列入。

表格

CREATE TABLE IF NOT EXISTS `leave` (
  `id_leave` int(11) NOT NULL AUTO_INCREMENT,
  `staff_leave_application_id_staff_leave_application` int(11) NOT NULL,
  `staff_leave_application_staff_id_staff` int(11) NOT NULL,
  `leave_type_id_leave_type` int(11) NOT NULL,
  `date` date NOT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `date_updated` date NOT NULL,
  PRIMARY KEY (`id_leave`,`staff_leave_application_id_staff_leave_application`,`staff_leave_application_staff_id_staff`),
  KEY `fk_table1_leave_type1` (`leave_type_id_leave_type`),
  KEY `fk_table1_staff_leave_application1` (`staff_leave_application_id_staff_leave_application`,`staff_leave_application_staff_id_staff`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

表休假:

CREATE TABLE IF NOT EXISTS `leave_balance` (
  `id_leave_balance` int(11) NOT NULL AUTO_INCREMENT,
  `staff_id_staff` int(11) NOT NULL,
  `leave_type_id_leave_type` int(11) NOT NULL,
  `balance` int(3) NOT NULL,
  `date_added` date NOT NULL,
  PRIMARY KEY (`id_leave_balance`),
  UNIQUE KEY `id_leave_balance_UNIQUE` (`id_leave_balance`),
  KEY `fk_leave_balance_staff1` (`staff_id_staff`),
  KEY `fk_leave_balance_leave_type1` (`leave_type_id_leave_type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

1 个答案:

答案 0 :(得分:1)

首先,在leave表中的标识列上定义唯一键:

ALTER TABLE leave
  ADD UNIQUE (staff_leave_application_staff_id_staff, leave_type_id_leave_type);

然后继续定义你的触发器:

CREATE TRIGGER my_insert_trigger AFTER INSERT ON leave FOR EACH ROW
  INSERT INTO leave_taken
    (staff_leave_application_staff_id_staff, leave_type_id_leave_type, num)
  VALUES
    (NEW.staff_leave_application_staff_id_staff, NEW.leave_type_id_leave_type, 1)
  ON DUPLICATE KEY UPDATE
    num = num + 1;

CREATE TRIGGER my_delete_trigger AFTER DELETE ON leave FOR EACH ROW
  UPDATE leave_taken
  SET    num = num - 1
  WHERE  staff_leave_application_staff_id_staff = OLD.staff_leave_application_staff_id_staff
     AND leave_type_id_leave_type = OLD.leave_type_id_leave_type;

DELIMITER ;;

CREATE TRIGGER my_update_trigger AFTER UPDATE ON leave FOR EACH ROW
  IF NOT (
      NEW.staff_leave_application_staff_id_staff <=> OLD.staff_leave_application_staff_id_staff
  AND NEW.leave_type_id_leave_type <=> OLD.leave_type_id_leave_type
  ) THEN
    UPDATE leave_taken
    SET    num = num - 1
    WHERE  staff_leave_application_staff_id_staff = OLD.staff_leave_application_staff_id_staff
       AND leave_type_id_leave_type = OLD.leave_type_id_leave_type;
    INSERT INTO leave_taken
      (staff_leave_application_staff_id_staff, leave_type_id_leave_type, num)
    VALUES
      (NEW.staff_leave_application_staff_id_staff, NEW.leave_type_id_leave_type, 1)
    ON DUPLICATE KEY UPDATE
      num = num + 1;
  END IF;;

DELIMITER ;