6

My database table has a column defined to use ON UPDATE CURRENT_TIMESTAMP:

  • Field: last_access
  • Type: timestamp
  • Collation:
  • Attributes: on update CURRENT_TIMESTAMP
  • Null: No
  • Default: CURRENT_TIMESTAMP
  • Extra: ON UPDATE CURRENT_TIMESTAMP

How can I force this last_access field to update for a given record when no other data in the record is changing?

Jim Fell
  • 13,750
  • 36
  • 127
  • 202
  • 1
    Well if that's all you want to update maybe just set it? Or see if making it null would then update it to over write the null with the current time.. – Jess Feb 09 '12 at 21:05

2 Answers2

8

UPDATE your_table SET last_access = CURRENT_TIMESTAMP(); ?

berty
  • 2,178
  • 11
  • 19
  • Honestly I've been tripping on this answer thinking something is missing in it. Basically its good. So I needed something like this to convince me, UPDATE table SET col1 = 'value1', updated_at = now() where col2 = 'value2' By updating a timestamp column updated_at which never fails to update the schema will know that something has definitely changed so it "overwrites" other columns as well – Ishmael Mavor Raines Jul 29 '22 at 11:20
0

You have to also update a timestamp column in the process

UPDATE table SET col1 = 'value1', updated_at = now() where col2 = 'value2'