How to store phone numbers on MySQL databases? [duplicate]
Solution 1:
- All as varchar (they aren't numbers but "collections of digits")
- Country + area + number separately
- Not all countries have area code (eg Malta where I am)
- Some countries drop the leading zero from the area code when dialling internal (eg UK)
- Format in the client code
Solution 2:
You should never store values with format. Formatting should be done in the view depending on user preferences.
Searching for phone nunbers with mixed formatting is near impossible.
For this case I would split into fields and store as integer. Numbers are faster than texts and splitting them and putting index on them makes all kind of queries ran fast.
Leading 0 could be a problem but probably not. In Sweden all area codes start with 0 and that is removed if also a country code is dialed. But the 0 isn't really a part of the number, it's a indicator used to tell that I'm adding an area code. Same for country code, you add 00 to say that you use a county code.
Leading 0 shouldn't be stored, they should be added when needed. Say you store 00 in the database and you use a server that only works with + they you have to replace 00 with + for that application.
So, store numbers as numbers.