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

前端 未结 5 603
死守一世寂寞
死守一世寂寞 2021-02-01 12:50

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

Not sure I understa

5条回答
  •  渐次进展
    2021-02-01 13:09

    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

提交回复
热议问题