Combate de caso MySQL - gatilho MySQL

Prefácio

Este ambiente é baseado no sistema Centos 7.8 para construir o MySQL-5.7.14 para
construção específica, consulte a construção do ambiente MySQL-5.7.14

Muitas vezes, é necessário realizar uma consulta de junção nas tabelas emp e dept, cada vez que as tabelas são conectadas e a mesma sequência de instruções é gravada. Ao mesmo tempo, como os dados da fila de salários são mais sensíveis, os requisitos externos são não visível.
Desta forma, podemos concluir criando uma visualização


1. O que é um gatilho

Trigger (trigger) é um procedimento armazenado especial, sua execução não é chamada pelo programa, nem é iniciada manualmente, mas sim acionada por eventos.
Quando uma operação (inserir, excluir, atualizar) é realizada em uma tabela, ela será ativada para execução. Os gatilhos costumam ser usados ​​para fortalecer as restrições de integridade de dados e regras de negócios.

Por exemplo, quando as informações de um aluno são adicionadas à tabela de alunos, o número total de alunos deve ser alterado ao mesmo tempo. Portanto, um gatilho pode ser criado para a tabela do aluno, e cada vez que um registro do aluno é adicionado ou excluído, uma operação de cálculo do número total de alunos é realizada para garantir a consistência entre o número total de alunos e o número de registros.

O gatilho (gatilho) é um procedimento armazenado especial, a diferença é que a execução do procedimento armazenado precisa ser chamada pela instrução CALL
e a execução do gatilho não precisa ser chamada pela instrução CALL ou iniciada manualmente, pois Tão longo quantoQuando um evento predefinido ocorre, ele será automaticamente chamado pelo MySQL.
As vantagens do programa de gatilho são as seguintes:

  • A execução do programa de gatilho é automática. Ele será executado imediatamente após fazer as alterações correspondentes nos dados da tabela relacionada ao programa de disparo.
  • O gatilho pode cascatear e modificar outras tabelas por meio de tabelas relacionadas no banco de dados.
  • O programa de gatilho pode implementar verificações e operações mais complexas do que as restrições FOREIGN KEY e as restrições CHECK.

2. Acione o caso real

1. Várias mesas para alunos

Sincronização de informações e novo (adicionar, excluir)
gatilho de criação

Prepare duas tabelas: aluno, aluno_total.
Quando as informações do aluno são adicionadas ou excluídas na tabela do aluno, as estatísticas de aluno_total também são atualizadas

# 创建student
mysql> create table student
    -> (id int unsigned auto_increment primary key,
    -> name varchar(50));
    
# 创建student_total
mysql> create table student_total(total int);

# 两张表中分别插入数据
mysql> insert into student(name) values('z3');
mysql> insert into student_total values(1);

mysql> select * from student;
+----+------+
| id | name |
+----+------+
|  1 | z3   |
+----+------+
1 row in set (0.00 sec)

mysql> select * from student_total;
+-------+
| total |
+-------+
|     1 |
+-------+
1 row in set (0.00 sec)

# 创建学生信息增加 触发器
mysql> \d $$
mysql> create trigger student_insert after insert
    -> on student for each row
    -> begin
    ->   update student_total set total=total+1;
    -> end$$
mysql> \d ;

# 创建学生信息删除 触发器
mysql>  \d $$
mysql>  create trigger student_delete after delete
    ->  on student for each row
    ->  begin
    ->    update student_total set total=total-1;
    ->  end$$
mysql> \d ;

# 增加学生时
mysql> insert into student(name)
    -> values('l4');
mysql> select * from student;
+----+------+
| id | name |
+----+------+
|  1 | z3   |
|  2 | l4   |
+----+------+
2 rows in set (0.00 sec)

mysql> select * from student_total;
+-------+
| total |
+-------+
|     2 |
+-------+
1 row in set (0.00 sec)

# 删除学生时
mysql> delete from student
    -> where id=1;
mysql> select * from student;
+----+------+
| id | name |
+----+------+
|  2 | l4   |
+----+------+
1 row in set (0.00 sec)

mysql> select * from student_total;
+-------+
| total |
+-------+
|     1 |
+-------+
1 row in set (0.00 sec)

Ver o gatilho criado

# 查看创建的触发器
mysql> show triggers\G
*************************** 1. row ***************************
             Trigger: student_insert
               Event: INSERT
               Table: student
           Statement: begin
  update student_total set total=total+1;
end
              Timing: AFTER
             Created: 2021-02-03 15:05:38.49
            sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
             Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8_general_ci
*************************** 2. row ***************************
             Trigger: student_delete
               Event: DELETE
               Table: student
           Statement: begin
   update student_total set total=total-1;
 end
              Timing: AFTER
             Created: 2021-02-03 15:21:11.06
            sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
             Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8_general_ci
2 rows in set (0.00 sec)


mysql> select * from information_schema.triggers where TRIGGER_NAME='student_insert' or TRIGGER_NAME='student_delete'\G
*************************** 1. row ***************************
           TRIGGER_CATALOG: def
            TRIGGER_SCHEMA: db
              TRIGGER_NAME: student_insert
        EVENT_MANIPULATION: INSERT
      EVENT_OBJECT_CATALOG: def
       EVENT_OBJECT_SCHEMA: db
        EVENT_OBJECT_TABLE: student
              ACTION_ORDER: 1
          ACTION_CONDITION: NULL
          ACTION_STATEMENT: begin
  update student_total set total=total+1;
