Example of trigger on after delete with update query mysql

By Hardik Savani | April 5, 2016 | Category : PHP MySql SQL

Sometimes we need to fire trigger like after delete update some fields of another tables. for example if you have chat_message tables and when remove row on this table then we need to update "remove_counter(remove_counter + 1)" on users table. i did give you bellow example you can see how to write trigger code:


delimiter //

CREATE TRIGGER add_ban_counter_in_users

AFTER DELETE ON `chat_messages`



UPDATE `users`

SET remove_counter = users.remove_counter + 1

WHERE users.id = old.user_id;


Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.