Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL trigger On Insert/Update events

So I have two tables like this...

ext_words ------------- | id | word | ------------- | 1  | this | ------------- | 2  | that | ------------- | 3  | this | -------------  ext_words_count --------------------- | id | word | count | --------------------- | 1  | this |   2   | --------------------- | 2  | that |   1   | --------------------- 

I am trying to create a trigger that will:

  • update ext_words_count.count when ext_words.word is updated.

To further complicate matters,

  • if ext_words.word does not exist in ext_words_count when ext_words is updated, I would like to insert it into ext_words_count and set count as 1.

I have been looking at similar questions:
1. Before / after insert trigger using auto increment field, and
2. Using Trigger to update table in another database
trying to combine the 2. Here is what I have so far:

DELIMITER $$ CREATE TRIGGER update_count AFTER UPDATE ON ext_words FOR EACH ROW BEGIN    UPDATE ext_words_count     SET word_count = word_count + 1   WHERE word = NEW.word;  END; $$ DELIMITER ; 

Any advice and direction is greatly appreciated. Or possibly another method that I have overlooked and as always thanks in advance!

UPDATE:
I have opted for using 2 triggers, one for INSERT and one for UPDATE because I am not that familiar with conditional statements in MySQL.

DELIMITER $$ CREATE TRIGGER insert_word AFTER INSERT ON ext_words   FOR EACH ROW     BEGIN       INSERT IGNORE INTO ext_words_count (word) VALUES (NEW.word);     END; $$ DELIMITER ; 

and

DELIMITER $$ CREATE TRIGGER update_word AFTER UPDATE ON ext_words   FOR EACH ROW     BEGIN       UPDATE ext_words_count        SET word_count = word_count + 1        WHERE word = NEW.word;     END; $$ DELIMITER ; 

The INSERT query is working great, however the UPDATE query is not updating word_count. Is there something I missed in the update query..?

like image 805
Drewness Avatar asked Apr 12 '13 15:04

Drewness


People also ask

How do you create a trigger after insert?

First, specify the name of the trigger that you want to create after the CREATE TRIGGER keywords. Second, use AFTER INSERT clause to specify the time to invoke the trigger. Third, specify the name of the table on which you want to create the trigger after the ON keyword.

Can we update a trigger in MySQL?

Restrictions. We can access the OLD rows but cannot update them. We can access the NEW rows but cannot update them. We cannot create an AFTER UPDATE trigger on a VIEW.

How do you determine if trigger is insert or update or DELETE?

Triggers have special INSERTED and DELETED tables to track "before" and "after" data. So you can use something like IF EXISTS (SELECT * FROM DELETED) to detect an update. You only have rows in DELETED on update, but there are always rows in INSERTED . Look for "inserted" in CREATE TRIGGER.

How do I create a trigger update in MySQL?

Introduction to MySQL AFTER UPDATE triggers In this syntax: First, specify the name of the trigger that you want to create in the CREATE TRIGGER clause. Second, use AFTER UPDATE clause to specify the time to invoke the trigger. Third, specify the name of the table to which the trigger belongs after the ON keyword.


2 Answers

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;    
like image 180
Drewness Avatar answered Sep 27 '22 18:09

Drewness


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 ; 
like image 42
Aman Maurya Avatar answered Sep 27 '22 17:09

Aman Maurya