将“year”列定义为Integer或String类型是否更好?

时间:2022-12-21 12:33:03

I do realize that it is better for a column to be an Integer if one has to perform mathematical calculations on it.

我确实意识到如果一个列必须对它进行数学计算,那么它最好是一个整数。

I probably have to perform mathematical calculations on the "year" column but minimally. So would it be better to store it as a String or Integer?

我可能不得不在“年”栏上进行数学计算,但最低限度。那么将它存储为String或Integer会更好吗?

Thanks.

3 个解决方案

#1


3  

Save it as an integer.

将其保存为整数。

While there may be some application where you are reading and serving this data so frequently that the int->string conversion is a problem... that is going to be an edge case.

虽然可能有一些应用程序,您正在经常阅读和提供此数据,以至于int->字符串转换是一个问题...这将是一个边缘情况。

On the other side

另一方面

  • Integers provide smaller options than strings in data storage (such as TINYINT)
  • 整数提供比数据存储中的字符串更小的选项(例如TINYINT)

  • You avoid conversions due to math
  • 您可以避免因数学而导致的转化

  • It's going to confuse/annoy/frustrate all the developers that come after you when they query a data type that is naturally a number and get a string.
  • 当他们查询自然是数字并获得字符串的数据类型时,会让所有开发人员感到困惑/烦恼/沮丧。

#2


3  

If you are not expecting your YEAR variable to ever contain non-digit values then yes you should store it as a number.

如果您不希望YEAR变量包含非数字值,则应将其存储为数字。

I would not store it as INT since I doubt year will reach the limit that INT has to offer. I would save it as SMALLINT or even TINYINT either should be unsigned.

我不会把它作为INT存储,因为我怀疑一年会达到INT提供的限制。我会将它保存为SMALLINT,甚至TINYINT都应该是无符号的。

SMALLINT UNSIGNED gives you max value of 65535, unless you are storing years that exceed the year 65535 this should suffice.

SMALLINT UNSIGNED为您提供最大值65535,除非您存储超过65535年的年份,这应该足够了。

#3


0  

You could go crazy and save it as a YEAR!

你可能会发疯并将它保存为一年!

This limits you to 1901-2155.

这限制你到1901-2155。

If this is too restrictive, I prefer a CHAR(4) to an INT; MySQL DATETIME comparisons are done in a string like manner..

如果限制太多,我更喜欢CHAR(4)和INT; MySQL DATETIME比较以类似字符串的方式完成。

You can do things like

你可以做的事情

WHERE year < CURDATE()

without worries then.

没有后顾之忧。

#1


3  

Save it as an integer.

将其保存为整数。

While there may be some application where you are reading and serving this data so frequently that the int->string conversion is a problem... that is going to be an edge case.

虽然可能有一些应用程序,您正在经常阅读和提供此数据,以至于int->字符串转换是一个问题...这将是一个边缘情况。

On the other side

另一方面

  • Integers provide smaller options than strings in data storage (such as TINYINT)
  • 整数提供比数据存储中的字符串更小的选项(例如TINYINT)

  • You avoid conversions due to math
  • 您可以避免因数学而导致的转化

  • It's going to confuse/annoy/frustrate all the developers that come after you when they query a data type that is naturally a number and get a string.
  • 当他们查询自然是数字并获得字符串的数据类型时,会让所有开发人员感到困惑/烦恼/沮丧。

#2


3  

If you are not expecting your YEAR variable to ever contain non-digit values then yes you should store it as a number.

如果您不希望YEAR变量包含非数字值,则应将其存储为数字。

I would not store it as INT since I doubt year will reach the limit that INT has to offer. I would save it as SMALLINT or even TINYINT either should be unsigned.

我不会把它作为INT存储,因为我怀疑一年会达到INT提供的限制。我会将它保存为SMALLINT,甚至TINYINT都应该是无符号的。

SMALLINT UNSIGNED gives you max value of 65535, unless you are storing years that exceed the year 65535 this should suffice.

SMALLINT UNSIGNED为您提供最大值65535,除非您存储超过65535年的年份,这应该足够了。

#3


0  

You could go crazy and save it as a YEAR!

你可能会发疯并将它保存为一年!

This limits you to 1901-2155.

这限制你到1901-2155。

If this is too restrictive, I prefer a CHAR(4) to an INT; MySQL DATETIME comparisons are done in a string like manner..

如果限制太多,我更喜欢CHAR(4)和INT; MySQL DATETIME比较以类似字符串的方式完成。

You can do things like

你可以做的事情

WHERE year < CURDATE()

without worries then.

没有后顾之忧。