In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.