简体   繁体   中英

mysql - datatype for mobileno. decimal vs varchar

I have stored phone/mobile no(s) with country-code in mysql table. for that I have used decimal datatype for both the fields, (eg for dialcode decimal(5,0), mobileno decimal(20,0)). My mobileno and dial-code or country-code must be numberic. now I am going to change it with varchar(5) for dial-code and varchar(20) for mobileno.

Is it ok for performance? can anybody tell me how maximum length should be for mobileno (any country)? and If any other datatype like flote or integer would preferable for the above case? if yes, then why?

Thanks.

This question runs the risk of asking for an opinion. However, I think it has a "right" or at least "better" answer.

You should use numeric types when you are storing numbers. Numbers have properties of being ordered and of being able to do arithmetic on them. Many things are stored as numbers that don't have these properties -- think about credit card numbers, zip codes, and FIPs codes. Telephone numbers also fall into this category.

One important consideration is whether leading 0s are important. For actual numbers, these are not important. For codes that happen to use digits, they are important.

So, I would advise you to store the values as character strings. I have no idea what the longest telephone number is, but I wouldn't want my database to be dependent on today's maximum value. Just use something like varchar(255) , which should be more than adequate for a long time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM