How to create a Trigger for a specific column of a table?

0

I created a trigger for my X table, for the purpose of making a update record that runs on this table.

For each update in the X table, modified (old) values are stored in the Z table.

PROBLEMATIC :

The X table has several columns, and for each update in one of the columns the trigger

It will be possible to specify a column from the X table to which the trigger should be triggered in case of a update ?

LOGIC :

Table X

  • Update in the and column: triggers trigger
asked by anonymous 20.12.2016 / 12:36

1 answer

1

In MySql you can not directly trigger a trigger only after changing a column. Triggers are triggered after an interaction (insert, update, or delete) in the table row. For your case it is necessary to filter, within the trigger, if the column has changed, only to then execute the desired action (log recording in your case):

CREATE TRIGGER tstTrigger AFTER UPDATE ON Tabela
FOR EACH ROW
BEGIN
 if NEW.campo <=> OLD.campo THEN
    --Executa operação
END IF;
END;

When performing an update in the table row the trigger will be triggered and will check if the field column value has changed. If it is different, it will perform an action, which may be logging.

Update Postgre allows the trigger execution restriction per column, as documented:

Postgresql - Create Trigger

    
20.12.2016 / 13:19