- MySQL There are several types of data can be used to indicate the date and time, different versions may vary, Table 3-2
- lists the date and time types in MySQL 5.0 are supported.
- Table 3-2 MySQL date and time of the type
Date and Time Types | byte | Minimum | Maximum |
DATE | 4 | 1000-01-01 | 9999-12-31 |
DATETIME | 8 | 1000-01-01 00:00:00 | 9999-12-31 23:59:59 |
TIMESTAMP | 4 | 19700101080001 | 2038 a year time |
TIME | 3 | -838:59:59 | 838:59:59 |
YEAR | 1 | 1901 | 2155 |
The date and time bytes Minimum Maximum Type
DATE. 4 1000-01-01 9999-12-31
DATETIME. 8 1000-01-01 00:00:00 9999-12-31 23:59:59
TIMESTAMP 19,700,101,080,001 2038 years. 4 at some point
the tIME 3 -838: 59: 59 838: 59: 59
YEAR 1 1901 2155
- the main difference between these data types are as follows:
l If you are used to represent the date, typically used to represent dATE.
If you want to represent year, month, day, hour, usually expressed DATETIME.
If only used to indicate the minutes and seconds, usually expressed TIME.
If you frequently need to insert or update date to the current system time is usually used to represent TIMESTAMP.
After TIMESTAMP value returned is displayed "YYYY-MM-DD HH: MM: SS" string format, the display width is fixed
- is 19 characters. If you want to get a numeric value, it should add +0 in TIMESTAMP column.
- If only that year, YEAR can be expressed, it takes up less space than DATE. There YEAR or two
- in four formats. The default is 4 format. 4-bit format, the allowable values are 2155 and 1901 ~ 0000. In
- 2-bit format, the allowable values are from 70 to 69, from 1970 to 2069 represents. MySQL displays YEAR values in YYYY format.
-
create table t (d date,t time,dt datetime);
TIMESTAMP Another important feature that is relevant and time zone.
When the date is inserted, will be converted to the local time zone is stored; and which is removed from the database, also you need the date displayed after converting the local time zone.
- TIMESTAMP support of smaller time range, which ranges from 19700101080001-2038 a year
- time, and DATETIME is from 1000-01-01 00:00:00 to 9999-12-31 23:59: 59, a wider range.
- a first table TIMESTAMP column is automatically set to the system time. If you insert a TIMESTAMP column
- NULL, the column value is automatically set to the current date and time. In the insert or update a row but does not explicitly give
- TIMESTAMP column is automatically set when the assignment is the current date and time for the column, when inserting values exceed
- the value range, MySQL considers the value of the overflow, use the " 0000-00-00 00:00:00 "be filled
- TIMESTAMP insertion and queries are affected by local time zones, better reflect the actual date. And
- DATETIME can only reflect upon insertion of the local time zone, people in other time zones see the data bound to have errors.
- TIMESTAMP attribute is greatly affected by MySQL server version and SQLMode, this chapter are based on MySQL