MySQL trigger syntax and application examples

MySQL trigger syntax and application examples

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

Example: Create a trigger to record table addition, deletion, and modification operations

//Create user table;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `account` varchar(255) DEFAULT NULL,
 `name` varchar(255) DEFAULT NULL,
 `address` varchar(255) DEFAULT NULL,
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
//Create a history table for user table operations DROP TABLE IF EXISTS `user_history`;
CREATE TABLE `user_history` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `user_id` bigint(20) NOT NULL,
 `operatetype` varchar(200) NOT NULL,
 `operatetime` datetime NOT NULL,
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

DELIMITER: Change the input terminator. By default, the input terminator is a semicolon;. Here, it is changed to two semicolons;;. The purpose of this is to encapsulate multiple statements containing semicolons and execute them together after all are entered, rather than automatically executing them when the default semicolon terminator is encountered.

  • new: Available when insert and update events are triggered, pointing to the record being operated on
  • old: Available when delete and update events are triggered, pointing to the record being operated on

INSERT:

DROP TRIGGER IF EXISTS `tri_insert_user`;
DELIMITER ;;
CREATE TRIGGER `tri_insert_user` AFTER INSERT ON `user` FOR EACH ROW begin
  INSERT INTO user_history(user_id, operatetype, operatetime) VALUES (new.id, 'add a user', now());
end
;;
DELIMITER ;

UPDATE:

DROP TRIGGER IF EXISTS `tri_update_user`;
DELIMITER ;;
CREATE TRIGGER `tri_update_user` AFTER UPDATE ON `user` FOR EACH ROW begin
  INSERT INTO user_history(user_id,operatetype,operatetime) VALUES (new.id, 'update a user', now());
end
;;
DELIMITER ;

DELETE:

DROP TRIGGER IF EXISTS `tri_delete_user`;
DELIMITER ;;
CREATE TRIGGER `tri_delete_user` AFTER DELETE ON `user` FOR EACH ROW begin
  INSERT INTO user_history(user_id, operatetype, operatetime) VALUES (old.id, 'delete a user', now());
end
;;
DELIMITER ;

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:
  • Use and understanding of MySQL triggers
  • Detailed explanation of MySQL database triggers
  • MySQL trigger principle and usage example analysis
  • Introduction to the use and advantages and disadvantages of MySQL triggers

<<:  Using js to achieve the effect of carousel

>>:  Sample code for partitioning and formatting a disk larger than 20TB on centos6

Recommend

How does MySQL connect to the corresponding client process?

question For a given MySQL connection, how can we...

mysql data insert, update and delete details

Table of contents 1. Insert 2. Update 3. Delete 1...

Database index knowledge points summary

Table of contents First Look Index The concept of...

React antd realizes dynamic increase and decrease of form

I encountered a pitfall when writing dynamic form...

Installation process of zabbix-agent on Kylin V10

1. Download the installation package Download add...

How to use Docker to package and deploy images locally

First time using docker to package and deploy ima...

How to add a certificate to docker

1. Upgrade process: sudo apt-get update Problems ...

JavaScript canvas to achieve raindrop effects

This article example shares the specific code of ...

Detailed implementation plan of Vue front-end exporting Excel files

Table of contents 1. Technology Selection 2. Tech...

Data URI and MHTML complete solution for all browsers

Data URI Data URI is a scheme defined by RFC 2397...

The difference between redundant and duplicate indexes in MySQL

MySQL allows you to create multiple indexes on a ...

WeChat applet realizes the function of uploading pictures

This article example shares the specific code for...

Complete steps for using Nginx+Tomcat for load balancing under Windows

Preface Today, Prince will talk to you about the ...