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

Asked

Viewed 1,652 times

0

I created a Trigger to my table X, with the purpose of making a record of update that run you in this table.

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

PROBLEMATICA :

The table X has several columns, and for each update in one of the columns the Trigger is soon fired.

Is it possible to specify a spine table X for which the Trigger shall be fired in the case of a update ?

LOGICA :

Table X

  • Si update in the column y : fires the Trigger

  • If not ( you are in another spine) : do nothing.

1 answer

1


In Mysql you cannot directly fire 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, inside Trigger, if the column has changed, only then perform 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 making an update to the table row Trigger will be triggered and will check whether the column value field changed. If different, it will perform an action, which can be the log recording.

Update Postgre allows Trigger execution restriction per column, as documented:

Postgresql - Create Trigger

Browser other questions tagged

You are not signed in. Login or sign up in order to post.