Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Trigger question: only trigger when a column is changed?

Tags:

I don't know if this is possible, but I have a column named active in a table. Whenever the active column gets changed, I would like to reset the date in the date column, but ONLY if the active column gets changed.

If other columns are changed but not the active column, then the date would remain the same.

like image 728
kylex Avatar asked Nov 04 '10 14:11

kylex


People also ask

How do you execute a trigger only when a specific column is updated MySQL?

In SQL Server, you can create DML triggers that execute code only when a specific column is updated. The trigger still fires, but you can test whether or not a specific column was updated, and then run code only if that column was updated. You can do this by using the UPDATE() function inside your trigger.

How do I know which column is updated in a trigger?

SQL Server COLUMNS_UPDATED() Function for Triggers. This function is used to know the inserted or updated columns of a table or view. It returns a VARBINARY stream that by using a bitmask allows you to test for multiple columns.

Does an on update trigger have access to old and new variables?

An UPDATE trigger can refer to both OLD and NEW transition variables. INSERT.

Is it possible to create the following trigger before or after update trigger for each row?

A new column value can be assigned in a BEFORE row trigger, but not in an AFTER row trigger (because the triggering statement takes effect before an AFTER row trigger is fired). If a BEFORE row trigger changes the value of new .


1 Answers

something like

DELIMITER //  CREATE TRIGGER updtrigger BEFORE UPDATE ON mytable      FOR EACH ROW      BEGIN      IF NEW.active <> OLD.active THEN      SET NEW.date = '';           END IF;      END      // 
like image 143
Haim Evgi Avatar answered Nov 03 '22 00:11

Haim Evgi