MySQL中有關(guān)TIMESTAMP和DATETIME的總結(jié)

原文鏈接https://www.cnblogs.com/ivictor/p/5028368.html

一、MySQL中如何表示當(dāng)前時(shí)間?

其實(shí),表達(dá)方式還是蠻多的,匯總?cè)缦拢?/p>

CURRENT_TIMESTAMP

CURRENT_TIMESTAMP()

NOW()

LOCALTIME

LOCALTIME()

LOCALTIMESTAMP

LOCALTIMESTAMP()

二、關(guān)于TIMESTAMP和DATETIME的比較

一個(gè)完整的日期格式如下:YYYY-MM-DD HH:MM:SS[.fraction],它可分為兩部分:date部分和time部分,其中,date部分對(duì)應(yīng)格式中的“YYYY-MM-DD”,time部分對(duì)應(yīng)格式中的“HH:MM:SS[.fraction]”。對(duì)于date字段來說,它只支持date部分,如果插入了time部分的內(nèi)容,它會(huì)丟棄掉該部分的內(nèi)容,并提示一個(gè)warning。

如下所示:


mysql> insert into test values(1,'20151208000000');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test values(1,'20151208104400');
Query OK, 1 row affected, 1 warning (0.01 sec) mysql> select * from test; +------+------------+
| id   | hiredate   |
+------+------------+
|    1 | 2015-12-08 |
|    1 | 2015-12-08 |
+------+------------+
2 rows in set (0.00 sec)</pre>

注:第一個(gè)沒提示warning的原因在于它的time部分都是0

TIMESTAMP和DATETIME的相同點(diǎn):

1> 兩者都可用來表示YYYY-MM-DD HH:MM:SS[.fraction]類型的日期。

TIMESTAMP和DATETIME的不同點(diǎn):

1> 兩者的存儲(chǔ)方式不一樣

對(duì)于TIMESTAMP,它把客戶端插入的時(shí)間從當(dāng)前時(shí)區(qū)轉(zhuǎn)化為UTC(世界標(biāo)準(zhǔn)時(shí)間)進(jìn)行存儲(chǔ)。查詢時(shí),將其又轉(zhuǎn)化為客戶端當(dāng)前時(shí)區(qū)進(jìn)行返回。

而對(duì)于DATETIME,不做任何改變,基本上是原樣輸入和輸出。

下面,我們來驗(yàn)證一下

首先創(chuàng)建兩種測(cè)試表,一個(gè)使用timestamp格式,一個(gè)使用datetime格式。

mysql> create table test(id int,hiredate timestamp);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test values(1,'20151208000000');
Query OK, 1 row affected (0.00 sec)

mysql> create table test1(id int,hiredate datetime);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test1 values(1,'20151208000000');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test; +------+---------------------+
| id   | hiredate            |
+------+---------------------+
|    1 | 2015-12-08 00:00:00 |
+------+---------------------+
1 row in set (0.01 sec)

mysql> select * from test1; +------+---------------------+
| id   | hiredate            |
+------+---------------------+
|    1 | 2015-12-08 00:00:00 |
+------+---------------------+
1 row in set (0.00 sec)</pre>

兩者輸出是一樣的。

其次修改當(dāng)前會(huì)話的時(shí)區(qū)

mysql> show variables like '%time_zone%'; +------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | CST    |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)

mysql> set time_zone='+0:00';
Query OK, 0 rows affected (0.00 sec) mysql> select * from test; +------+---------------------+
| id   | hiredate            |
+------+---------------------+
|    1 | 2015-12-07 16:00:00 |
+------+---------------------+
1 row in set (0.00 sec)

mysql> select * from test1; +------+---------------------+
| id   | hiredate            |
+------+---------------------+
|    1 | 2015-12-08 00:00:00 |
+------+---------------------+
1 row in set (0.01 sec)

上述“CST”指的是MySQL所在主機(jī)的系統(tǒng)時(shí)間,是中國標(biāo)準(zhǔn)時(shí)間的縮寫,China Standard Time UT+8:00

通過結(jié)果可以看出,test中返回的時(shí)間提前了8個(gè)小時(shí),而test1中時(shí)間則不變。這充分驗(yàn)證了兩者的區(qū)別。

