What are good reasons to choose TIMESTAMP over DATETIME columns in MySQL?
Disclaimer: This might be a duplicate of datetime vs. timestamp?, but I feel I am not convinced of the answers:
My question is:
What are the good reasons to choose TIMESTAMP over D开发者_JS百科ATETIME columns in MySQL?I'm pretty sure I don't know everything there is about the differences between those two, so I will try to list the main advantages of DATETIME (DT
) compared to TIMESTAMP (TS
) to my knowledge:
DT
is human readable (TS
is not without usingTO_DATE
)DT
has a possble timespan of 8999 Years (1000-01-01 00:00:00
to9999-12-31 23:59:59
) (TS
only about 68 years,1970-01-01
to2038-01-19
)DT
fields seem to perform better (according to this blog post)DT
can be used for advanced date calculation (SELECT NOW() + INTERVAL 2 DAY
)
And vice versa:
TS
only needs 4 bytes (DT
uses 8)TS
are stored as UTC values and changed according to the client's timezone settingTS
columns can serve as a "log" for monitoring when a row has changes
The only reason I see for using it then would be the row monitoring, but looking at the range of TIMESTAMP
, which will end in "only" 28 years*, I would rather go with an insert or update trigger.
So, what am I missing? I still don't see a really good reason for choosing timestamp for any purpose?
* I know, this is appears quite long, but back in the 60s some engineers also decided to shave off 2 bytes of the year field, because their computer systems would never run until the year 2000 .....
DT columns can also serve as a "log" for monitoring when a row changes, I use them and triggers to do that.
I usually use timestamp when working with a lot of PHP stuff as to cut down on too much date parsing. If the need ever arises, I can update my software to use datetime.
The main question is: what is the purpose of dateime/timestamp field? If it is just a timestamp like in a log file where you only store the actual time the timestam is the better choice. Because it needs only 4 bytes and we are in the scope what the timestamp can cover it. If it is a date like date of birth the datetime is the good choice. We hope we'll have solution before end of timestamp scope. ;) (Sorry for my English).
Here's a funny aspect of DT which I already used: Using DT - at least in MySQL - you have the option of leaving either the year at "0000", and/or both month and day: "00-00". This comes in handy for storing birthday dates without requiring complete information!
精彩评论