美国邮政编码(邮政编码)的 Mysql 数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1493926/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:11:49  来源:igfitidea点击:

Mysql Datatype for US Zip (Postal Codes)

sqlmysqldatabase-designdata-modelingzipcode

提问by gsueagle2008

I am writing a web application, that is US specific, so the format that other countries use for postal codes are not important. I have a list of us zip codes that i am trying to load into a database table that includes the

我正在编写一个特定于美国的 Web 应用程序,因此其他国家/地区用于邮政编码的格式并不重要。我有一个我们的邮政编码列表,我试图将其加载到包含以下内容的数据库表中

  • 5 digit us zip code
  • latitude
  • longitude
  • usps classification code
  • state code
  • city
  • 5 位美国邮政编码
  • 纬度
  • 经度
  • usps分类代码
  • 状态代码
  • 城市

the zip code is the primary key as it is what i will be querying against. i started using a medium int 5 but that truncates the zip codes that have leading zeros.

邮政编码是主键,因为它是我将要查询的。我开始使用中等 int 5,但它会截断具有前导零的邮政编码。

i considered using a char5 but am concerned about the performance hit of indexing against a char variable.

我考虑过使用 char5,但我担心对 char 变量进行索引会影响性能。

so my question is what is the best mysql datatype to store zip codes as?

所以我的问题是存储邮政编码的最佳 mysql 数据类型是什么?

Note: i have seen it in several other questions related to zip codes. I am only interested in US 5 digit zip codes. So there is no need to take other countries postal code formats into consideration.

注意:我已经在与邮政编码相关的其他几个问题中看到了它。我只对美国 5 位邮政编码感兴趣。因此无需考虑其他国家的邮政编码格式。

回答by Erich

char(5) is the correct way to go. String indexing is quite fast, particularly when it is such a small data set.

char(5) 是正确的方法。字符串索引非常快,特别是当它是一个如此小的数据集时。

You are correct in that you should never use an integer for a zip code, since it isn't truly numeric data.

你是对的,你永远不应该对邮政编码使用整数,因为它不是真正的数字数据。

Edit to add: Check out this for good reasons why you don't use numbers for non-numerically important data: Is it a good idea to use an integer column for storing US ZIP codes in a database?

编辑添加:看看这个有充分的理由为什么你不使用数字来处理非数字重要的数据: 使用整数列在数据库中存储美国邮政编码是个好主意吗?

回答by KM.

go with your medium INT(5) ZEROFILL, it should add the leading zeros for you. No need to impact the index and performance on a formatting issue.

使用您的中型 INT(5) ZEROFILL,它应该为您添加前导零。无需在格式问题上影响索引和性能。

回答by David

If he makes it Char(6), then he can handle Canadian postal codes as well.

如果他将其设为 Char(6),那么他也可以处理加拿大邮政编码。

When you consider that there is a maximum of 100,000 5-digit Zip Code and how little space it would take up even if you made the entire table memory-resident, there's no reason not to.

当您考虑到最多 100,000 个 5 位邮政编码以及即使您使整个表驻留内存也占用的空间很少时,没有理由不这样做。