Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is best data type for phone number in MySQL and what should Java type mapping for it be?

I am using MySQL with the Spring JDBC template for my web application. I need to store phone numbers with only digits (10). I am a little bit confused about data type using data type.

  1. What is the preferable data type for it in MySQL?
  2. What should be the Java data type in Bean (POJO) classes for that?
  3. How can I validate that datatype with javax validations/constraints for length and also only digit allowed?
like image 848
Vishal Zanzrukia Avatar asked Jun 22 '14 17:06

Vishal Zanzrukia


People also ask

What data type should I use for a phone number?

I generally use VARCHARs to store telephone numbers. Storage is not so expensive these days that I benefit that much from the savings found by storing them as numeric values.

What is data type for mobile number in MySQL?

Use VARCHAR to Store Phone Numbers in MySQL The final suggested method for handling phone numbers in MySQL is by using the VARCHAR data type. VARCHAR offers the flexibility of dynamic memory allocation when the phone number length will vary across database users.

Which data type is better to use for storing phone number in Java?

String Data Type to Store Phone Number in Java It is better to use String type to hold a phone number in Java to solve this problem. Now, we store phone numbers with a Country Code + Area Code + Phone Number format. See the example below.

What would be a good access SQL data type for a phone number field?

A phone number should always be stored as a string or text and never an integer. Some phone numbers generally use hyphens and possibly parentheses. Also, you might need to indicate the country code before the phone number such as +46 5555-555555.


1 Answers

Strings & VARCHAR.

  • Do not try storing phone numbers as actual numbers. it will ruin the formatting, remove preceding 0s and other undesirable things.

  • You may, if you choose to, restrict user inputs to just numeric values but even in that case, keep your backing persisted data as characters/strings and not numbers.

  • Be aware of the wider world and how their number lengths and formatting differ before you try to implement any sort of length restrictions, validations or masks (eg XXX-XXXX-XX).

  • Non numeric characters can be valid in phone numbers. A prime example being + as a replacement for 00 at the start of an international number.

Edited in from conversation in comments:

  • It is one of the bigger UI mistakes that phone numbers have anything to do with numerics. It is much better to think of and treat them like addresses, it is closer to what they actually are and represent than phone "numbers".
like image 52
indivisible Avatar answered Sep 24 '22 01:09

indivisible