创建多个表或只有一个 [英] Create many tables or just one

查看:115
本文介绍了创建多个表或只有一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,最好的方法是:

创建单个表地址(id,country,city,boulevard,street)



或make 3 like this:



countries (id,country)

城市(id,city,id_country)

地址(id,id_country,id_city,boulevard,street)





国家/地区(ID,国家/地区)

城市< ,city,id_country)

地址(id,id_city,boulevard,street)

解决方案>

这不可能可靠地回答,因为没有足够的背景信息。



如果您只需要关于国家/地区,城市和街道的文字信息,字段或输入形式的加强/验证,比1表方法就足够了。




  • 优点:更简单的模型,DB和代码,更容易实现,更好的性能需要)

  • 缺点:对国家/地区城市街道信息的控制较少,输入速度较慢,搜索能力不可靠(国家/地区名称是免费的,美国,美国等等,都对应于同一个国家)



如果您要链接逻辑上国家(和城市,地址等)与其他元素,并检查输入,你应该建模他们分离。这也允许UI增强(用户选择国家而不是自由打字,或者例如,当用户选择国家时,您可以提供从其中提取的城市的列表)。




  • 优点:更好的数据验证,更好的搜索能力,更多的输入控制

  • >:复杂的代码和DB,性能更弱,维护更复杂



然而,大多数时候,之间。要将某些重要信息更好地控制在单独的表(例如国家和城市)中,并将街道留在主表中,则是免费的。



您还可以在国家表中输入ID,而将整个contry名称放在主表中,使用countries表纯粹用于验证/加强。这使数据库不正确,但带来更多的好处。


What is the best approach in this case:
Create a single table address(id, country, city, boulevard, street)

or make 3 like this:

countries (id, country)
cities (id, city, id_country)
address (id, id_country, id_city, boulevard, street)

or

countries (id, country)
cities (id, city, id_country)
address (id, id_city, boulevard, street)

解决方案

That's not possible to answer reliably, as there is not enough background information. The answer depends on your reqs.

If you just need a textual info about the country, city and street, with no further checking of those fields or input-form enchancement/validation, than 1 table approach is enough.

  • Pros: simpler model, DB and code, easier implementation, better performance (you can index the fields if needed)
  • Cons: Less control over country-city-street info, slower data entering, unreliable search capacity (a country name is free, so you can have USA, U.S.A. United States, and so on, all corresponding to the same country)

If you want to "link" logically countries (and cities, adresses, and so on) with other elements and check the input, you should model them separatelly. This also enables UI enchancement (user picks up a country instead of free typing, or for example, when user selects the country, you can offer the list of cities to pick up from).

  • Pros: better data validation, better search caapacity, more control over input
  • Cons: complexer code and DB, weaker performance, more complex maintenance

Most of the times, however, you should look for solution somewhere in between. To put certain important information that you want to control better in a separate table (for example Countries and Cities) and leave the streets in the main table, free.

You can also omitt IDs in country tables and rather put the whole contry name in the main table, using countries table purely for the validation/enchancement. This denormalizes a bit the DB, but brings further benefits.

这篇关于创建多个表或只有一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