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

How to build and deploy Node project with Docker

Table of contents What is Docker Client-side Dock...

How to add fields to a large data table in MySQL

Preface I believe everyone is familiar with addin...

Summary of several situations in which MySQL indexes fail

1. Indexes do not store null values More precisel...

Three useful codes to make visitors remember your website

Three useful codes to help visitors remember your...

Introduction to the usage of props in Vue

Preface: In Vue, props can be used to connect ori...

Summary of 9 excellent code comparison tools recommended under Linux

When we write code, we often need to know the dif...

Mysql master-slave synchronization configuration scheme under Centos7 system

Preface Recently, when working on a high-availabi...

How to handle long data when displaying it in html

When displaying long data in HTML, you can cut off...

An article tells you how to write a Vue plugin

Table of contents What is a plugin Writing plugin...

IIS7 IIS8 reverse proxy rule writing, installation and configuration method

Purpose: Treat Station A as the secondary directo...

Solution to the Docker container being unable to access the host port

I recently encountered a problem at work. The doc...

How to use docker+devpi to build local pypi source

Some time ago, I needed to use pip downloads freq...