0

Assume I have a mysql table with columns Status, Timestamp and others. Is it possible to make Timestamp update whenever Status is updated?

S.Dan
  • 1,826
  • 5
  • 28
  • 55

2 Answers2

1

Yeah, you've got 3 different approaches:

1) Add the timestamp update to your queries explicitly.

2) Implement triggers on your database. Triggers are used to take actions when events occur. The only bad thing with using triggers is it's not guaranteed the timestamp with be the same moment the update has occurred; hence the reason why updating it with the query is better.

3) Make your updates using stored procedures which make multiple changes by a single database call.

To be honest: 1 is the simplest approach and is what I'd recommend as this also isn't really what triggers are necessarily intended for.

Gerik
  • 698
  • 3
  • 11
1

You can use a AFTER UPDATE ON table Trigger, that will check if the Status Column value has been changed. Trigger Syntax and Examples

For example

CREATE TRIGGER `your_trigger_name` 
AFTER INSERT ON `TableName` 
FOR EACH ROW  

IF NOT NEW.StatusColumnName <=> OLD.StatusColumnName THEN
    UPDATE TableName SET TimeStampColumnName = NOW() WHERE id = OLD.id; 
END IF;
neildt
  • 5,101
  • 10
  • 56
  • 107