mysql trigger creation and usage examples

mysql trigger creation and usage examples

This article describes how to create and use MySQL triggers. Share with you for your reference, the details are as follows:

What is a trigger

  • Triggers are used to "automatically" perform some actions after/before certain actions. (For example, if new student information is inserted, the number of students should be modified in the class table).
  • After the insert delete update trigger is set, executing the insert delete update operation will automatically trigger the set content.
  • A table can have a maximum of 6 triggers (3*2, three operations*two times (before|after)).

Create a trigger

grammar:

create trigger trigger name before|after event on table name for each row trigger statement;

  • The recommended trigger name is trigger_xxx, which is easy to distinguish. Trigger names cannot be repeated .
  • before|after represents the execution time of the trigger statement. If it is before, the trigger statement is executed before the insert, delete, or update operation; after means after.
  • An event is one of the insert, delete, and update operations.
  • for each row is a trigger that will execute the corresponding operation on any record.
  • A trigger statement is a statement that is executed when a trigger is fired.

Explanation of keywords in MySQL creation syntax:

Fields meaning Possible values
DEFINER= Optional parameter, specifies the creator, the default is the currently logged in user (CURRENT_USER);
The trigger will be executed by the user specified by this parameter, so permissions need to be considered;
DEFINER='root@%'
DEFINER=CURRENT_USER
trigger_name The trigger name should preferably consist of the table name + trigger event keyword + trigger time keyword;
trigger_time The trigger time, before or after a certain event; BEFORE、AFTER
trigger_event Trigger events, such as triggering when inserting or deleting;
INSERT : Insert operation trigger, triggered when INSERT, LOAD DATA, REPLACE;
UPDATE : Update operation trigger, triggered when an UPDATE operation occurs;
DELETE : Delete operation trigger, triggered by DELETE and REPLACE operations;
INSERT, UPDATE, DELETE
table_name The table name of the trigger operation time;
trigger_order Optional parameter, if multiple triggers with the same trigger event and trigger time are defined (
For example: BEFORE UPDATE), the default triggering order is consistent with the order in which the triggers are created. You can use this parameter to change their triggering order. This parameter is supported since MySQL 5.7.2.
FOLLOWS : The currently created trigger is activated after the existing triggers;
PRECEDES : The currently created trigger is activated before the existing triggers;
FOLLOWS、PRECEDES
trigger_body The content of the SQL statement that triggers execution, usually starts with begin and ends with end begin .. end

Mysql trigger usage example:

1. MySQL trigger Insert triggers update of the same table:

Below we have a table " tmp1 ", tmp1 table has two integer fields: n1, n2. We need to use triggers to automatically set the value of the n2 field to 5 times the value of the n1 field when inserting records into tmp.
Create a test table and trigger:

-- Create a test table drop table if exists tmp1;
 
create table tmp1 (n1 int, n2 int);
 
-- Create trigger DELIMITER $
 
drop trigger if exists tmp1_insert$
 
create trigger tmp1_insert
 
before insert on tmp1
 
for each row
 
begin
 
  set new.n2 = new.n1*5;
 
end$
 
DELIMITER ;

Test trigger update effect:

mysql> insert tmp1(n1) values(18);
 
Query OK, 1 row affected (0.01 sec)
 
mysql> insert tmp1(n1) values(99);
 
Query OK, 1 row affected (0.00 sec)
 
mysql> select * from tmp1;
 
+------+------+
 
| n1 | n2 |
 
+------+------+
 
| 18 | 90 |
 
| 99 | 495 |
 
+------+------+
 
2 rows in set (0.00 sec)

2. MySQL trigger Update triggers to update another table:

Below are two tables tmp1 and tmp2, both of which have the same field name . When a trigger is used to update the name of a table, the name of another table will also be updated.
Create a test table and trigger:

-- Create a test table and insert test data drop table if exists tmp1;
 
drop table if exists tmp2;
 
create table tmp1 (id int, name varchar(128)) default charset='utf8';
 
create table tmp2 (fid int, name varchar(128)) default charset='utf8';
 
insert into tmp1 values(1, '爱E族');
 
insert into tmp2 values(1, '爱E族');
 
-- Create trigger DELIMITER $
 
drop trigger if exists tmp1_update$
 
create trigger tmp1_update
 
after update on tmp1
 
for each row
 
begin
 
  update tmp2 set name=new.name where fid=new.id;
 
end$
 
DELIMITER ;

Test trigger update effect:

mysql> select * from tmp1;
 
+------+---------+
 
| id | name |
 
+------+---------+
 
| 1 | Love E tribe |
 
+------+---------+
 
1 row in set (0.00 sec)
 
mysql> select * from tmp2;
 
+------+---------+
 
| fid | name |
 
+------+---------+
 
| 1 | Love E tribe |
 
+------+---------+
 
1 row in set (0.00 sec)
 
mysql> update tmp1 set name='aiezu.com' where id=1;
 
Query OK, 1 row affected (0.00 sec)
 
Rows matched: 1 Changed: 1 Warnings: 0
 
mysql> select * from tmp1;
 
+------+-----------+
 
| id | name |
 
+------+-----------+
 
| 1 | aiezu.com |
 
+------+-----------+
 
1 row in set (0.00 sec)
 
mysql> select * from tmp2;
 
+------+-----------+
 
| fid | name |
 
+------+-----------+
 
| 1 | aiezu.com |
 
+------+-----------+
 
1 row in set (0.00 sec)

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Detailed explanation of mysql trigger example
  • Introduction and use of triggers and cursors in MySQL
  • Use and understanding of MySQL triggers
  • How to use cursor triggers in MySQL
  • The use of MySQL triggers and what to pay attention to
  • MySQL trigger usage scenarios and method examples
  • Detailed explanation of MySQL database triggers
  • A brief discussion on MySql views, triggers and stored procedures
  • Detailed explanation of the basic usage of MySQL triggers [create, view, delete, etc.]
  • Use of MySQL triggers

<<:  Detailed graphic tutorial on how to enable remote secure access with Docker

>>:  Vue method to verify whether the username is available

Recommend

Implementation of built-in modules and custom modules in Node.js

1. Commonjs Commonjs is a custom module in nodejs...

In-depth explanation of binlog in MySQL 8.0

1 Introduction Binary log records SQL statements ...

mysql having usage analysis

Usage of having The having clause allows us to fi...

MySQL 8.0.12 Installation and Configuration Tutorial

This article records the detailed tutorial for in...

Example code of vue + element ui to realize player function

The display without the effect picture is just em...

Element UI table realizes drop-down filtering function

This article example shares the specific code for...

How to force vertical screen on mobile pages

I recently wrote a mobile page at work, which was...

Example of implementing the Graphql interface in Vue

Note: This article is about the basic knowledge p...

HTML tag dl dt dd usage instructions

Basic structure: Copy code The code is as follows:...

WeChat Mini Program to Implement Electronic Signature

This article shares the specific code for impleme...

Record the steps of using mqtt server to realize instant communication in vue

MQTT Protocol MQTT (Message Queuing Telemetry Tra...

How to clear default styles and set common styles in CSS

CSS Clear Default Styles The usual clear default ...

Detailed steps for using jib for docker deployment in Spring Cloud

Introduction to Jib Jib is a library developed by...

Detailed explanation of SQL injection - security (Part 2)

If there are any errors in this article or you ha...