MySQL trigger On Insert/Update events

老子叫甜甜 提交于 2019-11-26 18:55:10
Drewness

With Grijesh's perfect help and his suggestion to use conditional statements, I was able to get ONE trigger that does both tasks. Thanks again Grijesh

 DELIMITER $$ 
 CREATE TRIGGER update_count AFTER INSERT ON ext_words 
 FOR EACH ROW 
   BEGIN
     IF NOT EXISTS (SELECT 1 FROM ext_words_count WHERE word = NEW.word) THEN
       INSERT INTO ext_words_count (word) VALUES (NEW.word);
   ELSE
       UPDATE ext_words_count SET word_count = word_count + 1 WHERE word = NEW.word;
   END IF;
  END $$    
 DELIMITER;   

avoid use of keywords like count as its is used by the sql method some time its create error but some runs good

DELIMITER $$
 CREATE TRIGGER update_count
   AFTER UPDATE ON ext_words
     FOR EACH ROW
       BEGIN

          SELECT count INTO @x FROM ext_words_count LIMIT 1;
          UPDATE ext_words_count
          SET count = @x + 1
          WHERE word = NEW.word;

END;
$$
DELIMITER ;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!