MySQL中datetime和timestamp的區(qū)別及使用詳解
一、MySQL中如何表示當前時間?
其實,表達方式還是蠻多的,匯總?cè)缦拢?/p>
CURRENT_TIMESTAMP
CURRENT_TIMESTAMP()
NOW()
LOCALTIME
LOCALTIME()
LOCALTIMESTAMP
LOCALTIMESTAMP()
二、關于TIMESTAMP和DATETIME的比較
一個完整的日期格式如下:YYYY-MM-DD HH:MM:SS[.fraction],它可分為兩部分:date部分和time部分,其中,date部分對應格式中的“YYYY-MM-DD”,time部分對應格式中的“HH:MM:SS[.fraction]”。對于date字段來說,它只支持date部分,如果插入了time部分的內(nèi)容,它會丟棄掉該部分的內(nèi)容,并提示一個warning。
如下所示:
mysql> create table test(id int,hiredate date); Query OK, 0 rows affected (0.01 sec) 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> show warning; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'warning' at line 1 mysql> select * from test; +------+------------+ | id | hiredate | +------+------------+ | 1 | 2015-12-08 | | 1 | 2015-12-08 | +------+------------+ 2 rows in set (0.00 sec)
注:第一個沒提示warning的原因在于它的time部分都是0
TIMESTAMP和DATETIME的相同點:
1> 兩者都可用來表示YYYY-MM-DD HH:MM:SS[.fraction]類型的日期。
TIMESTAMP和DATETIME的不同點:
1> 兩者的存儲方式不一樣
對于TIMESTAMP,它把客戶端插入的時間從當前時區(qū)轉(zhuǎn)化為UTC(世界標準時間)進行存儲。查詢時,將其又轉(zhuǎn)化為客戶端當前時區(qū)進行返回。
而對于DATETIME,不做任何改變,基本上是原樣輸入和輸出。
下面,我們來驗證一下
首先創(chuàng)建兩種測試表,一個使用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 | +------+---------------------+ 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)
兩者輸出是一樣的。
其次修改當前會話的時區(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所在主機的系統(tǒng)時間,是中國標準時間的縮寫,China Standard Time UT+8:00
通過結(jié)果可以看出,test中返回的時間提前了8個小時,而test1中時間則不變。這充分驗證了兩者的區(qū)別。
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'。
總結(jié):TIMESTAMP和DATETIME除了存儲范圍和存儲方式不一樣,沒有太大區(qū)別。當然,對于跨時區(qū)的業(yè)務,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 | +------+---------------------+ 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)
看起來是不是有點奇怪,我并沒有對hiredate字段進行插入操作,它的值自動修改為當前值,而且在創(chuàng)建表的時候,我也并沒有定義“show create table test\G”結(jié)果中顯示的“ DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP”。
其實,這個特性是自動初始化和自動更新(Automatic Initialization and Updating)。
自動初始化指的是如果對該字段(譬如上例中的hiredate字段)沒有顯性賦值,則自動設置為當前系統(tǒng)時間。
自動更新指的是如果修改了其它字段,則該字段的值將自動更新為當前系統(tǒng)時間。
它與“explicit_defaults_for_timestamp”參數(shù)有關。
默認情況下,該參數(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。
很多時候,這并不是我們想要的,如何禁用呢?
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 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,而且一張表中,最多允許一個TIMESTAMP字段采用該特性。從MySQL 5.6.5開始,Automatic Initialization and Updating同時適用于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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
MySQL遞歸sql語句WITH表達式實現(xiàn)方法代碼
SQL遞歸查詢語句是指通過遞歸方式對數(shù)據(jù)進行查詢的語句,下面這篇文章主要給大家介紹了關于MySQL遞歸sql語句WITH表達式實現(xiàn)的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-01-01登錄MySQL時出現(xiàn)Authentication plugin ‘caching_sha2_pass
這篇文章主要介紹了登錄MySQL時出現(xiàn)Authentication plugin ‘caching_sha2_password‘ reported error錯誤的解決方案,文中通過圖文結(jié)合的形式講解的非常詳細,對大家的解決問題有一定的幫助,需要的朋友可以參考下2024-12-12mysql實現(xiàn)將data文件直接導入數(shù)據(jù)庫文件
這篇文章主要介紹了mysql實現(xiàn)將data文件直接導入數(shù)據(jù)庫文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03MySQL數(shù)據(jù)庫使用mysqldump導出數(shù)據(jù)詳解
mysqldump是mysql用于轉(zhuǎn)存儲數(shù)據(jù)庫的實用程序。它主要產(chǎn)生一個SQL腳本,其中包含從頭重新創(chuàng)建數(shù)據(jù)庫所必需的命令CREATE TABLE INSERT等。接下來通過本文給大家介紹MySQL數(shù)據(jù)庫使用mysqldump導出數(shù)據(jù)詳解,需要的朋友一起學習吧2016-04-04