MySQL中有关TIMESTAMP和DATETIME的总结

转载自iVictor,原文链接:http://www.cnblogs.com/ivictor/p/5028368.html

一、MySQL中如何表示当前时间?

其实,表达方式还是蛮多的,汇总如下:

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部分的内容,它会丢弃掉该部分的内容,并提示一个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,它把客户端插入的时间从当前时区转化为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 |
+------+---------------------+
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)

两者输出是一样的。

其次修改当前会话的时区

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所在主机的系统时间,是中国标准时间的缩写,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 |
+------+---------------------+
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字段进行插入操作,它的值自动修改为当前值,而且在创建表的时候,我也并没有定义“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 |
+---------------------------------+-------+
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,且不限制数量。

参考:

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

3. http://www.2cto.com/database/201308/233832.html

MySQL中datetime和timestamp的区别及使用的更多相关文章

  1. MySQL 中 datetime 和 timestamp 的区别与选择

    MySQL 中常用的两种时间储存类型分别是datetime和 timestamp.如何在它们之间选择是建表时必要的考虑.下面就谈谈他们的区别和怎么选择. 1 区别 1.1 占用空间 类型 占据字节 表 ...

  2. mysql 中 datetime和 timestamp的区别

    DATETIME日期和时间的组合.支持的范围是'1000-01-01 00:00:00'到'9999-12-31 23:59:59'.MySQL以'YYYY-MM-DD HH:MM:SS'格式显示DA ...

  3. mysql中datetime和timestamp的区别

    原文地址:http://database.51cto.com/art/200905/124240.htm 相同 显示 TIMESTAMP列的显示格式与DATETIME列相同.换句话说,显示宽度固定在1 ...

  4. SQL中datetime和timestamp的区别

    在开发一个简单的报名程序时,要求在每一条新插入的记录后面添加一个日期字段,方便日后查询和排序.于是立即百度,发现可以使用datetime或timestamp两种日期类型来实现.这对于爱纠结的我来说是不 ...

  5. Mysql中datetime和timestamp区别

    DATETIME日期和时间的组合.支持的范围是'1000-01-01 00:00:00'到'9999-12-31 23:59:59'.MySQL以'YYYY-MM-DD HH:MM:SS'格式显示DA ...

  6. mysql中datetime与timestamp的比较

    相同 显示 TIMESTAMP列的显示格式与DATETIME列相同.换句话说,显示宽度固定在19字符,并且格式为YYYY-MM-DD HH:MM:SS. 不同 范围 datetime 以'YYYY-M ...

  7. Mysql - date、datetime、timestamp 的区别

    date.datetime 的区别 顾名思义,date 日期,datetime 日期时间,所以 date 是 datetime 的日期部分 MySQL 以 YYYY-MM-DD hh:mm:ss 格式 ...

  8. MySQL中 utf8与utf8mb4的区别

    MySQL中 utf8与utf8mb4的区别 一.简介 ​ MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode.好在 ...

  9. 用count(*)还是count(列名) || Mysql中的count()与sum()区别

    Mysql中的count()与sum()区别   首先创建个表说明问题 CREATE TABLE `result` (   `name` varchar(20) default NULL,   `su ...

随机推荐

  1. Unity3d 镜面折射 vertex and frag Shader源代码

    Unity3d 镜面折射  网上能找到的基本上是固定管道或表面渲染的shader. 特此翻译为顶点.片段渲染的Shader, 本源代码仅仅涉及shader与cs部分, 请自行下载NGUI  unity ...

  2. Git Gui 查看分支历史的时候中文显示乱码

    如图所示 解决方案1 在Git Gui工具栏上选择-编辑-选项: 选择:Default File Contents Encoding, change为UTF-8 成功: 解决方案2  C:\Users ...

  3. [vue]v-bind: sytle/class-bind&属性值绑定

    v-bind - style绑定 - class绑定 - 属性值绑定 <!DOCTYPE html> <html lang="en"> <head&g ...

  4. 逻辑运算,&,&&, |, ||, ^, !

    &:与  特点:判断时两边为true才为true,只要两边有一个为false则结果为false:true&true=true: false&true=false: true&a ...

  5. MVC html.beginform & ajax.beginform

    1.指定表单提交方式和路径等 @using (Html.BeginForm("Index", "Home", FormMethod.Get, new { nam ...

  6. Django的基本开发环境配置和MTV模型

    一.MTV模型 Django的MTV分别代表: Model(模型):负责业务对象与数据库的对象(ORM) Template(模版):负责如何把页面展示给用户 View(视图):负责业务逻辑,并在适当的 ...

  7. redis环境搭建与配置

    通过初始化脚本启动redis 1.将redis源码的utils文件夹下面有的redis_init_script复制到/etc/init.d/redis_端口号下面. 带密码的实例 REQUIRED_P ...

  8. python chunk模块

    chunk模块用于读取TIFF格式的文件,打开应该使用二进制模式 TIFF 标签图像文件格式 import chunk import chunk f=open('E:\\test.tiff','rb' ...

  9. mysql错误日志与通用日志

    错误日志 MySQL错误日志是记录MySQL 运行过程中较为严重的警告和错误信息,以及MySQL每次启动和关闭的详细信息. 1.错误日志路径查询 show variables like '%log_e ...

  10. Spring 问题总结

    Spring问答Top 25:http://www.importnew.com/15851.html [Java面试五]Spring总结以及在面试中的一些问题.:http://www.cnblogs. ...