跳到主要内容

TIMESTAMP 的时间范围

· 阅读需 2 分钟

执行 sql 报错。

UPDATE inspection_plans t SET t.plan_end_time = '2041-04-08 10:41:03' WHERE t.id = 1

错误信息:

Data truncation: Incorrect datetime value: '2041-04-08 10:41:03' for column 'plan_end_time' at row 1

问题原因:TIMESTAMP 的范围是 '1970-01-01 00:00:01' UTC '2038-01-19 03:14:07' UTC

解决方法:将 TIMESTAMP 改为 DATETIME 类型。

alter table inspection_plans modify plan_end_time datetime;

总结:

  • DATE 能够表示的时间范围:1000-01-019999-12-31 ,只记录年月日,不存储时分秒
  • TIME 能够表示的时间范围:'-838:59:59''838:59:59' ,只记录时分秒,不存储年月日
  • DATETIME 能够表示的时间范围:1000-01-01 00:00:009999-12-31 23:59:59 推荐
  • TIMESTAMP 能够表示的时间范围:1970-01-01 00:00:01 UTC2038-01-19 03:14:07 UTC
  • YEAR 能够表示的时间范围:19012155
  • TIMESTAMPDATETIME 的区别:
    • TIMESTAMP 是 UTC 时间,存储时会转换为 UTC 时间,取出时会转换为当前时区的时间
    • DATETIME 是本地时间,存储时不会转换为 UTC 时间,取出时也不会转换为 UTC 时间