When using MYSQL, triggers are often used, but sometimes improper use can cause some trouble. Is there a way to control the calling of triggers? How to disable the trigger? 1. Create two new tables: Table demo_1: CREATE TABLE `demo_1` ( `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key', `STUNAME` varchar(32) DEFAULT NULL COMMENT 'Name', `AGE` tinyint(4) DEFAULT NULL COMMENT 'Age', PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8; Table: demo_2 CREATE TABLE `demo_2` ( `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key', `STU_ID` int(11) DEFAULT NULL COMMENT 'Student ID', `MATH` double DEFAULT NULL COMMENT 'score', PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8; 2. Then create a trigger for demo_1 DROP TRIGGER IF EXISTS `insertTragger`; DELIMITER ;; CREATE TRIGGER `insertTragger` AFTER INSERT ON `demo_1` FOR EACH ROW begin select ID INTO @v1 from demo_1 order by ID desc limit 1; Insert into demo_2(STU_ID, MATH) values(@v1, 98.5); end ;; DELIMITER ; 3. The trigger is: when a piece of data is added in demo_1, a piece of data is automatically added in demo_2. 4. But I don't want to trigger the trigger every time data is input or output. I only want it to be triggered when I want it to be triggered. Rewrite the trigger: DROP TRIGGER IF EXISTS `insertTragger`; DELIMITER ;; CREATE TRIGGER `insertTragger` AFTER INSERT ON `demo_1` FOR EACH ROW begin if @enable_trigger = 1 then select ID INTO @v1 from demo_1 order by ID desc limit 1; Insert into demo_2(STU_ID, MATH) values(@v1, 98.5); end if; end ;; DELIMITER ; 5. Call trigger SET @enable_trigger = 1; INSERT INTO demo_1(STUNAME , AGE) VALUES('Xiao Qiang', 17); After execution, a data entry 2 Xiaoqiang17 is added to the table demo_1 Table demo_2 also adds a data 2 2 98.5 6. Disable the trigger SET @enable_trigger = 0; INSERT INTO demo_1(STUNAME , AGE) VALUES('Xiao Qiang', 17); After execution: After execution, a data entry 2 Xiaoqiang17 is added to the table demo_1 No data is added to table demo_2 The above solves the problem of flexible calling of triggers. Summarize The above is the tutorial on disabling and starting triggers in MySQL that I introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: Detailed explanation of function classification and examples of this pointing in Javascript
>>: Linux disk sequential writing and random writing methods
Recently, some friends said that after installing...
1. Introduction Today a colleague asked me how to...
1. Download the latest nginx docker image $ docke...
cause I recently started to refactor the project,...
Well, you may be a design guru, or maybe that'...
Introduction to common Dockerfile instructions in...
CSS naming rules header: header Content: content/c...
Start a new project This article mainly records t...
Table of contents jQuery's $.ajax The beginni...
I. Strict Mode Explanation According to the restr...
This article shares the specific code of Vue to i...
1. Download the installation package from the off...
Preface In the development of actual projects, we...
1. Turn off the firewall and transfer the softwar...