博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL datetime && timestamp
阅读量:6898 次
发布时间:2019-06-27

本文共 3590 字,大约阅读时间需要 11 分钟。

hot3.png

MySQL datetime && timestamp

转自:

The temporal data types in MySQL can be confusing. Hopefully, this example and discussion will help to explain the differences in the timestamp and datetime data types.

From the MySQL reference:

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ‘YYYY-MM-DD HH:MM:SS’ format. The supported range is ‘1000-01-01 00:00:00′ to ‘9999-12-31 23:59:59′.

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of ‘1970-01-01 00:00:01′ UTC to ‘2038-01-19 03:14:07′ UTC.

A major difference between these two data types is that TIMESTAMP data type values are converted from current time zone to UTC for storage purpose and converted back from UTC to current time zone when used. The datetime data type values are unchanged in relation to time zone.

This example is a good exercise in demonstrating the difference between these two data types.

mysql> show variables like '%time_zone%';+------------------+---------------------+| Variable_name    |  Value              |+------------------+---------------------+| system_time_zone | India Standard Time || time_zone        | Asia/Calcutta       |+------------------+---------------------+2 rows in set (0.00 sec)

You can see our current time zone information. Under this environment, let us create a table with the two data types and populate it with the same temporal information.

create table datedemo( mydatetime datetime, mytimestamp timestamp);Query OK, 0 rows affected (0.05 sec)
insert into datedemo values ((now()), (now()));Query OK, 1 row affected (0.02 sec)
select * from datedemo;+---------------------+---------------------+| mydatetime          | mytimestamp         |+---------------------+---------------------+| 2011-08-21 14:11:09 | 2011-08-21 14:11:09 |+---------------------+---------------------+1 row in set (0.00 sec)

At this point the datetime and timestamp data types have remained the exact same values. Let us change the time zone see the results.

SET TIME_ZONE = "america/new_york";Query OK, 0 rows affected (0.00 sec)
 select * from datedemo;+---------------------+---------------------+| mydatetime          | mytimestamp         |+---------------------+---------------------+| 2011-08-21 14:11:09 | 2011-08-21 04:41:09 |+---------------------+---------------------+1 row in set (0.00 sec)

The above example shows how the TIMESTAMP date type changed the values after changing the time-zone to ‘america/new_work’ where DATETIME is unchanged.

=================================

datetime

1. 占用8个字节

2. 允许为空值,可以自定义值,系统不会自动修改其值。

3. 实际格式储存(Just stores what you have stored and retrieves the same thing which you have stored.)

4. 与时区无关(It has nothing to deal with the TIMEZONE and Conversion.)

5. 不可以设定默认值,所以在不允许为空值的情况下,必须手动指定datetime字段的值才可以成功插入数据。

6. 可以在指定datetime字段的值的时候使用now()变量来自动插入系统的当前时间。

结论:datetime类型适合用来记录数据的原始的创建时间,因为无论你怎么更改记录中其他字段的值,

datetime字段的值都不会改变,除非你手动更改它。

timestamp

1. 占用4个字节

2. 允许为空值,但是不可以自定义值,所以为空值时没有任何意义。

3. TIMESTAMP值不能早于1970或晚于2037。这说明一个日期,例如'1968-01-01',虽然对于DATETIME或DATE值是有效的,但对于TIMESTAMP值却无效,如果分配给这样一个对象将被转换为0。

4.值以UTC格式保存( it stores the number of milliseconds)

5.时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区。

6. 默认值为CURRENT_TIMESTAMP(),其实也就是当前的系统时间。

7. 数据库会自动修改其值,所以在插入记录时不需要指定timestamp字段的名称和timestamp字段的值,你只需要在设计表的时候添加一个timestamp字段即可,插入后该字段的值会自动变为当前系统时间。

8. 以后任何时间修改表中的记录时,对应记录的timestamp值会自动被更新为当前的系统时间。

结论:timestamp类型适合用来记录数据的最后修改时间,因为只要你更改了记录中其他字段的值,timestamp字段的值都会被自动更新。

================END================

转载于:https://my.oschina.net/xinxingegeya/blog/360050

你可能感兴趣的文章
python:threading多线程模块-Condition实现复杂的同步
查看>>
Centos7使用YUM进行install或update出现KeyboardInterrupt错误
查看>>
网络扫描工作zenmap
查看>>
Nginx技巧:灵活的server_name
查看>>
嵌入式开发那点事之一
查看>>
css直接画方格
查看>>
Qt多个信号连接到一个槽,在槽中识别信号的发送者方法
查看>>
IOS学习笔记2—Objective C—类、属性、方法
查看>>
How to hack windows 8 using kali linux
查看>>
人生的过程何尝不是在等车和转乘
查看>>
我的友情链接
查看>>
foolscap实现rpc(一)
查看>>
NFS共享服务搭建
查看>>
Oracle表空间数据文件移动
查看>>
我的友情链接
查看>>
程序状态字(PSW)的动画说明,
查看>>
shell学习之expect命令
查看>>
Python中的特殊变量名
查看>>
20个你可能不知道的 Linux 网络工具
查看>>
我的友情链接
查看>>