end
        ACTION_ORIENTATION: ROW
             ACTION_TIMING: AFTER
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
  ACTION_REFERENCE_OLD_ROW: OLD
  ACTION_REFERENCE_NEW_ROW: NEW
                   CREATED: 2021-02-03 15:05:38.49
                  SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
                   DEFINER: root@localhost
      CHARACTER_SET_CLIENT: utf8
      COLLATION_CONNECTION: utf8_general_ci
        DATABASE_COLLATION: utf8_general_ci
*************************** 2. row ***************************
           TRIGGER_CATALOG: def
            TRIGGER_SCHEMA: db
              TRIGGER_NAME: student_delete
        EVENT_MANIPULATION: DELETE
      EVENT_OBJECT_CATALOG: def
       EVENT_OBJECT_SCHEMA: db
        EVENT_OBJECT_TABLE: student
              ACTION_ORDER: 1
          ACTION_CONDITION: NULL
          ACTION_STATEMENT: begin
   update student_total set total=total-1;
 end
        ACTION_ORIENTATION: ROW
             ACTION_TIMING: AFTER
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
  ACTION_REFERENCE_OLD_ROW: OLD
  ACTION_REFERENCE_NEW_ROW: NEW
                   CREATED: 2021-02-03 15:21:11.06
                  SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
                   DEFINER: root@localhost
      CHARACTER_SET_CLIENT: utf8
      COLLATION_CONNECTION: utf8_general_ci
        DATABASE_COLLATION: utf8_general_ci
2 rows in set (0.00 sec)

Excluir gatilho

mysql> drop trigger student_insert;
Query OK, 0 rows affected (0.00 sec)

mysql> drop trigger student_delete;
Query OK, 0 rows affected (0.00 sec)

mysql> show triggers\G
Empty set (0.00 sec)

2. Atualização síncrona de informações de várias tabelas (adicionar, atualizar, excluir)

# 创建tb1、tb2表
mysql> create table tb1
    -> (id int primary key auto_increment,
    -> name varchar(50),
    -> gender enum('男','女') default '男',
    -> age int);

mysql> create table tb2
    -> (id int primary key auto_increment,
    -> name varchar(50),
    -> salary double(10,2)
    -> );


# 分别创建插入、更新、删除的触发器
mysql> create trigger tb1_after_insert
    -> after insert
    -> on tb1 for each row
    -> begin
    ->   insert into tb2(name,salary) values(new.name,8000);
    -> end$$

mysql> create trigger tb1_after_update
    -> after update
    -> on tb1 for each row
    -> begin
    ->   update tb2 set name=new.name where name=old.name;
    -> end$$

mysql> create trigger tb1_after_delete
    -> after delete 
    -> on tb1 for each row
    -> begin
    ->   delete from tb2 where name=old.name;
    -> end$$

#

# 测试
---tb1插入三条记录
mysql> insert into tb1(name,age) values('张三','16');
mysql> insert into tb1(name,age) values('李四','22');
mysql> insert into tb1(name,age) values('王五','32');

---tb1、tb2均插入信息
mysql> select * from tb1;
+----+--------+--------+------+
| id | name   | gender | age  |
+----+--------+--------+------+
|  1 | 张三   ||   16 |
|  2 | 李四   ||   22 |
|  3 | 王五   ||   32 |
+----+--------+--------+------+
3 rows in set (0.00 sec)

mysql> select * from tb2;
+----+--------+---------+
| id | name   | salary  |
+----+--------+---------+
|  1 | 张三   | 8000.00 |
|  2 | 李四   | 8000.00 |
|  3 | 王五   | 8000.00 |
+----+--------+---------+
3 rows in set (0.00 sec)

# 修改id=2的学生name
mysql> update tb1
    -> set name='老六'
    -> where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from tb1;
+----+--------+--------+------+
| id | name   | gender | age  |
+----+--------+--------+------+
|  1 | 张三   ||   16 |
|  2 | 老六   ||   22 |
|  3 | 王五   ||   32 |
+----+--------+--------+------+
3 rows in set (0.00 sec)

mysql> select * from tb2;
+----+--------+---------+
| id | name   | salary  |
+----+--------+---------+
|  1 | 张三   | 8000.00 |
|  2 | 老六   | 8000.00 |
|  3 | 王五   | 8000.00 |
+----+--------+---------+
3 rows in set (0.00 sec)

mysql> delete from tb1
    -> where id=3;
Query OK, 1 row affected (0.00 sec)

mysql> select * from tb1;
+----+--------+--------+------+
| id | name   | gender | age  |
+----+--------+--------+------+
|  1 | 张三   ||   16 |
|  2 | 老六   ||   22 |
+----+--------+--------+------+
2 rows in set (0.01 sec)

mysql> select * from tb2;
+----+--------+---------+
| id | name   | salary  |
+----+--------+---------+
|  1 | 张三   | 8000.00 |
|  2 | 老六   | 8000.00 |
+----+--------+---------+
2 rows in set (0.00 sec)

Acho que você gosta

Origin blog.csdn.net/XY0918ZWQ/article/details/113606122
Recomendado
Clasificación