简体   繁体   English

管理国家,州和城市的数据库

[英]Database for manage countries, states and cities

I need to manage countries, states/province and cities name information. 我需要管理国家,州/省和城市名称信息。 All information will display on webpage with drop down list or other ways. 所有信息将通过下拉列表或其他方式显示在网页上。 Currently I am designing my database. 目前,我正在设计数据库。 I have two ideas to design tables 我有两个设计桌子的想法

1. use only one table 1.只使用一张桌子

countries, states/provinces and cities are usually very stable information, so I want to use only one table to manage all information and make it reference to itself such as: 国家,州/省和城市通常是非常稳定的信息,因此我只想使用一张表来管理所有信息并使其引用自身,例如:

 id name parent_id type --+--------------+----------+-- 1 USA null 1 2 California 1 2 3 Los Angeles 2 3 4 San Francisco 2 3 

type: 1 for country, 2 for states or provinces and 3 for cities 类型:1个代表国家/地区,2个代表州或省,3个代表城市

2. use separate tables 2.使用单独的表

I will use one table to manage country, and one table to manage states/province and one table to manage cities. 我将使用一张表管理国家/地区,使用一张表管理州/省,使用一张表管理城市。 then make them reference between each other. 然后使它们之间相互参考。

So which one is better? 那么哪个更好呢? and please explain your reason. 并请解释您的原因。

I think #2 is better. 我认为#2更好。

With separate tables, you can easily manage records. 使用单独的表,您可以轻松管理记录。 For SELECT query; 用于SELECT查询; it's is faster 它更快

Example If you want select country only , 示例如果仅想选择国家/地区,

1# : SELECT * FROM countries WHERE type = 1 ; 1#:选择*来自类型为1的国家/地区;

2# : SELECT * FROM countries; 2#:选择*来自国家;

Of course; 当然; 2# is faster because data in tables is less than 1# 2#更快,因为表中的数据小于1#

I don't think "speed" is the problem here, like @fanfan1609 said, but if you use one table, you would save "different" things in one table. 我不认为“速度”是这里的问题,就像@ fanfan1609所说的那样,但是如果您使用一个表,则会在一个表中保存“不同”的东西。 A state is not a country and so on. 国家不是国家,依此类推。 Think of database-normalization. 想想数据库规范化。

What if a city will get a postalcode, because you need it? 如果某个城市因为您需要而获得邮政编码,该怎么办? Would you change your monster-table to have a postalcode column, just for one type? 您是否将怪兽表更改为只有一种类型的邮政编码栏? Then you begin to add a "meta" column of type varchar, and let the crap begin. 然后,您开始添加类型为varchar的“元”列,然后开始废话。

You should certainly choose table-splitted solution. 您当然应该选择表拆分解决方案。 It is so due extensibility issue. 这是应有的可扩展性问题。 Once you need to extend your country or state dataset stored, you'll see how inefficient it would be. 一旦需要扩展存储的国家或州数据集,您将发现它效率低下。

#1 is Better than #2 #1比#2更好
also, you can create recursive view 另外,您可以创建递归视图
Take a look here: 在这里看看:
How to do the Recursive SELECT query in MySQL? 如何在MySQL中执行递归SELECT查询?

Good Luck ! 祝好运 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM