利用觸發器同步兩張表的資料
阿新 • • 發佈:2022-02-22
insert 和update
觸發器不會產生new表和old表,所謂new,old只是指 insert 和 delete
對於INSERT語句, 只有NEW是合法的;
對於DELETE語句,只有OLD才合法;
對於UPDATE語句,NEW、OLD可以同時使用。
需求:更新和插入insert_event_process 表,要同步更新和插入insert_event_process_his 表
#利用觸發器,同步onm_event_process的insert操作 DELIMITER || CREATE TRIGGER insert_event_process AFTER INSERT ONonm_event_process FOR EACH ROW BEGIN INSERT INTO onm_event_process_his(`uuid`, `event_id`, `node_id`, `operate`, `message`, `operator_id`, `operator_name`, `receiver`, `start_time`, `end_time`) VALUES (NEW.`uuid`, NEW.`event_id`, NEW.`node_id`, NEW.`operate`, NEW.`message`, NEW.`operator_id`, NEW.`operator_name`, NEW.`receiver`, NEW.`start_time`, NEW.`end_time`);END|| DELIMITER ; #利用觸發器,同步onm_event_process的update操作 DELIMITER || CREATE TRIGGER update_event_process AFTER UPDATE ON onm_event_process FOR EACH ROW BEGIN UPDATE onm_event_process_his SET `uuid` = NEW.`uuid`, `event_id` = NEW.`event_id`, `node_id`= NEW.`node_id`, `operate` = NEW.`operate`, `message` =NEW.`message`, `operator_id` =NEW.`operator_id`, `operator_name` = NEW.`operator_name`, `receiver` = NEW.`receiver`, `start_time` = NEW.`start_time`, `end_time` =NEW.`end_time` WHERE `uuid` = NEW.`uuid`; END|| DELIMITER ; #刪除插入觸犯器 DROP TRIGGER IF EXISTS insert_event_process #刪除更新觸犯器 DROP TRIGGER IF EXISTS insert_event_process