TIMESTAMP和DATETIME相同和區別
目錄
TIMESTAMP和DATETIME的相同點:
1> 兩者都可用來表示YYYY-MM-DD HH:MM:SS[.fraction]型別的日期。
TIMESTAMP和DATETIME的不同點:
1> 兩者的儲存方式不一樣
對於TIMESTAMP,它把客戶端插入的時間從當前時區轉化為UTC(世界標準時間)進行儲存。查詢時,將其又轉化為客戶端當前時區進行返回。
而對於DATETIME,不做任何改變,基本上是原樣輸入和輸出。
下面,我們來驗證一下
首先建立兩種測試表,一個使用timestamp格式,一個使用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 | +------+---------------------+ row in set (0.01 sec) mysql> select * from test1; +------+---------------------+ | id | hiredate | +------+---------------------+ | 1 | 2015-12-08 00:00:00 | +------+---------------------+ row in set (0.00 sec)
其次修改當前會話的時區
mysql> show variables like '%time_zone%'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CST | | time_zone | SYSTEM | +------------------+--------+ 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 | +------+---------------------+ row in set (0.00 sec) mysql> select * from test1; +------+---------------------+ | id | hiredate | +------+---------------------+ | 1 | 2015-12-08 00:00:00 | +------+---------------------+ row in set (0.01 sec)
上述“CST”指的是MySQL所在主機的系統時間,是中國標準時間的縮寫,China Standard Time UT+8:00
通過結果可以看出,test中返回的時間提前了8個小時,而test1中時間則不變。這充分驗證了兩者的區別。
2> 兩者所能儲存的時間範圍不一樣
timestamp所能儲存的時間範圍為:'1970-01-01 00:00:01.000000' 到 '2038-01-19 03:14:07.999999'。
datetime所能儲存的時間範圍為:'1000-01-01 00:00:00.000000' 到 '9999-12-31 23:59:59.999999'。
總結:TIMESTAMP和DATETIME除了儲存範圍和儲存方式不一樣,沒有太大區別。當然,對於跨時區的業務,TIMESTAMP更為合適。
關於TIMESTAMP和DATETIME的自動初始化和更新
首先,我們先看一下下面的操作
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 |
+------+---------------------+
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
row in set (0.00 sec)
看起來是不是有點奇怪,我並沒有對hiredate欄位進行插入操作,它的值自動修改為當前值,而且在建立表的時候,我也並沒有定義“show create table test\G”結果中顯示的“ DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP”。
其實,這個特性是自動初始化和自動更新(Automatic Initialization and Updating)。
自動初始化指的是如果對該欄位(譬如上例中的hiredate欄位)沒有顯性賦值,則自動設定為當前系統時間。
自動更新指的是如果修改了其它欄位,則該欄位的值將自動更新為當前系統時間。
它與“explicit_defaults_for_timestamp”引數有關。
預設情況下,該引數的值為OFF,如下所示:
mysql> show variables like '%explicit_defaults_for_timestamp%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | OFF |
+---------------------------------+-------+
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。
很多時候,這並不是我們想要的,如何禁用呢?
1. 將“explicit_defaults_for_timestamp”的值設定為ON。
2. “explicit_defaults_for_timestamp”的值依舊是OFF,也有兩種方法可以禁用
1> 用DEFAULT子句該該列指定一個預設值
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
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
row in set (0.00 sec)
在MySQL 5.6.5版本之前,Automatic Initialization and Updating只適用於TIMESTAMP,而且一張表中,最多允許一個TIMESTAMP欄位採用該特性。從MySQL 5.6.5開始,Automatic Initialization and Updating同時適用於TIMESTAMP和DATETIME,且不限制數量。