2> 兩者所能存儲(chǔ)的時(shí)間范圍不一樣

timestamp所能存儲(chǔ)的時(shí)間范圍為:'1970-01-01 00:00:01.000000' 到 '2038-01-19 03:14:07.999999'。

datetime所能存儲(chǔ)的時(shí)間范圍為:'1000-01-01 00:00:00.000000' 到 '9999-12-31 23:59:59.999999'。

總結(jié):TIMESTAMP和DATETIME除了存儲(chǔ)范圍和存儲(chǔ)方式不一樣,沒有太大區(qū)別。當(dāng)然,對(duì)于跨時(shí)區(qū)的業(yè)務(wù),TIMESTAMP更為合適。

三、關(guān)于TIMESTAMP和DATETIME的自動(dòng)初始化和更新

首先,我們先看一下下面的操作

mysql> create table test(id int,hiredate timestamp);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test(id) values(1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test; +------+---------------------+
| id   | hiredate            |
+------+---------------------+
|    1 | 2015-12-08 14:34:46 |
+------+---------------------+
1 row in set (0.00 sec)

mysql> show create table test\G *************************** 1. row ***************************
       Table: test Create Table: CREATE TABLE `test` (
  `id` int(11) DEFAULT NULL,
  `hiredate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec)

看起來是不是有點(diǎn)奇怪,我并沒有對(duì)hiredate字段進(jìn)行插入操作,它的值自動(dòng)修改為當(dāng)前值,而且在創(chuàng)建表的時(shí)候,我也并沒有定義“show create table test\G”結(jié)果中顯示的“ DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP”。

其實(shí),這個(gè)特性是自動(dòng)初始化和自動(dòng)更新(Automatic Initialization and Updating)。

自動(dòng)初始化指的是如果對(duì)該字段(譬如上例中的hiredate字段)沒有顯性賦值,則自動(dòng)設(shè)置為當(dāng)前系統(tǒng)時(shí)間。

自動(dòng)更新指的是如果修改了其它字段,則該字段的值將自動(dòng)更新為當(dāng)前系統(tǒng)時(shí)間。

它與“explicit_defaults_for_timestamp”參數(shù)有關(guān)。

默認(rèn)情況下,該參數(shù)的值為OFF,如下所示:

mysql> show variables like '%explicit_defaults_for_timestamp%'; +---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | OFF   |
+---------------------------------+-------+
1 row in set (0.00 sec)

下面我們看看官檔的說明:

By default, the first TIMESTAMP column has both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP if neither is specified explicitly。

很多時(shí)候,這并不是我們想要的,如何禁用呢?

  1. 將“explicit_defaults_for_timestamp”的值設(shè)置為ON。

  2. “explicit_defaults_for_timestamp”的值依舊是OFF,也有兩種方法可以禁用

    1> 用DEFAULT子句該該列指定一個(gè)默認(rèn)值

    2> 為該列指定NULL屬性。

如下所示:

mysql> create table test1(id int,hiredate timestamp null);
Query OK, 0 rows affected (0.01 sec)

mysql> show create table test1\G *************************** 1. row ***************************
       Table: test1 Create Table: CREATE TABLE `test1` (
  `id` int(11) DEFAULT NULL,
  `hiredate` timestamp NULL DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec)

mysql> create table test2(id int,hiredate timestamp default 0);
Query OK, 0 rows affected (0.01 sec)

mysql> show create table test2\G *************************** 1. row ***************************
       Table: test2 Create Table: CREATE TABLE `test2` (
  `id` int(11) DEFAULT NULL,
  `hiredate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec)

在MySQL 5.6.5版本之前,Automatic Initialization and Updating只適用于TIMESTAMP,而且一張表中,最多允許一個(gè)TIMESTAMP字段采用該特性。從MySQL 5.6.5開始,Automatic Initialization and Updating同時(shí)適用于TIMESTAMP和DATETIME,且不限制數(shù)量。

參考:

  1. http://dev.mysql.com/doc/refman/5.6/en/datetime.html

  2. http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html

  3. http://www.2cto.com/database/201308/233832.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容