MySQL trigger simple usage example

MySQL trigger simple usage example

This article uses examples to illustrate the simple usage of MySQL triggers. Share with you for your reference, the details are as follows:

MySQL triggers, like stored procedures, are programs embedded in MySQL. Triggers are triggered by events, including INSERT, UPDATE, and DELETE, but not SELECT.

Create a trigger

CREATE TRIGGER name,time,event ON table_name FOR EACH ROW trigger_stmt

For example

Copy the code as follows:
CREATE TRIGGER ins_sum BEFORE INSERT ON account FOR EACH ROW SET @sum = @sum + NEW.amount

Triggers with multiple execution statements

CREATE TABLE test1(a1 INT);
CREATE TABLE test2(a2 INT);
CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
CREATE TABLE test4(
  a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  b4 INT DEFAULT 0
);
DELIMITER //
CREATE TRIGGER testref BEFORE INSERT ON test1
  FOR EACH ROW BEGIN
    INSERT INTO test2 SET a2 = NEW.a1;
  DELETE FROM test3 where a3 = NEW.a1;
    UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
  END
  //
DELIMITER ;
INSERT INTO test3(a3) VALUES (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL);
INSERT INTO test4(a4) VALUES (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0);
//Start testing INSERT INTO test1 VALUES (1), (3), (1), (7), (1), (8), (4), (4);

View Triggers

SHOW TRIGGERS \G; //View all SELECT * FROM information_schema.TRIGGERS where TRIGGER_NAME = 'testref';

Deleting a trigger

DROP TRIGGER testref;

Comprehensive Case

Step 1: Create the persons table

CREATE TABLE persons (name VARCHAR(40), num int);

Step 2: Create a sales table

CREATE TABLE sales (name VARCHAR(40), sum int);

Step 3: Create a trigger

CREATE TRIGGER num_sum AFTER INSERT ON persons
FOR EACH ROW INSERT INTO sales VALUES (NEW.name,7*NEW.num);

Step 4: Insert records into the persons table

INSERT INTO persons VALUES ('xiaoxiao',20),('xiaohua',69);
SELECT * FROM persons;
SELECT *FROM sales;

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 usage
  • Detailed explanation of the concept, principle and usage of MySQL triggers
  • Brief introduction and use cases of triggers in MySQL
  • How to test mysql triggers and stored procedures
  • How to create a trigger in MySQL
  • MySQL 5.0 Trigger Reference Tutorial
  • Different ways to write triggers in MySQL and SQL
  • Basic Learning Tutorial on Triggers in MySQL
  • A simple example and introduction to MySQL triggers
  • Detailed example of mysql trigger usage
  • MySQL trigger syntax and application examples

<<:  How to install pip package in Linux

>>:  Zabbix uses PSK shared key to encrypt communication between Server and Agent

Recommend

Detailed explanation of InnoDB storage files in MySQL

Physically speaking, an InnoDB table consists of ...

Detailed process of SpringBoot integrating Docker

Table of contents 1. Demo Project 1.1 Interface P...

HTML is something that web page creators must learn and master.

What are the benefits of learning HTML? 1: Easily...

React Native startup process detailed analysis

Introduction: This article takes the sample proje...

How to install Elasticsearch7.6 cluster in docker and set password

Table of contents Some basic configuration About ...

Detailed explanation of Javascript basics loop

Table of contents cycle for for-in for-of while d...

Vue uses vue-quill-editor rich text editor and uploads pictures to the server

Table of contents 1. Preparation 2. Define the gl...

Tutorial on deploying the open source project Tcloud with Docker on CentOS8

1. Install Docker 1. I installed Centos7 in the v...

Summary of MySQL InnoDB architecture

Table of contents introduction 1. Overall archite...

The easiest way to install MySQL 5.7.20 using yum in CentOS 7

The default database of CentOS7 is mariadb, but m...

Detailed explanation of three commonly used web effects in JavaScript

Table of contents 1 element offset series 1.1 Off...