I want to have one table with two TIMESTAMP
columns. One column to keep track of when the record was created and another to keep track of when it was modified. I want these values handled by the database. I don't want my app layer to have to think about it.
I know that if you have a TIMESTAMP
column with a DEFAULT CURRENT_TIMESTAMP
or an ON UPDATE CURRENT_TIMESTAMP
you cannot have another TIMESTAMP
column. You can use DATETIME
but there is no way to default it, that I know of, outside of a trigger.
I found that you can have multiple TIMESTAMP
columns by leaving each without DEFAULT
or ON UPDATE
and inserting NULL
when the record is created, causing each to have the current timestamp. From that point on the first column will automatically be updated.
This works fantastically but it leaves me with a funny feeling. Like this may be a bug and it could be patched at any time. If this is the way it is supposed to work then so be it. I will merrily go on my way. Can anyone tell me if this is the best way to do this or should I be using triggers?
It's documented in the MySQL docs:
In addition, you can initialize or update any TIMESTAMP column to the current date and time by assigning it a NULL value, unless it has been defined with the NULL attribute to permit NULL values.
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With