C
C
Cyapa2014-10-28 03:22:30
MySQL
Cyapa, 2014-10-28 03:22:30

What is the best way to store a phone number in a database?

Hello friends. Tell me, how best to store the phone number in the database: as a string or as a number? The second option wins in weight, but it should be noted that very often you will have to make a selection of numbers according to certain conditions. For example:
79[4-8][2-3]*
You need to find numbers using this pattern (They start with 79, then the number from 4 to 8, then the number from 2 to 3, the rest of the numbers can be any).
To do this with numbers, the template can be parsed and turned into more-less conditions. With strings, the situation looks simpler, take at least the same LIKE, which itself can process the "asterisk" in the template (of course, if it is first replaced by %).
Or maybe there is some alternative? Store some pre-trained number data to help make fetches faster?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Entelis, 2014-10-28
@Cyapa

I love questions like this.
Let's start with - how many numbers do you have in the database, what is important for you to save space?
On the topic - lines.

V
vnpp, 2018-07-31
@vnpp

If you follow the communication recommendation (see Telephone Country Codes, Telephone Numbering Plan), it is advisable to store the full number (+ extension). The full number consists of an area code and internal numbers, the total length is 11 digits, for a separate country (localized systems) there is no point in storing an area code from 1 to 3 digits. In total, int8 is suitable for the full number, int4 can also be suitable for local ones.
For extension numbers - a separate field, here who is in what much.
Numbers starting with zeros, as a rule, are local (open numbering plan), there is no point in storing them, because You can call them only if you are in the same region.
It is more correct to store the full number and, if necessary, separately, the rules for converting the full number into local ones and necessarily the restrictions where it works.
Storing in int allows you to exclude input errors at the database level without using regexp for each insert / update, it is more convenient to store continuous ranges.
However, if you do not store "the whole world", there is little tangible benefit from switching from string to int when storing a directory of numbers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question