Type | Range | Remark |
DATE | '1000-01-01' to '9999-12-31' |
只有日期部分,沒有時間部分 |
DATETIME | '1000-01-01 00:00:00' to '9999-12-31 23:59:59' |
時間格式爲 Y YYY-MM-DD hh:mm:ss ,默認精確到秒 |
TIMESTAMP | '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC |
默認精確到秒 |
5.7 以後的版本(其實應該說5.6.5),在默認的秒精確度上,能夠帶小數,最多帶6位小數,便可以精確到 microseconds (6 digits) precision。java
Type | Range | Remark |
DATETIME | '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999' |
'YYYY-MM-DD hh:mm:ss [.fraction ]' |
TIMESTAMP | '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999' |
'YYYY-MM-DD hh:mm:ss [.fraction ]' |
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)
By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis.
As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value,
the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions.
The current time zone is available as the value of the time_zone system variable.
可能上面的幾句英文很差理解,咱們舉個例子。mysql
建立2張測試表:git
create table testtime(id int,hiredate timestamp); create table testtime1(id int,hiredate datetime);
向這兩個測試表中分別插入一筆測試數據sql
insert into testtime values(1,'20151208000000'); insert into testtime1 values(1,'20151208000000');
查看這種顯示的時區時間設置測試
查詢命令spa
show variables like '%time_zone%';
上述「CST」指的是MySQL所在主機的系統時間,是中國標準時間的縮寫,China Standard Time UT+8:00code
修改time_zoneserver
set time_zone='+0:00';
經過結果能夠看出,testtime中返回的時間提早了8個小時,而testtime1中時間則不變。blog
若是新建一個客戶端鏈接,這個時區的修改不影響新鏈接。ci
TIMESTAMP 在mysql 5.6.5以後,TIMESTAMP(fraction
)中的fraction
表明的是小數位數,即默認秒,以秒爲單位的小數點位數。 up to microseconds (6 digits) precision,最大爲6.
超過6則報錯:
ERROR 1426 (42000): Too-big precision 7 specified for 'hiredate'. Maximum is 6.
在比較久的版本上,這個數字就表明不一樣的意義,如下內容爲舊版本的關於TIMESTAMP的知識。
TIMESTAMP(fraction
)中fraction
值顯示尺寸的格式以下表所示:
列類型 | 顯示格式 |
TIMESTAMP(14) | YYYYMMDDHHMMSS |
TIMESTAMP(12) | YYMMDDHHMMSS |
TIMESTAMP(10) | YYMMDDHHMM |
TIMESTAMP(8) | YYYYMMDD |
TIMESTAMP(6) | YYMMDD |
TIMESTAMP(4) | YYMM |
TIMESTAMP(2) | YY |
就版本中「完整」TIMESTAMP格式是14位,但TIMESTAMP列也能夠用更短的顯示尺寸,創造最多見的顯示尺寸是六、八、十二、和14。
在建立表時能夠指定一個任意的顯示尺寸,可是定義列長爲0或比14大均會被強制定義爲列長14。列長在從1~13範圍的奇數值尺寸均被強制爲下一個更大的偶數。