DROP TRIGGER
IF
EXISTS sys_menu_edit;
CREATE TRIGGER sys_menu_edit BEFORE UPDATE ON sys_menu FOR EACH ROW
BEGIN
INSERT INTO `g4m`.`sys_log` ( `table_name`, `val_id`, `data_json` )
VALUES
(
'sys_menu',
old.id,
CONCAT(
"{",
CONCAT_WS(
',',
CONCAT_WS( old.id, '"id":"', '"' ),
CONCAT_WS( old.CODE, '"code":"', '"' ),
CONCAT_WS( old.type_dic, '"type_dic":"', '"' ),
CONCAT_WS( old.NAME, '"name":"', '"' ),
CONCAT_WS( old.pid, '"pid":"', '"' ),
CONCAT_WS( old.status_dic, '"status_dic":"', '"' ),
CONCAT_WS( old.url, '"url":"', '"' ),
CONCAT_WS( old.path, '"path":"', '"' ),
CONCAT_WS( old.icon, '"icon":"', '"' ),
CONCAT_WS( old.sort, '"sort":"', '"' ),
CONCAT_WS( old.remark, '"remark":"', '"' ),
CONCAT_WS( old.create_time, '"create_time":"', '"' ),
CONCAT_WS( old.modify_uer_id, '"modify_uer_id":"', '"' ),
CONCAT_WS( old.modify_time, '"modify_time":"', '"' )
),
"}"
)
);
一共兩個表,一個是原始表 news,一個是日志表news_logs(多了一列日期),可以實現(xiàn)新插入時自動記錄日志,不用寫代碼了。 類似還可以實現(xiàn)更新時插入更新日志,詳情可查看 MySQL 的文檔。
delimiter $$
create trigger tri_city_insert
after insert
on t_xfw_city
for each row
begin
insert into t_tri_city(id,name,provinceid,ctype)
values(new.id,new.name,new.provinceid,1);
end
delimiter $$
create trigger tri_city_update
after update
on t_xfw_city
for each row
begin
insert into t_tri_city(id,name,provinceid,ctype)
values(new.id,new.name,new.provinceid,2);
end
delimiter $$
create trigger tri_city_delete
after delete
on t_xfw_city
for each row
begin
insert into t_tri_city(id,name,provinceid,ctype)
values(old.id,old.name,old.provinceid,3);
end