校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁(yè) > 知識(shí)庫(kù) > MySQL中datetime和timestamp的區(qū)別及使用詳解

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

熱門(mén)標(biāo)簽:常州網(wǎng)絡(luò)外呼系統(tǒng)開(kāi)發(fā) 400電話申請(qǐng)信用卡 巫師三血與酒地圖標(biāo)注 外呼系統(tǒng)電銷受騙 萊西市地圖標(biāo)注 在哪里申請(qǐng)400電話 走過(guò)哪個(gè)省地圖標(biāo)注 安徽ai電話電銷機(jī)器人有效果嗎 銷售語(yǔ)音電話機(jī)器人

一、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字段來(lái)說(shuō),它只支持date部分,如果插入了time部分的內(nèi)容,它會(huì)丟棄掉該部分的內(nèi)容,并提示一個(gè)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)

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

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

1> 兩者都可用來(lái)表示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,不做任何改變,基本上是原樣輸入和輸出。

下面,我們來(lái)驗(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)

兩者輸出是一樣的。

其次修改當(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í)間,是中國(guó)標(biāo)準(zhǔn)時(shí)間的縮寫(xiě),China Standard Time UT+8:00

通過(guò)結(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ǔ)方式不一樣,沒(méi)有太大區(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)

看起來(lái)是不是有點(diǎn)奇怪,我并沒(méi)有對(duì)hiredate字段進(jìn)行插入操作,它的值自動(dòng)修改為當(dāng)前值,而且在創(chuàng)建表的時(shí)候,我也并沒(méi)有定義“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字段)沒(méi)有顯性賦值,則自動(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)

下面我們看看官檔的說(shuō)明:

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開(kāi)始,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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Mysql數(shù)據(jù)庫(kù)中datetime、bigint、timestamp來(lái)表示時(shí)間選擇,誰(shuí)來(lái)存儲(chǔ)時(shí)間效率最高
  • Mysql中的Datetime和Timestamp比較
  • 淺談mysql導(dǎo)出表數(shù)據(jù)到excel關(guān)于datetime的格式問(wèn)題
  • python3實(shí)現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)
  • mysql datetime查詢異常問(wèn)題解決
  • MySql用DATE_FORMAT截取DateTime字段的日期值
  • MySQL時(shí)間字段究竟使用INT還是DateTime的說(shuō)明
  • MySQL 5.6 中TIMESTAMP with implicit DEFAULT value is deprecated錯(cuò)誤
  • mysql之TIMESTAMP(時(shí)間戳)用法詳解
  • MySQL錯(cuò)誤TIMESTAMP column with CURRENT_TIMESTAMP的解決方法
  • 解析mysql中UNIX_TIMESTAMP()函數(shù)與php中time()函數(shù)的區(qū)別
  • MySQL 中 datetime 和 timestamp 的區(qū)別與選擇

標(biāo)簽:來(lái)賓 陽(yáng)江 黃石 河北 煙臺(tái) 赤峰 果洛 鞍山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL中datetime和timestamp的區(qū)別及使用詳解》,本文關(guān)鍵詞  MySQL,中,datetime,和,timestamp,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL中datetime和timestamp的區(qū)別及使用詳解》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于MySQL中datetime和timestamp的區(qū)別及使用詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 启东市| 额尔古纳市| 长治市| 荣昌县| 宜兰市| 无锡市| 嘉义县| 金门县| 西昌市| 唐山市| 贵阳市| 来凤县| 汉沽区| 得荣县| 都匀市| 宜川县| 保德县| 锡林浩特市| 宕昌县| 榆社县| 新干县| 陇南市| 南宫市| 江阴市| 金坛市| 古交市| 盐津县| 霍城县| 礼泉县| 甘洛县| 托里县| 准格尔旗| 乐平市| 泽库县| 深圳市| 米林县| 无极县| 阜阳市| 尤溪县| 曲沃县| 布尔津县|