[Jump to content]

[Jump to content]

Call: 01223 96 99 44

Using MySQL Timestamps for Date Created and Date Updated fields

There is considerable confusion when googling about MySQL Timestamps, even at Stack Overflow - and it's not complex, so here is a simple guide:

To create a date created field which stamps the date and time when the row is created and then doesn't change it when it's updated use:
ALTER TABLE wonderBras ADD dateCreated TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

To create a date updated field which updates the date and time with every change, use:
ALTER TABLE wonderBras ADD dateUpdated TIMESTAMP;
(Note: this has the same effect as using ALTER TABLE wonderBras ADD dateUpdated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

The annoying MySQL bug(?) is that (unlike an Oracle DB) you CANNOT USE THESE TWO AT THE SAME TIME ON THE SAME TABLE.