Table of Contents

DB Triggers

Author: Milos Zubal

Introduction


The Trigger

SELECT * FROM snmp_manager.tab_modification_log;

The trigger code:

USE snmp_manager;
 
DROP TABLE IF EXISTS snmp_manager.tab_modification_log;
CREATE TABLE snmp_manager.tab_modification_log
(
	serialnum INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
	mod_user VARCHAR(64) DEFAULT "",
	mod_table VARCHAR(64) NOT NULL,
	mod_id INTEGER NOT NULL,
	old_val VARCHAR(512) DEFAULT "",
	new_val VARCHAR(512) DEFAULT "",
	ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
 
 
DROP TRIGGER IF EXISTS snmp_manager.thread_config_after_update;
DELIMITER $$
CREATE TRIGGER snmp_manager.thread_config_after_update
AFTER UPDATE ON snmp_manager.thread_config
FOR EACH ROW
BEGIN
	IF (NEW.is_active != OLD.is_active) THEN
		INSERT INTO snmp_manager.tab_modification_log
			( mod_user, mod_table, mod_id, old_val, new_val )
		VALUES
		(
			 USER(),
			 "thread_config",
			 NEW.id,
			 OLD.is_active,
			 NEW.is_active  
		);
	END IF;
END $$
DELIMITER ;