当前位置:数据库 > MySQL >>

MySQL中datetime与timestamp用法比较


区别对比


储存

TIMESTAMP

1.4个字节储存(Time stamp value is stored in 4 bytes)

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

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

datetime

1.8个字节储存(8 bytes storage)

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

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


总结

相同点

都可以用于表示时间
都呈字符串显示

不同点

顾名思义,date只表示’YYYY-MM-DD’形式的日期,datetime表示’YYYY-MM-DD HH:mm:ss’形式的日期加时间,timestamp与datetime显示形式一样。
date和datetime可表示的时间范围为’1000-01-01’到’9999-12-31’,timestamp由于受32位int型的限制,能表示’1970-01-01 00:00:01’到’2038-01-19 03:14:07’的UTC时间。
mysql在存储timestamp类型时会将时间转为UTC时间,然后读取的时候再恢复成当前时区。假如你存储了一个timestamp类型的值之后,修改了mysql的时区,当你再读取这个值时就会得到一个错误的时间。而这种情况在date和datetime中不会发生。
 
timestamp类型提供了自动更新的功能,你只需要将它的默认值设置为CURRENT_TIMESTAMP。
除了date是保留到天,datetime和timestamp都保留到秒,而忽略毫秒。

补充:数据库,mysql教程 
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,