mysql timestamp year_MySql中datetime与timestamp的区别

本篇文章是基于MySql5.7的。

概述

MySql中与日期时间相关的共有5种类型,分别是DATE,TIME,DATETIME,TIMESTAMP,YEAR,它们分别对应的关系如下表所示:

Data Type

“Zero” Value

DATE

'0000-00-00'

TIME

'00:00:00'

DATETIME

'0000-00-00 00:00:00'

TIMESTAMP

'0000-00-00 00:00:00'

YEAR

0000

Year

在旧版本中,Year表示的年,有两种类型,分别是year(2),year(4),表示形式:yy,yyyy。其中4位的即使我们常用的年。

但在MySql5.7中已经不支持yy这中形式了,只支持4位的了,所以即使你输入两位,数据库也会自动转为4位。而Mysql中year字段的范围也改为了1901~2155。转换的范围是:

Year values in the range 70-99 are converted to 1970-1999.

Year values in the range 00-69 are converted to 2000-2069.

datetime和timestamp

其中,datetime和timestamp这两种类型都是用于表示YYYY-MM-DD HH:MM:SS 这种年月日时分秒格式的数据,但两者还是有些许不同之处的。

存储范围不同:datetime的存储范围是 1000-01-01 00:00:00.000000 到 9999-12-31 23:59:59.999999,而timestamp的范围是 1970-01-01 00:00:01.000000 到 2038-01-19 03:14:07.999999;

datetime存储与时区无关,而timestamp存储的是与时区有关,这也是两者最大的不同。MySql在存储timestamp时会先将时间转为UTC(世界协调时)进行存储,然后查询的时候再从UTC转为当前的时区进行返回。也就是说使用timestamp进行存储的时间返回的时候会随着数据库的时区而发生改变。而datetime的存储则与时区无关,数据是什么,就存储什么,也就返回什么。

datetime适用于记录数据的创建时间,因为这个时间是不会变的。而timestamp有自动修改更新的功能,也就是说,我们对表里的其他数据进行修改,timestamp修饰的字段会自动更新为当前的时间,这个特性称为自动初始化和自动更新(Automatic Initialization and Updating)。所以timestamp适用于那种记录数据的最后修改时间的字段。当然,我们也可以设置timestamp不自动更新,通过设置 explicit_defaults_for_timestamp配置,从OFF设置为ON 来实现。

从MySql5.6.5之后,Automatic Initialization and Updating这种特性不但适用于timestamp,也适用于datetime了。并且以前MySql版本要求同一张表中满足该特性的timestamp只能有一个字段,而现在也不再限制数量了。

下面我们通过一个简单的例子来查看一下:

我们以表test_date中的create字段和update字段来进行比较,并且我们使用命令行来进行操作。

查看表结构:

mysql> show create table test_date;

+-----------+-------------------------------------------------------+

| Table | Create Table

+-----------+-------------------------------------------------------+

| test_date | CREATE TABLE `test_date` (

`id` int(11) NOT NULL,

`create` datetime DEFAULT NULL,

`update` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

+-----------+----------------------------------------------------+

1 row in set (0.00 sec)

查看表中数据:

mysql> select * from test_date;

+----+---------------------+---------------------+

| id | create | update |

+----+---------------------+---------------------+

| 1 | 2018-02-02 22:38:20 | 2018-02-02 22:38:20 |

+----+---------------------+---------------------+

1 row in set (0.00 sec)

看可以看到,create和update两个字段的值是一样的。

修改当前会话的时区:

mysql> show variables like '%time_zone%';

+------------------+--------+

| Variable_name | Value |

+------------------+--------+

| system_time_zone | |

| time_zone | +00:00 |

+------------------+--------+

2 rows in set, 1 warning (0.00 sec)

mysql> set time_zone='-3:00';

Query OK, 0 rows affected (0.00 sec)

mysql> select * from test_date;

+----+---------------------+---------------------+

| id | create | update |

+----+---------------------+---------------------+

| 1 | 2018-02-02 22:38:20 | 2018-02-02 19:38:20 |

+----+---------------------+---------------------+

1 row in set (0.00 sec)

注意两点:

时区信息是MySql的系统变量,我们可以通过 show variables like '%time_zone%' 来获取mysql的时区信息,默认值一般是 SYSTEM,即服务器的时区。

修改当前会话的时区只会影响到当前连接,如果再开一个连接,时区将仍是mysql默认时区。如果要修改整个mysql的时区,可以有多种方式,比如修改my.ini配置等,然后记得重启即可。

从以上例子,我们可以大致看出datetime与timestamp的不同了。

另外在说一点,MySql也提供了查询当前UTC的日期和时间,可以通过UTC_TIMESTAMP方法来查询:

mysql> select utc_timestamp(),utc_timestamp()+0;

+---------------------+-------------------+

| utc_timestamp() | utc_timestamp()+0 |

+---------------------+-------------------+

| 2018-02-05 01:08:03 | 20180205010803 |

+---------------------+-------------------+

1 row in set (0.03 sec)

针对以上一些特性,看一下官方文档,顺便学一下英文:

datetime与timestamp范围:

the range for DATETIME values is '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999', and the range for TIMESTAMP values is '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999';

datetime与timestamp存储:

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.) By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis. As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions. The current time zone is available as the value of the time_zone system variable

新版本之后的一些变化:

As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and for at most one TIMESTAMP column per table. The following notes first describe automatic initialization and updating for MySQL 5.6.5 and up, then the differences for versions preceding 5.6.5.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值