濮阳杆衣贸易有限公司

主頁 > 知識庫 > MySQL中datetime和timestamp的區(qū)別及使用詳解

MySQL中datetime和timestamp的區(qū)別及使用詳解

熱門標簽:常州網(wǎng)絡外呼系統(tǒng)開發(fā) 400電話申請信用卡 巫師三血與酒地圖標注 外呼系統(tǒng)電銷受騙 萊西市地圖標注 在哪里申請400電話 走過哪個省地圖標注 安徽ai電話電銷機器人有效果嗎 銷售語音電話機器人

一、MySQL中如何表示當前時間?

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

CURRENT_TIMESTAMP

CURRENT_TIMESTAMP()

NOW()

LOCALTIME

LOCALTIME()

LOCALTIMESTAMP

LOCALTIMESTAMP()

二、關(guān)于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更為合適。

三、關(guān)于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ù)有關(guā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。

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

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數(shù)據(jù)庫中datetime、bigint、timestamp來表示時間選擇,誰來存儲時間效率最高
  • Mysql中的Datetime和Timestamp比較
  • 淺談mysql導出表數(shù)據(jù)到excel關(guān)于datetime的格式問題
  • python3實現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)
  • mysql datetime查詢異常問題解決
  • MySql用DATE_FORMAT截取DateTime字段的日期值
  • MySQL時間字段究竟使用INT還是DateTime的說明
  • MySQL 5.6 中TIMESTAMP with implicit DEFAULT value is deprecated錯誤
  • mysql之TIMESTAMP(時間戳)用法詳解
  • MySQL錯誤TIMESTAMP column with CURRENT_TIMESTAMP的解決方法
  • 解析mysql中UNIX_TIMESTAMP()函數(shù)與php中time()函數(shù)的區(qū)別
  • MySQL 中 datetime 和 timestamp 的區(qū)別與選擇

標簽:來賓 陽江 黃石 河北 煙臺 赤峰 果洛 鞍山

巨人網(wǎng)絡通訊聲明:本文標題《MySQL中datetime和timestamp的區(qū)別及使用詳解》,本文關(guān)鍵詞  MySQL,中,datetime,和,timestamp,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL中datetime和timestamp的區(qū)別及使用詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于MySQL中datetime和timestamp的區(qū)別及使用詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    广饶县| 鄂托克前旗| 泸溪县| 洞头县| 仙游县| 西乌珠穆沁旗| 平江县| 剑河县| 巴中市| 兴义市| 金华市| 偃师市| 陇西县| 西丰县| 义马市| 金门县| 林口县| 珲春市| 道孚县| 丘北县| 临夏市| 吉安县| 辰溪县| 郸城县| 开原市| 上杭县| 栾川县| 铜鼓县| 贵德县| 易门县| 定结县| 化德县| 区。| 泽州县| 珲春市| 南涧| 临沧市| 平阴县| 乐安县| 平乐县| 富宁县|