How to disable and start triggers in Mysql
Today, I will talk to you about how to disable and start triggers in Mysql. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
1. Create two new tables:
Table demo_1:
CREATE TABLE `demo_ 1` (`ID`int (11) NOT NULL AUTO_INCREMENT COMMENT 'key', `STREE` 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 'key', `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
two。 Then create a trigger for demo_1
DROP TRIGGER IF EXISTS `insertTragger`; DELIMITER;; CREATE TRIGGER `insertTragger` AFTER INSERT ON `insertTragger` FOR EACH ROW beginselect ID INTO @ v1 from demo_1 order by ID desc limit 1 into demo_2 (STU_ID, MATH) values (@ v1,98.5); end;;DELIMITER
3. The trigger is: when a piece of data is added to demo_1, a piece of data is automatically added to demo_2.
4. But I don't want to trigger every time I get in and out of the data, only when I want to.
Rewrite the trigger:
DROP TRIGGER IF EXISTS `insertTragger`; DELIMITER;; CREATE TRIGGER `insertTragger` AFTER INSERT ON `insertTragger` FOR EACH ROW beginif @ enable_trigger = 1 thenselect ID INTO @ v1 from demo_1 order by ID desc limit 1 domestic insert into demo_2 (STU_ID, MATH) values (@ v1,98.5); end if;end;;DELIMITER
5. Call trigger
SET @ enable_trigger = 1 th insert INTO demo_1 (STUNAME, AGE) VALUES ('Xiaoqiang', 17); after execution, a data item 2 Xiaoqiang 17 was added to the table demo_1, and a data item 22 98.5 was also added to the table demo_2.
6. Disable the tactile device
SET @ enable_trigger = 0th insert INTO demo_1 (STUNAME, AGE) VALUES ('Xiaoqiang', 17); after execution: after execution, add a piece of data 2 Xiaoqiang 17 to the table demo_1 after reading the above, do you have any further understanding of how to disable and start triggers in Mysql? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.