Skip to main content

Posts

Showing posts with the label MySql trigger

MySql Trigger

MySql Trigger A trigger is a stored program which executed automatically to respond to a specific event. such as insert, update or delete occurred in a table. Syntax : CREATE     TRIGGER `event_name` BEFORE/AFTER INSERT/UPDATE/DELETE     ON `database`.`table`     FOR EACH ROW BEGIN -- trigger body -- this code is applied to every  -- inserted/updated/deleted row     END; Notes: event_name :  All triggers must have unique names within a schema trigger_event : Indicates the kind of operation that activates the trigger. tbl_name : The trigger becomes associated with the table named tbl_name Example 1: Suppose you have user table and user_audit table and you want to track new users only. Then you have to create a trigger DELIMITER $$ CREATE TRIGGER `blog_after_insert` AFTER INSERT  ON `user`  FOR EACH ROW  BEGIN SET @changetype = 'NEW'; INSERT INTO user_audit (user_id, change...