4

there are 2 columns in my mysql table: created_on and modified_on.I have set the default value for created_on column to CURRENT_TIMESTAMP. thus on every insert operation in the table the created_on column correctly sets the value to current timestamp.however i want that when i update the same row the 'modified_on' column updates the value to the timestamp when it is updated.Let me know if i use any function/trigger to do so or is there any in-build mysql feature for the same

2 Answers2

4

Try alter your table with ON UPDATE keyword like:

ALTER TABLE `tableName` 
CHANGE `modified_on` `modified_on` TIMESTAMP 
ON UPDATE CURRENT_TIMESTAMP;

Or you can add default value when inserted like:

ALTER TABLE `tableName` 
CHANGE `modified_on` `modified_on` TIMESTAMP 
ON UPDATE CURRENT_TIMESTAMP
NOT NULL DEFAULT CURRENT_TIMESTAMP ;

You also can define ON UPDATE to a timestamp data type when you create a table.

Habibillah
  • 27,347
  • 5
  • 36
  • 56
1

Write a trigger for doing this

Sumit Desai
  • 1,542
  • 9
  • 22