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:
|
<<: How to install pip package in Linux
>>: Zabbix uses PSK shared key to encrypt communication between Server and Agent
Table of contents Introduction question Design 1:...
Flex layout is undoubtedly simple and easy to use...
Physically speaking, an InnoDB table consists of ...
Table of contents 1. Demo Project 1.1 Interface P...
What are the benefits of learning HTML? 1: Easily...
Introduction: This article takes the sample proje...
The commonly used Oracle10g partitions are: range...
Table of contents Some basic configuration About ...
First, download the installation package from the...
Table of contents cycle for for-in for-of while d...
Table of contents 1. Preparation 2. Define the gl...
1. Install Docker 1. I installed Centos7 in the v...
Table of contents introduction 1. Overall archite...
The default database of CentOS7 is mariadb, but m...
Table of contents 1 element offset series 1.1 Off...