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

Solution to the failure of loading dynamic library when Linux program is running

Unable to load dynamic library under Linux When t...

What is the function and writing order of the a tag pseudo class

The role of the a tag pseudo-class: ":link&qu...

Summary of several principles that should be followed in HTML page output

1. DOCTYPE is indispensable. The browser determin...

MySQL uses binlog logs to implement data recovery

MySQL binlog is a very important log in MySQL log...

HTML exceeds the text line interception implementation principle and code

The HTML code for intercepting text beyond multipl...

Linux uses suid vim.basic file to achieve privilege escalation

Reproduce on Kali First set suid permissions for ...

Implementation of IP address configuration in Centos7.5

1. Before configuring the IP address, first use i...

The difference and choice between datetime and timestamp in MySQL

Table of contents 1 Difference 1.1 Space Occupanc...

Detailed explanation of non-parent-child component value transfer in Vue3

Table of contents App.vue sub1.vue sub2.vue Summa...

Three ways to implement waterfall flow layout

Preface When I was browsing Xianyu today, I notic...

Usage of HTML H title tag

The usage of H tags, especially h1, has always bee...

MySQL backup table operation based on Java

The core is mysqldump and Runtime The operation i...

The whole process of installing mysql5.7.22 under ARM64 architecture

MySQL download address: https://obs.cn-north-4.my...