Choosing a Temporal Data Type
Problem
You need to store temporal data but aren’t sure which is the most appropriate data type.
Solution
Choose the data type according to the characteristics of the information to be stored and how you need to use it. Consider questions such as these:
Do you need times only, dates only, or combined date and time values?
What range of values do you require?
Do you want automatic initialization of the column to the current date and time?
Discussion
MySQL provides
DATE and TIME data types for representing date and
time values separately, and
DATETIME and
TIMESTAMP types for combined
date-and-time values. These values have the following
characteristics:
DATEvalues are handled as strings inCCYY-MM-DDformat, whereCC,YY,MM, andDDrepresent the century, year within century, month, and day parts of the date. The supported range forDATEvalues is1000-01-01to9999-12-31.TIMEvalues are represented as strings inhh:mm:ssformat, wherehh,mm, andssare the hours, minutes, and seconds parts of the time.TIMEvalues often can be thought of as time-of-day values, but MySQL actually treats them as elapsed time. Thus, they may be greater than23:59:59or even negative. (The actual range of aTIMEcolumn is-838:59:59to838:59:59.)DATETIMEandTIMESTAMPvalues are represented as combined date-and-time strings inCCYY-MM-DDhh:mm:ssformat. (Before MySQL 4.1,TIMESTAMPdisplay format wasCCYYMMDDhhmmssnumeric format. Older applications that depend on this display ...