mysql触发器实时检测一条语句进行备份删除思路详解

问题描述:用户有一个这样一个需求,在一张表里会不时出现 “违规” 字样的字段,需要在出现这个字段的时候,把整行的数据删掉。这是个采集任务,如果发现有“违规”字样的数据,会整点或者什么时间进行统一上报,也无法对源头进行控制让这种数据不生成。

现在需要实现以下需求:

1.实时检测这条数据的产生,发现后删除

2.在删除之前作备份这条数据

解决思路:

需要明确解决思路,

1.首先是如何实时探测删除?询问开发,这条数据的生成方式为insert,就可以做一个当表做插入的时候,然后做一个after insert 做delete数据的触发器

2.如何进行备份?何种方式备份?能不能备份到一个表里,这个表里记录每次插入的时间,建立这个备份表可以取与原表结构基本相同,但是备份表要删除原表的自增属性,主键,外键等属性,新增一个时间戳字段,方便记录每次备份数据的时间,删除以上属性是为了能够把数据写入备份表中

3.如何在删除之前做备份呢?一开始想的是放到一个触发器就行,先把数据进行备份,然后后面跟着一条删除,测试的时候行不通

测试方案:

先准备一些测试数据和测试表

1.建立测试数据

mysql> show create table student;
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| table   | create table                                                                                                                                                                                                                                                             |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | create table `student` (
  `sno` char(9) not null,
  `sname` char(20) not null,
  `ssex` char(2) default null,
  `sage` smallint default null,
  `sdept` char(20) default null,
  primary key (`sno`)
) engine=innodb default charset=utf8mb4 collate=utf8mb4_0900_ai_ci |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

2.建立备份表。查看建表语句,正式环境是不知道原表的表结构的,需要有改原表结构,并创建新备份表的操作的

原表建表语句

mysql> show create table student;
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| table   | create table                                                                                                                                                                                                                                                             |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | create table `student` (
  `sno` char(9) not null,
  `sname` char(20) not null,
  `ssex` char(2) default null,
  `sage` smallint default null,
  `sdept` char(20) default null,
  primary key (`sno`)
) engine=innodb default charset=utf8mb4 collate=utf8mb4_0900_ai_ci |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

3.准备备份语句,删除语句,插入测试语句

备份语句(因为备份表多一个时间戳的字段,所以备份语句要做修改一下)

mysql> show create table student_bak;
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| table | create table |
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student_bak | create table `student_bak` (
`sno` char(9) not null,
`sname` char(20) not null,
`ssex` char(2) default null,
`sage` smallint default null,
`sdept` char(20) default null,
`create_date` timestamp not null default current_timestamp
) engine=innodb default charset=utf8mb4 collate=utf8mb4_0900_ai_ci |
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

备份效果

插入测试语句:

insert into student values('201215124','张三','男',20,'el');

删除语句(删除数据一定要写精准了,到时候删什么,就要备份什么)

delete from student where sdept='el';

4.实际测试方案

4.1 把两条语句写进一个触发器(操作失败,逻辑执行不成功)

drop trigger if exists test_trigger;
delimiter $
create trigger test_trigger
after
insert on student2
for each row
begin
insert into student_bak(sno,sname,ssex,sage,sdept) select * from student where sdept='el';
delete from student where sdept='el';
end $
delimiter ;

4.2 准备两个单独的触发器,一个是当原表出现这条数据时,插入到备份表实现备份的效果。第二个触发器是做备份表备份完数据之后,在做一个删除原表该条数据的触发器,然后实现备份完删除的效果(还是操作失败),执行报错,说的是触发器有冲突还是什么的,这样做让数据库不知道执行逻辑了。

4.3 做一个在原表如果进行删除目标数据,然后备份该条数据到备份表的触发器。最后再实现实时探测目标数据出现然后删除的操作就行了,不局限于触发器的思维,做一个定时任务就可以了(操作成功)

比如下面测试的当数据库的表中sdept字段出现了一个叫‘el’的字段时,需要把整行数据删除掉

drop trigger if exists student_bak_trigger;
delimiter $
create trigger student_bak_trigger 
before 
delete on student 
for each row 
begin   
insert into  student_bak(sno,sname,ssex,sage,sdept) select * from student where sdept='el';
end $
delimiter ;

这个触发器就实现了,如果原表的目标数据被删除了,触发器触发了就会备份该条数据

mysql> select * from student;
+-----------+--------+------+------+-------+
| sno       | sname  | ssex | sage | sdept |
+-----------+--------+------+------+-------+
| 201215121 | 李勇   | 男   |   20 | cs    |
| 201215122 | 刘晨   | 女   |   19 | cs    |
| 201215123 | 王敏   | 女   |   18 | ma    |
| 201215130 | 兵丁   | 男   |   20 | ch    |
+-----------+--------+------+------+-------+
4 rows in set (0.00 sec)

