Creating Trigger in MySQL

Create trigger with multiple if else

CREATE DEFINER=`root`@`localhost` TRIGGER trigger_example
   BEFORE DELETE ON tbl_users
   FOR EACH ROW
BEGIN
       IF OLD.userId = 1126 THEN
         INSERT INTO tbl_archive_users ( userID, deleted_date, by) VALUES (OLD.userID,SYSDATE(),1126);
       ELSEIF OLD.userId = 1127 THEN
         INSERT INTO tbl_archive_users (userID, deleted_date,by) VALUES (OLD.userID,SYSDATE(), 1127);
       END IF;
END

Create Trigger table

DELIMITER //
CREATE TRIGGER archive_users
BEFORE DELETE ON tbl_users
  FOR EACH ROW
     BEGIN
        INSERT INTO tbl_archive_users (`userId`,`fname`,`lname`,`email`)
        VALUES(OLD.userId,OLD.fname,OLD.lname,OLD.email);
END; //
DELIMITER ;

Post a Comment