• mysql的日期存储字段比较int,datetime,timestamp区别


    1、首先是我们分析datetime长度是8个字节,INT的长度是4个字节,存储空间上比datatime少。

    2、int存储索引的空间也比datetime少,排序效率高,查询速度比较快。

    3、方便计算,unix时间戳有点就是方便计算。

    总结了一下timestamp和datetime的区别:
    1、timestamp有范围限制1970 ~ 2037
    2、timestamp支持default current_timestamp 来设置默认自动当前时间
    3、timestamp支持on update current_timestamp 来设置更新时自动当前时间
    4、timestamp时区相关,存储时以UTC时间保持,查询时转换为当前时区,
    即如果在东8区的08:00:00分保存的数据,在东9区看到的是09:00:00
    5、timestamp 4个字节存储(实际上就是int),datetime 8个字节
    6、如果timestamp的值超出范围,mysql不会报错
    7、如果是自动更新模式,手动修改数据导致timestamp字段更新
    8、同时有两个timestamp字段默认值为current_timestamp会报错
    9、如果mysql在maxdb模式下,timestamp等同于datetime,也就是说timestamp
    受服务器模式影响

    网上还有还一个TIMESTAMP 和INT比较的例子,timestamp效率比int不会差,而且timestamp有自动更新功能

    相同点
    TIMESTAMP列的显示格式与datetime列相同。
    换句话说,显示宽度固定在19字符,并且格式为YYYY-MM-DD HH:MM:SS。

    不同点
    datetime
    以”YYYY-MM-DDHH:MM:SS”格式检索和显示datetime值。支持的范围为”1000-01-01 00:00:00″到”9999-12-31 23:59:59″
    TIMESTAMP值不能早于1970或晚于2037
    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 thesamething which you have stored.)
    3、与时区无关(It has nothing to deal with the TIMEZONEandConversion.)

  • 相关阅读:
    Android自定义drawable(Shape)详解
    如何设置对话框的宽度和高度
    Android资料之-EditText中的inputType
    android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效的解决方法
    android ScrollView 充满屏幕
    治疗神经衰弱最有效的方法和药物是什么
    交换机和路由器的区别
    小众编程语言同样值得你关注
    RotateAnimation详解
    你可能没听过的 Java 8 中的 10 个特性
  • 原文地址:https://www.cnblogs.com/guangxiaoluo/p/4905389.html
Copyright © 2020-2023  润新知