之前在做项目的时候,有学长看到我设计数据库的时候时间存储用timestamp,他提醒我尽量不要用timestamp,而是选用datatime,当时没注意这个问题,今天我又遇到了这个问题,决定弄明白它。
对比
| timestamp | datetime | |
|---|---|---|
| 存储所占的字节数 | 4 | 8 | 
| 是否受时区影响 | 受影响 | 不受影响 | 
| 表示的范围不同 | 不能早于1970年或晚于2037年 | ‘1000-01-01 00:00:00’到’9999-12-31 23:59:59’ | 
| 存储格式 | 以UTC格式保存(it stores the number of milliseconds) | 实际格式储存(Just stores what you have stored and retrieves the same thing which you have stored.) | 
验证
在某个数据库连接上修改时区

在某个数据库连接上修改时区,不会影响其他的连接,下面继续验证

在数据库连接A中,修改时区为 set time_zone='+9:00' (我处于东八区)
接下来,另开一个数据库连接B,查看当前时间

可以看出,时间并没有变到东九区
 
          