Skip to content
Advertisement

Use UNIX_TIMESTAMP on update instead of timestamp

I want to add a column with a unix timestamp to see when the row was last altered. So far i could only figure out how to add a column with timestamp format.

Is there any solution to use a unix timestamp – something like this:

UPDATE:

similar question

As far as i understand this thread only shows how to add a unix timestamp manually to each row. I was wondering if it is also possible to do this automatically.

Advertisement

Answer

The TIMESTAMP data type is the only one that supports the CURRENT_TIMESTAMP default in MySQL. Internally the timestamp is stored as an int, but the interface uses a date time format.

You have some options to accomplish what you want:

Store it as a timestamp, and apply the UNIX_TIMESTAMP() function when you select from it

Store it as an int, and use a trigger to populate the value

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement