1.修改数据的触发器
修改数据的触发器
create trigger tb_user_update_trigger
after update on tb_user for each row
begin
insert int user_logs(id,operation,operate_time,operate_id,operate_params)VALUES
(null,'update',now(),new.id,concat
('更新之前的数据:id=',new.id,',name=',new.name,', phone=',new.phone,', email=',new.email,', profession=',new.profession
' | 更新之后的数据:id=',new.id,',name=',new.name,', phone=',new.phone,', email=',new.email,', profession=',new.profession));
end;
show triggers;
更新数据
update tb_user set age = 32 where id = 23;
思考下面这个触发器会触发几次?几条数据就触发几次。
update tb_user set age = 32 where id <= 5;
2.删除数据的触发器
create trigger tb_user_delete_trigger
after delete on tb_user for each row
begin
insert int user_logs(id,operation,operate_time,operate_id,operate_params)VALUES
(null,'delete',now(),old.id,concat
('删除之前的数据:id=',new.id,',name=',new.name,', phone=',new.phone,', email=',new.email,', profession=',new.profession);
end;
show triggers;
delete from tb_user where id = 5;