MYSQL Triggers


Create MYSQL Trigger

MYSQL Triggers can be created in order to make an update on a database table only when a specific condition is met. With the code the code below, there are two columns in one table that can be cloned. For example, columnb can be a cloned copy of columna only when the date in the date column is in the future. Therefore, if the date is in the future for a specific entry and columna is updated; then columnb will be updated too. But, columnb will not update if the date column is in the past. 

DELIMITER //
 CREATE TRIGGER updatemytable BEFORE UPDATE ON tablename
     FOR EACH ROW
     BEGIN
     IF NEW.columna <> OLD.columnb AND OLD.date >= now() THEN
     SET NEW.columnb = NEW.columna;     
     END IF;
     END
     //    
mysql> DELIMITER;