MySQL 中电话号码的最佳数据类型是哪种,Java 类型映射应该是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24353778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which is best data type for phone number in MySQL and what should Java type mapping for it be?
提问by Vishal Zanzrukia
I am using MySQL with Spring JDBC template for my web application. I need to store phone number with only digits (10). I am little bit confused about data type using data type.
我在我的 Web 应用程序中使用 MySQL 和 Spring JDBC 模板。我需要存储只有数字(10)的电话号码。我对使用数据类型的数据类型有点困惑。
- What is preferable data type for it in MySQL?
- What should be Java data type in Bean (POJO) classes for that?
- How can I validate that datatype with javax validations/constrains for length and also only digit allowed?
- MySQL 中它的首选数据类型是什么?
- Bean (POJO) 类中的 Java 数据类型应该是什么?
- 如何使用长度的 javax 验证/约束以及仅允许数字来验证该数据类型?
回答by indivisible
Strings & VARCHAR.
字符串和 VARCHAR。
Do not try storing phone numbers as actual numbers. it will ruin the formatting, remove preceding
0
s 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 for00
at the start of an international number.
不要尝试将电话号码存储为实际号码。它会破坏格式,删除前面的
0
s 和其他不需要的东西。如果您选择,您可以将用户输入限制为仅数字值,但即使在这种情况下,也要将支持的持久数据保留为字符/字符串而不是数字。
在尝试实施任何类型的长度限制、验证或掩码(例如 XXX-XXXX-XX)之前,请注意更广阔的世界以及它们的数字长度和格式有何不同。
非数字字符可以在电话号码中有效。一个主要的例子是
+
作为00
国际号码开头的替代品。
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".
- 电话号码与数字有任何关系是更大的 UI 错误之一。将它们视为地址要好得多,它比电话“号码”更接近它们的实际情况和代表。
回答by GreyBeardedGeek
- varchar
String
A simple regex. See: How to check if a string contains only digits in Java. Use javax.constraints.Pattern.
- 变量字符
细绳
一个简单的正则表达式。请参阅:如何检查字符串是否仅包含 Java 中的数字。使用 javax.constraints.Pattern。
回答by NcDreamy
VARCHAR with probably 15-20 length would be sufficient and would be the best option for the database. Since you would probably require various hyphens and plus signs along with your phone numbers.
长度可能为 15-20 的 VARCHAR 就足够了,并且是数据库的最佳选择。因为您可能需要各种连字符和加号以及您的电话号码。
回答by damon
you can use var-char,String,and int ,it depends on you, if you use only country code with mobile number than you can use int,if you use special formate for number than use String or var-char type, if you use var-char then must defile size of number and restrict from user.
您可以使用 var-char、String 和 int ,这取决于您,如果您只使用带有手机号码的国家代码,则可以使用 int,如果您使用特殊格式的数字而不是使用 String 或 var-char 类型,如果您使用 var-char 则必须修改数字的大小并限制用户。
回答by 00500005
Consider using the E.164format. For full international support, you'd need a VARCHAR of 15 digits.
考虑使用E.164格式。要获得全面的国际支持,您需要一个 15 位的 VARCHAR。
See Twilio's recommendationfor more information on localization of phone numbers.
有关电话号码本地化的更多信息,请参阅Twilio 的建议。
回答by Irshad Khan
In MySQL -> INT(10) does not mean a 10-digit number, it means an integer with a display width of 10 digits. The maximum value for an INT in MySQL is 2147483647 (or 4294967295 if unsigned).
在 MySQL -> INT(10) 不是指 10 位数字,而是指显示宽度为 10 位的整数。MySQL 中 INT 的最大值为 2147483647(如果无符号则为 4294967295)。
You can use a BIGINT instead of INT to store it as a numeric. Using BIGINT will save you 3 bytes per row over VARCHAR(10).
您可以使用 BIGINT 而不是 INT 将其存储为数字。使用 BIGINT 将比 VARCHAR(10) 每行节省 3 个字节。
If you want to Store "Country + area + number separately". Try using a VARCHAR(20). This allows you the ability to store international phone numbers properly, should that need arise.
如果要分别存储“国家+地区+数字”。尝试使用 VARCHAR(20)。这使您能够在需要时正确存储国际电话号码。
回答by Александр Аверьянов
In mysql: BIGINT. In java: Long.
在 mysql 中:BIGINT。在 Java 中:长。
回答by mannedear
My requirement is to display 10 digit phone number in the jsp. So here's the setup for me.
MySQL: numeric(10)
我的要求是在jsp中显示10位电话号码。所以这是我的设置。
MySQL:numeric(10)
Java Side:
Java端:
@NumberFormat(pattern = "#")
private long mobileNumber;
and it worked!
它奏效了!
回答by bala raja
It's all based on your requirement. if you are developing a small scale app and covers only specific region (target audience), you can choose BIGINT to store only numbers since VARCHAR consumes more byte than BIGINT ( having optimal memory usage design matters ). but if you are developing a large scale app and targets global users and you have enough database capabilities to store data, you can definitely choose VARCHAR.
这一切都基于您的要求。如果您正在开发一个小规模的应用程序并且只涵盖特定区域(目标受众),您可以选择 BIGINT 来仅存储数字,因为 VARCHAR 比 BIGINT 消耗更多字节(具有最佳内存使用设计很重要)。但是如果你正在开发一个大规模的应用程序,面向全球用户,并且你有足够的数据库能力来存储数据,那么你绝对可以选择 VARCHAR。
回答by duytung112
- String
- Varchar
- 细绳
- 瓦尔查尔
This is my opinion for my database as recommended by my mentor
这是我的导师推荐的我对数据库的看法