我应该在 MySQL 中使用大 INT 还是常规 INT 来存储时间戳?

发布于 2024-08-17 10:01:47 字数 84 浏览 2 评论 0原文

我应该在 MySQL 中使用大整数还是常规整数来存储时间戳?我计划将其存储在 INT 中,而不是内置时间戳或日期时间中,那么我应该使用哪种 INT 类型?

Should I be using a big integer or a regular integer in MySQL to store a timerstamp in? I plan on storing it in an INT and not the built in timestamp or datetime so which INT type should I use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白云不回头 2024-08-24 10:01:47

Int 将在 2038 年变为负值(如果您使用 UNIX 时间戳): http://en.wikipedia .org/wiki/2038_problem

所以 BIGINT 可能是最安全的选择

Int would roll over to a negative in 2038 (if you are using UNIX timestamp): http://en.wikipedia.org/wiki/2038_problem.

so BIGINT is probably the safest choice

缘字诀 2024-08-24 10:01:47

您应该将其存储在时间戳中,因为这很可能是 DBMS 针对时间戳数据进行优化的内容。

我很好奇为什么你会牺牲 MySQL 开发人员为使时间戳按其应有的方式工作而付出的所有辛勤工作,并用几乎肯定无法正常工作的东西来替换它。

由于您没有说明为什么要使用整数,我只是假设这是暂时的精神错乱,您很快就会恢复:-)

You should be storing it in a timestamp since that's most likely what the DBMS will optimize for timestamp data.

I'm curious as to why you would sacrifice all the hard work that the MySQL developers have put into making timestamps work the way they should, and replacing it with something that will almost certainly not work as well.

Since you don't state why you want to use an integer, I'm just going to assume it was temporary insanity and that you'll soon recover :-)

爱的十字路口 2024-08-24 10:01:47

我认为这完全取决于你想做什么。有一些适合时间/日期类型的类型(请参阅:http://dev.mysql.com/doc/refman/5.0/en/date-and-time-type-overview.html)

  • DATE - 通常为 3 个字节,范围:1000-01-019999-12-31
  • DATETIME - 8 个字节,范围 1000-01-01 00:00:009999-12-31 23:59:59
  • TIMESTAMP - 4 字节范围 1970-01-01 00:00:01 UTC 到 2038-01-19 03:14:07 UTC。
    然后,有一些语义问题需要注意:

(int 是 4 个字节,如 TIMESTAMP,bigint 是 8 个字节,如 DATETIME)

  • 日期存储日历天,日期时间和时间戳都以秒精度存储日期+时间(亚秒精度是支持日期算术,但不支持存储)
  • 时间戳存储 UTC 值。也就是说,您粘贴的任何值都会从会话的时区(默认情况下为服务器的时区)转换为 UTC 并存储。检索时,UTC 值会再次转换回会话中有效的时区。
  • 可以声明时间戳以在插入或更新或两者时自动获取当前时间戳。您最好阅读手册以了解详细信息(请参阅 http://dev.mysql .com/doc/refman/5.1/en/timestamp.html

I think it entirely depends on what you want to do. There a few proper types for time/date types (see: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-type-overview.html)

  • DATE - typically 3 bytes, range: 1000-01-01 to 9999-12-31.
  • DATETIME - 8 bytes, range 1000-01-01 00:00:00 to 9999-12-31 23:59:59
  • TIMESTAMP - 4 bytes range 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC.
    Then, there are some semantic issues to be aware of:

(int is 4 bytes like TIMESTAMP, bigint is 8 bytes like DATETIME)

  • date stores calendar days, datetime and timestamp both store date+time at a second precision (sub-second precision is supported in date arithmetic, but not for storage)
  • timestamp stores an UTC value. That is, any value you stick in there is converted from the session's timezone (by default, the server's timezone) to UTC and stored. On retrieval, the UTC value is converted back again to the timezone in effect in the session.
  • timestamp can be declared to automatically get the current timestamp on insert or update or both. you best study the manual for the details (see http://dev.mysql.com/doc/refman/5.1/en/timestamp.html)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文