MYSQL使用触发器实现合计

delimiter $$

create
    /*[DEFINER = { user | CURRENT_USER }]*/
    trigger `jianchayuan_jixiao`.`yrkpi_before_insert` before insert
    on `jianchayuan_jixiao`.`yrkpi`
    for each row begin
		set new.kpi_deptscore=new.kpi_deptscore1+new.kpi_deptscore2+new.kpi_deptscore3+new.kpi_deptscore4+new.kpi_deptscore5;
    end$$

delimiter ;
delimiter $$

use `jianchayuan_jixiao`$$

drop trigger /*!50032 IF EXISTS */ `yrkpi_before_update`$$

create
    /*!50017 DEFINER = 'root'@'localhost' */
    trigger `yrkpi_before_update` before update on `yrkpi` 
    for each row begin
		update `yrkpi`
		set kpi_deptscore=new.kpi_deptscore1+new.kpi_deptscore2+new.kpi_deptscore3+new.kpi_deptscore4+new.kpi_deptscore5;
    end;
$$

delimiter ;

你可能感兴趣的:(MYSQL)