Database
SQL

Example Of Trigger On After Delete With Update Query Mysql

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:

  • 4.5/5.0
  • Last updated 08 September, 2022
  • By Admin
Example
delimiter //
CREATE TRIGGER add_ban_counter_in_users 
	AFTER DELETE ON `chat_messages` 
	FOR EACH ROW
BEGIN
   UPDATE `users` 
   	SET remove_counter = users.remove_counter + 1 
   	WHERE users.id = old.user_id;
END