SQL unix 时间戳的字段类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1993509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:54:49  来源:igfitidea点击:

field type for unix timestamp

sql

提问by Megaman

Whats the best field type to use for unix timestamps?

用于 unix 时间戳的最佳字段类型是什么?

Will int(10)be enough for a while?

一会int(10)就够了吗?

回答by Michael Petrotta

Unix time_tis either 32 bits wide, or 64. So, int(8)or binary(8)is sufficient, at least for the next 293 billion years.

Unixtime_t要么是 32 位宽,要么是 64 位。所以,int(8)或者binary(8)足够了,至少在接下来的 2930 亿年里。

回答by bobince

The number in a MySQL INT(n)datatype doesn't specify how much storage space is reserved, it's a display-width for formatting purposes only. As such an INT(10)is the same as a plain INTEGER, that is to say a 32-bit signed number.

MySQLINT(n)数据类型中的数字不指定保留多少存储空间,它只是用于格式化目的的显示宽度。因此 anINT(10)与一个 plain 相同INTEGER,即一个 32 位有符号数。

So this is certainly an appropriate datatype for a 32-bit Unix timestamp. But if you want 64-bit timestamps it's not going to be enough; you would have to use a BIGINT.

所以这当然是 32 位 Unix 时间戳的合适数据类型。但是,如果您想要 64 位时间戳,这还不够;你将不得不使用一个BIGINT.

回答by CodeStock

For Unix timestamp you can easily use INT(4) UNSIGNEDwhich max value is 4294967295. It's far enough for you to store time()values for the next ~133 years. If your app will stop working because of this, you will be long dead ;)

对于 Unix 时间戳,您可以轻松使用INT(4) UNSIGNED最大值为4294967295. 这足以让您存储time()未来约 133 年的价值。如果您的应用程序因此而停止工作,您将死掉很久;)

You can also try to use TIMESTAMP data type, which is less problematic and when you want to convert it to Unix timestamp you can use UNIX_TIMESTAMP() function.

您也可以尝试使用 TIMESTAMP 数据类型,它的问题较少,当您想将其转换为 Unix 时间戳时,您可以使用 UNIX_TIMESTAMP() 函数。

ex.

前任。

SELECT UNIX_TIMESTAMP(col_timestamp) FROM tbl_name;

回答by Tatu Ulmanen

For timestamps, you should use the TIMESTAMPor DATETIMEfield type.

对于时间戳,您应该使用TIMESTAMPorDATETIME字段类型。