MySQL 美国邮政编码的最佳列类型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15843688/
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
What is the best column type for a United States ZIP code?
提问by Tapan Banker
I want to store Zip Code(within United States) in MySQL database. Savingspace is a priority. which is better option using VARCHAR - limited to maximum length of 6 digit or using INT or using MEDIUM Int . The Zip code will not be used for any calculation. The Zip code will be used to insert (Once), Updated (if required) and Retrieved - Once (but it can be more than once) .
我想在 MySQL 数据库中存储邮政编码(美国境内)。节省空间是当务之急。这是使用 VARCHAR 的更好选择 - 限制为最大长度为 6 位或使用 INT 或使用 MEDIUM Int 。邮政编码将不用于任何计算。邮政编码将用于插入(一次)、更新(如果需要)和检索 - 一次(但可以多次)。
Which is better option to use here VARCHAR or INT or MEDIUM IN MYSQL? Please suggest anything else ?
在 MYSQL 中使用VARCHAR 或 INT 或 MEDIUM哪个更好?请提出其他建议?
回答by Taryn
There are a few problems with storing a zip code as a numeric value.
将邮政编码存储为数值存在一些问题。
- Zip Codes have extensions, meaning they can be
12345-6789
. You cannot store a dash in a numeric datatype. - There are many zip codes that start with a zero, if you store it as an int you are going to lose the leading zero.
- You do not add/subtract, etc zip codes or use numeric functions with them.
- 邮政编码有扩展名,这意味着它们可以是
12345-6789
. 您不能在数字数据类型中存储破折号。 - 有许多以零开头的邮政编码,如果您将其存储为 int,您将丢失前导零。
- 您不添加/减去等邮政编码或使用数字函数。
I would store a zip code as a varchar(5)
or varchar(10)
.
我会将邮政编码存储为varchar(5)
或varchar(10)
。
As a side note, I am not sure why you would select varchar(6)
, do you have a reason for selecting an unusual length when standard zip codes are 5 or 10 with the extension?
作为旁注,我不确定您为什么会选择varchar(6)
,当标准邮政编码为 5 或 10 时,您是否有理由选择不寻常的长度并带有扩展名?
回答by G-Nugget
I usually use MEDIUMINT(5) ZEROFILL
for 5 digit zip codes. This preserves any leading 0
s and it only uses 3 bytes where a VARCHAR(5)
would use 6. This assumes that you don't need the extended zip codes, which have a dash and 4 extra numbers. If you were to decide to use a textual type, I would use CHAR(5)
instead of VARCHAR(5)
since it is better if the data is always 5 characters long.
我通常使用MEDIUMINT(5) ZEROFILL
5 位邮政编码。这会保留任何前导0
s 并且它只使用 3 个字节,而 aVARCHAR(5)
将使用 6个字节。这假设您不需要扩展的邮政编码,它有一个破折号和 4 个额外的数字。如果您决定使用文本类型,我会使用CHAR(5)
而不是VARCHAR(5)
因为如果数据总是 5 个字符长会更好。
回答by fisharebest
Zip codes are always 5 characters, hence you would need a CHAR datatype, rather than VARCHAR.
邮政编码总是 5 个字符,因此您需要 CHAR 数据类型,而不是 VARCHAR。
Your options are therefore
因此,您的选择是
CHAR(5)
MEDIUMINT (5) UNSIGNED ZEROFILL
The first takes 5 bytes per zip code.
第一个每个邮政编码需要 5 个字节。
The second takes only 3 bytes per zip code. The ZEROFILL option is necessary for zip codes with leading zeros.
第二个每个邮政编码只需要 3 个字节。ZEROFILL 选项对于带前导零的邮政编码是必需的。
So, if space is your priority, use the MEDIUMINT.
因此,如果您优先考虑空间,请使用 MEDIUMINT。
回答by Lawakush Kurmi
I would suggest, use VARCHAR data type because in some countries zip codes are used as alphanumeric and in other places as an integer. So we cannot use an integer for global use. Also, zip code may start with zero like 001101
so in this case if we take data type integer then leading zero will be lost so we cannot pass actual zip code.
我建议使用 VARCHAR 数据类型,因为在某些国家/地区,邮政编码用作字母数字,而在其他地方用作整数。所以我们不能将整数用于全局使用。此外,001101
在这种情况下,邮政编码可能以零开头,如果我们采用整数数据类型,那么前导零将丢失,因此我们无法传递实际的邮政编码。