mysql> select * from student_bak;
+-----------+--------+------+------+-------+---------------------+
| sno       | sname  | ssex | sage | sdept | create_date         |
+-----------+--------+------+------+-------+---------------------+
| 201215124 | 张三   | 男   |   20 | el    | 2021-09-18 15:42:20 |
+-----------+--------+------+------+-------+---------------------+
1 row in set (0.00 sec)

mysql> insert into student values('201215125','王五','男',30,'el');
query ok, 1 row affected (0.00 sec)

mysql> select * from student;
+-----------+--------+------+------+-------+
| sno       | sname  | ssex | sage | sdept |
+-----------+--------+------+------+-------+
| 201215121 | 李勇   | 男   |   20 | cs    |
| 201215122 | 刘晨   | 女   |   19 | cs    |
| 201215123 | 王敏   | 女   |   18 | ma    |
| 201215125 | 王五   | 男   |   30 | el    |
| 201215130 | 兵丁   | 男   |   20 | ch    |
+-----------+--------+------+------+-------+
5 rows in set (0.00 sec)

mysql> select * from student_bak;
+-----------+--------+------+------+-------+---------------------+
| sno       | sname  | ssex | sage | sdept | create_date         |
+-----------+--------+------+------+-------+---------------------+
| 201215124 | 张三   | 男   |   20 | el    | 2021-09-18 15:42:20 |
+-----------+--------+------+------+-------+---------------------+
1 row in set (0.00 sec)

mysql> delete from student where sdept='el';
query ok, 1 row affected (0.01 sec)

mysql> select * from student_bak;
+-----------+--------+------+------+-------+---------------------+
| sno       | sname  | ssex | sage | sdept | create_date         |
+-----------+--------+------+------+-------+---------------------+
| 201215124 | 张三   | 男   |   20 | el    | 2021-09-18 15:42:20 |
| 201215125 | 王五   | 男   |   30 | el    | 2021-09-18 15:47:28 |
+-----------+--------+------+------+-------+---------------------+
2 rows in set (0.00 sec)

最后实现一个定时任务,来循环的删除一个叫‘el’的字段的整行数据,这里的定时任务是面向全局的,一定要加上数据库名和具体表名,定时任务的执行速度是可以手动调整的,下面为3s/次,以便实现需要的效果

create event if not exists e_test_event
on schedule every 3 second 
on completion preserve
do delete from abc.student where sdept='el'; 

关闭定时任务:

mysql> alter event e_test_event on completion preserve disable;
query ok, 0 rows affected (0.00 sec)

查看定时任务:

mysql> select * from information_schema.events\g;*************************** 4. row ***************************
       event_catalog: def
        event_schema: abc
          event_name: e_test_event
             definer: root@%
           time_zone: system
          event_body: sql
    event_definition: delete from abc.student where sdept='el'
          event_type: recurring
          execute_at: null
      interval_value: 3
      interval_field: second
            sql_mode: only_full_group_by,strict_trans_tables,no_zero_in_date,no_zero_date,error_for_division_by_zero,no_engine_substitution
              starts: 2021-09-17 13:35:44
                ends: null
              status: enabled
       on_completion: preserve
             created: 2021-09-17 13:35:44
        last_altered: 2021-09-17 13:35:44
       last_executed: 2021-09-18 15:43:35
       event_comment: 
          originator: 3330614
character_set_client: utf8mb4
collation_connection: utf8mb4_0900_ai_ci
  database_collation: utf8mb4_0900_ai_ci
4 rows in set (0.00 sec)

查看触发器:

mysql> select * from information_schema.triggers\g;
*************************** 5. row ***************************
           trigger_catalog: def
            trigger_schema: abc
              trigger_name: student_bak_trigger
        event_manipulation: delete
      event_object_catalog: def
       event_object_schema: abc
        event_object_table: student
              action_order: 1
          action_condition: null
          action_statement: begin   
insert into  student_bak(sno,sname,ssex,sage,sdept) select * from student where sdept='el';
end
        action_orientation: row
             action_timing: before
action_reference_old_table: null
action_reference_new_table: null
  action_reference_old_row: old
  action_reference_new_row: new
                   created: 2021-09-18 15:41:48.53
                  sql_mode: only_full_group_by,strict_trans_tables,no_zero_in_date,no_zero_date,error_for_division_by_zero,no_engine_substitution
                   definer: root@%
      character_set_client: utf8mb4
      collation_connection: utf8mb4_0900_ai_ci
        database_collation: utf8mb4_0900_ai_ci
5 rows in set (0.00 sec)

实现效果:

到此这篇关于mysql触发器实时检测一条语句进行备份删除思路详解的文章就介绍到这了,更多相关mysql触发器备份删除内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