5

I'm working on a project that is focused on meta information about phone numbers.

Using postgres, we will be seeding the database with millions of phone numbers and I'm concerned about the best way to store this information.

Right now I've been considering a phones table with each row representing the phone number as a string. Then simply joining off of that... Something like below:

+-----------------------+             +-----------------------+
|     phone_numbers     |             |     phones            |
+-----------------------+             +-----------------------+
| id: integer           +-------+     | id: integer           |
| digits: string        |       |     |                       |
|                       |       +-----+ phone_number: integer |
|                       |             |                       |
|                       |             |                       |
|                       |             |                       |
+-----------------------+             +-----------------------+

How should the database schema be designed with regard to the storage of phone numbers?

2
  • 1
    One person can have multiple phone numbers and multiple persons can have the same phone number: You're missing a table for the many-to-many relationship between people and phone Commented Mar 18, 2015 at 6:54
  • Guys thank you for commenting. I updated the example to focus on the relation more than the tables. I'm specifically wondering how to store phone numbers that can easily be referenced... i.e. strings, digits, broken apart into area code, prefix, suffix, etc. thank you
    – SharkLaser
    Commented Mar 18, 2015 at 7:03

1 Answer 1

8

The only pattern you can really follow for international phone numbers is to break them into three parts:

  1. International direct dialling prefix (maybe with associated table to lookup the country code: http://en.wikipedia.org/wiki/List_of_country_calling_codes)
  2. The phone number itself.
  3. Extension number(s)

Stored them as strings in order to preserve any desired formatting spaces or leading zeros, and validate that they follow an approved pattern.

Make a choice on whether to store national phone number "0" prefixes or not -- best to stick to the E.123 presentation of numbers where possible: http://en.wikipedia.org/wiki/E.123

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.