MySQL log trigger implementation code

MySQL log trigger implementation code

SQL statement

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":"', '"' )
),
"}"
)
);

MySQL uses triggers to implement logging

CREATE TRIGGER news_log AFTER INSERT ON news;

FOR EACH ROW INSERT INTO `news_logs` select *, now() from news where newsid = (select max(newsid ) from news);

DROP TRIGGER news_log;

There are two tables in total, one is the original table news, and the other is the log table news_logs (with an additional date column), which can automatically record logs when new entries are inserted without writing code. Similarly, you can also insert update logs when updating. For details, see the MySQL documentation.

mysql uses triggers to record operations

MySQL cannot perform insert, update, and delete operations in one trigger. You need to create three triggers separately.

Insert trigger:

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

Update trigger:

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

Delete trigger:

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

It is a bit troublesome to create three triggers for one table. I wonder if there is a better way.

You may also be interested in:
  • Detailed explanation of mysql trigger example
  • A brief summary and examples of MySQL triggers
  • An article to give you a deep understanding of Mysql triggers
  • Use of MySQL triggers
  • MySQL trigger trigger add, delete, modify and query operation example
  • Use of MySQL trigger

<<:  Basic ideas and codes for implementing video players in browsers

>>:  javascript to switch pictures by clicking a button

Recommend

Implementation of MySQL asc and desc data sorting

Data sorting asc, desc 1. Single field sorting or...

Html makes a simple and beautiful login page

Let’s take a look first. HTML source code: XML/HT...

React passes parameters in several ways

Table of contents Passing parameters between pare...

In-depth understanding of the role of Vuex

Table of contents Overview How to share data betw...

How to use docker to deploy dubbo project

1. First, use springboot to build a simple dubbo ...

A brief analysis of Vue's asynchronous update of DOM

Table of contents The principle of Vue asynchrono...

How to use DCL to manage users and control permissions in MySQL

DCL (Data Control Language): Data control languag...

Sharing some wonderful uses of wxs files in WeChat applet

Table of contents Preface application Filters Dra...

Analysis of a MySQL deadlock scenario example

Preface Recently I encountered a deadlock problem...

MySQL Flush-List and dirty page flushing mechanism

1. Review The Buffer Pool will be initialized aft...