views:

941

answers:

7

Hello,

I want to input telephone number in a form, including country code, extension

create table if not exists employee(    ` 
      country_code_tel   int(11),
      tel_number         int(10),
      extension          int(10),
      mobile             bigint(20)
);

If tel_number is larger than 15 bit, which datatype can i use, i'd better use Bigint(20)?

create table address(
      address           varchar(255),  
      city              varchar(255),
      country           varchar(255),
      post_code         int(11)
);

For example i have a country code for Canada, i can use +2 or 002. Which is better or processing?

Thanks for your advice.

+4  A: 

Actually you can use a varchar for a telephone number. You do not need an int because you are not going to perform arithmetic on the numbers.

Vincent Ramdhanie
@Vincent, but if i want to use authenticate the format of numbers, i think i have to use int. Actually it's the first time i make a "real application", So I want to get your suggestions from real projects.
garcon1986
@garcon1986: The validation - (not 'authentication') - has to be done on your application prior to comitting to the databases. You could prefer writing an SQL trigger with a valitation PL procedure to live inside the database - this will be harder, but is the way to go if your DB data willbe acessed by front-end apps written in different programing languages by different people - , but you should not rely onthe database datatype for validation
jsbueno
@garcon1986 Validation is still easier with varchar than int. Every real project that I know uses varchar rather thasn int for telephone fields. Even validation libraries expect characters.
Vincent Ramdhanie
@thanks a lot, Vincent. I'm sorry for misunderstanding the two concepts firstly. And I'll try to use "varchar" type in telephone numbers with validation.
garcon1986
@garcon1986 It is no problem at all. No need to apologize...that is why this site exists
Vincent Ramdhanie
+1  A: 

i would use a varchar for telephone numbers. that way you can also store + and (), which is sometimes seen in tel numbers (as you mentioned yourself). and you don't have to worry about using up all bits in integers.

kon
@KON,thanks, i prefer to use varchar for telephone numbers now.
garcon1986
+1  A: 

I'm not sure whether it's a good idea to use integers at all. Some numbers might contain special characters (# as part of the extension for example) which you should be able to handle too. So I would suggest using varchars instead.

nfechner
@nfechner, Thanks for providing the case, it's practical.
garcon1986
we use an 'x' here.. 999 9999 x1234
ShoeLace
+3  A: 

Well, personally I do not use numeric datatype to store phone numbers or related info.

How do you store a number say 001234567? It'll end up as 1234567, losing the leading zeros.

Of course you can always left-pad it up, but that's provided you know exactly how many digits the number should be.

This doesn't answer your entire post,
Just my 2 cents

o.k.w
@o.k.w, it's really practical. Thanks for your advice.
garcon1986
@garcon: Honestly speaking, the other guys here gave more holistic views. Mine is just one of many scenarios which 'void' the use of numeric type for phone numbers. :P
o.k.w
+1  A: 

Just add my 2p of real-world experience.

We store 2 fields for phone numbers - a "number" and a "mask". Both of these are stored as TinyText types

Before we store the files we parse the phone number to get the formatting that has been used and that creates the mask, we then store the number a digits only e.g.

Input: (0123) 456 7890
Number: 01234567890
Mask: (nnnn)_nnn_nnnn

Theoretically this allows us to perform comparison searches on the Number field such as getting all phone numbers that begin with a specific area code, without having to worry how it was input by the users

Dan Kelly
@Dan Kelly, Thanks , you have provided me with another method. cool!
garcon1986
A: 

int(11) is sometimes to short for postal code, take a look for address representation in Oracle Spatial DBMS

http://www.stanford.edu/dept/itss/docs/oracle/10g/appdev.101/b10826/sdo_geocode_concepts.htm#CIHBBGIF

UrX
A: 

I usually store phone numbers as a BIGINT in E164 format.

E164 never start with a 0, with the first few digits being the country code.

+441234567890 +44 (0)1234 567890 01234 567890

etc. would be stored as 441234567890.

Curon