mysql数据类型混乱
所以这更多的是关于 MySQL 数据类型的普遍问题。我想在此示例中正确存储 5 位美国邮政编码 (zip_code)。
一个县有 10 个不同的城市和 5 个不同的邮政编码。
city | zip code
-------+----------
city 0 | 33333
city 1 | 11111
city 2 | 22222
city 3 | 33333
city 4 | 44444
city 5 | 55555
city 6 | 33333
city 7 | 33333
city 8 | 44444
city 9 | 22222
我通常会构造一个像 varchar(50), int(5) 这样的表,并且不会三思而后行。
(1) 如果我们想确保该表只有 5 个不同邮政编码之一,我们应该使用 enum 数据类型,对吧?
现在考虑一个更大范围内的类似场景。一个州有 500 个城市,有 418 个不同的邮政编码。
(2) 我应该将 418 邮政编码存储为 enum 数据类型还是 int 数据类型并创建另一个表来引用?
So this is more of a generalized question about MySQLs data types. I'd like to store a 5-digit US zip code (zip_code) properly in this example.
A county has 10 different cities and 5 different zip codes.
city | zip code
-------+----------
city 0 | 33333
city 1 | 11111
city 2 | 22222
city 3 | 33333
city 4 | 44444
city 5 | 55555
city 6 | 33333
city 7 | 33333
city 8 | 44444
city 9 | 22222
I would typically structure a table like this as varchar(50), int(5) and not think twice about it.
(1) If we wanted to ensure that this table had only one of 5 different zip codes we should use the enum data type, right?
Now think of a similar scenario on a much larger scale. In a state, there are five-hundred cities with 418 different zip codes.
(2) Should I store 418 zip codes as an enum data type OR as an int and create another table to reference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
邮政编码应该是整数... MEDIUMINT 是什么你正在寻找。对于
ENUM
来说,几千个不同的值并不是一个好主意 - 此外 IIRCENUM
仅限于 64 个不同的值。您可以创建另一个名为
city
的表来存储城市。结构非常简单:只需标准 ID + 城市名称列 -VARCHAR(50)
就足够了。当然,您必须将
city_id
添加到邮政编码表(附加外键)。Zipcodes should be integers... MEDIUMINT is what you're looking for. Few thousands different values is not good idea for
ENUM
- besides IIRCENUM
is limited to 64 different values.You could create another table called
city
to store cities. The structure would be very simple: just standard ID + column for city name -VARCHAR(50)
should be enought.Of course you would have to add
city_id
to zipcodes table (with Foreign Key attached).如果smallint unsigned(0到65535)太小,则更改为mediumint。
if smallint unsigned (0 to 65535) is too small change to mediumint.