postgresql 为什么最好将电话号码存储为字符串而不是整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23637057/
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-10-21 01:26:47  来源:igfitidea点击:

Why is it best to store a telephone number as a string vs. integer?

ruby-on-railsrubyruby-on-rails-3postgresqlruby-on-rails-4

提问by user3399101

As the question states, why is it considered best practice to store telephone numbers as strings rather than integers in the telephone_number column?

正如问题所述,为什么在 phone_number 列中将电话号码存储为字符串而不是整数被认为是最佳实践?

Not sure I understand the rationale for this. Please help clear this up!

不确定我是否理解这样做的理由。请帮忙解决这个问题!

Thanks!

谢谢!

回答by Neil Slater

Telephone numbers are strings of digit characters, they are not integers.

电话号码是一串数字字符,它们不是整数。

Consider for example:

考虑例如:

  • Expressing a telephone number in a different base would render it meaningless

  • Adding or multiplying two telephone numbers together, or any math operation on a phone number, is meaningless. The result is not another telephone number (except by conicidence)

  • Telephone numbers are intended to be entered "as-is" into a connected device.

  • Telephone numbers may have leading zeroes.

  • Manipulations of telephone numbers, such as adding an area code, are String operations.

  • 以不同的基数表示电话号码会使它变得毫无意义

  • 将两个电话号码相加或相乘,或者对电话号码进行任何数学运算,都是毫无意义的。结果不是另一个电话号码(除非有意外)

  • 电话号码旨在“按原样”输入到连接的设备中。

  • 电话号码可能有前导零。

  • 电话号码的操作,例如添加区号,是字符串操作。

Storing the string version of the telephone number makes this clear and unambiguous.

存储电话号码的字符串版本可以使这一点清晰明确。



History: On old pulse-encoded dial systems, the code for each digit in a telephone number was sent as the same number of pulses as the digit (or 10 pulses for "0"). That may be why we still use digits to represent the parts of a phone number. See http://en.wikipedia.org/wiki/Pulse_dialing

历史:在旧的脉冲编码拨号系统上,电话号码中每个数字的代码都以与数字相同的脉冲数(或“0”的 10 个脉冲)发送。这可能就是我们仍然使用数字来表示电话号码部分的原因。请参阅http://en.wikipedia.org/wiki/Pulse_dialing

回答by elixenide

What Neil Slater said is correct. I would add that there are lots of edge cases where you can't express a telephone number as a number value consistently.

尼尔斯莱特说的是正确的。我要补充的是,在很多边缘情况下,您无法始终将电话号码表示为数值。

For example, consider these numbers:

例如,考虑这些数字:

011-123-555-1212
+11-123-555-1212
+1 (112) 355-5121 x2

These are all potentially valid phone numbers, but they mean very different things. Yet, in integer form, they are all 111235551212.

这些都是可能有效的电话号码,但它们的含义截然不同。然而,在整数形式中,它们都是111235551212

回答by Bovine

If you are going to store the number for display from input, then you must use a string.

如果要存储输入的数字以供显示,则必须使用字符串。

However, while it is true that no mathematical operations can be performed on a number that have meaning. Using a number in hashsets and for indexing is quicker than using a string. So provided you can guarantee or homogenise your set of numbers, so they are all consistent, then you may see better performance operating on a number.

但是,确实不能对有意义的数字执行任何数学运算。在散列集和索引中使用数字比使用字符串更快。因此,只要您可以保证或同质化您的一组数字,使它们都保持一致,那么您可能会看到对数字进行操作时的性能更好。

For example, in the Telco world, rating calls for a given customer includes a lot of searching on their CLI and in this situation it is faster and cheaper to search by integer. Generally though strings will be fine performance wise, it is only where performance matters and you have multiple searches to perform for a huge range of numbers - i.e. Rating 250 million calls across 2 million lines and 2000 tariffs. In memory rating also gets expensive, so being able to use a 64bit int or uint is cheaper when dealing with these volumes.

例如,在电信领域,对给定客户的评级呼叫包括在他们的 CLI 上进行大量搜索,在这种情况下,按整数搜索更快、更便宜。一般来说,尽管字符串在性能方面会很好,但只有在性能很重要的地方,您才能对大量数字执行多次搜索 - 即对 200 万条线路和 2000 条资费的 2.5 亿个呼叫进行评级。内存等级也变得昂贵,因此在处理这些卷时能够使用 64 位 int 或 uint 更便宜。

回答by Pavan

Consider these phone numbers for example

例如,考虑这些电话号码

099-1234-56789or +91-8907-687665.

099-1234-56789+91-8907-687665

In this case,if the phone_numberattribute is of type integer,then it can't accept these values.It should be a stringto hold these type of values.So stringis always preferred than integer

在这种情况下,如果phone_number属性是 type integer,那么它不能接受这些值。它应该是 astring来保存这些类型的值。string所以总是比integer

回答by Alexandre FILLATRE

There is several reasons for this :

这有几个原因:

  • Phone numbers often start with a "0" : an integer will remove all leading "0"s
  • Phone number can have special char : +, (, -, etc. (for exemple : +33 (0)6 12 23 34)
  • You cannot perform operations on phones : adding phones, for instance, would be meaningless
  • Phone number may be internationalised, i.e. different format for different people, thus not possible with integers
  • 电话号码通常以“0”开头:整数将删除所有前导“0”
  • 电话号码可以有特殊字符 : +, (,-等(例如:+33 (0)6 12 23 34)
  • 您无法对手机进行操作:例如添加手机是没有意义的
  • 电话号码可能是国际化的,即不同的人使用不同的格式,因此不可能使用整数

There might be other reasons, but I guess that's already a fair amount of those :)

可能还有其他原因,但我想这已经是相当多的原因了:)