Address.City:字符串还是引用?

发布于 2024-10-16 17:20:16 字数 167 浏览 0 评论 0原文

我必须创建一个数据库来存储大量人员及其地址,并且必须可以按地点进行搜索(即查找该城市或其他城市的人员)。

我怀疑是否应该将地址中的城市字段保留为简单的 varchar,还是创建一个城市表并引用它,以避免重复的城市名称等。

注意:我正在使用 SQL-Server 并将通过 EF 访问数据。

I have to create a database that will store a huge amount of people along with their addresses, that will have to be searchable by place (i.e. find people in that or other city).

I am doubting whether I should keep the city field in the address as simple varchar or create a city table and refer to it, to avoid duplicate city names etc.

Note: I am using SQL-Server and will access the data via EF.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃酥萝莉 2024-10-23 17:20:16

背景

权威表 - 一个参考表,其中包含其具有权威的“事物”的可能值。例如,国家/地区权限表将包含国家/地区的所有可能值。

答案

根据您对“大量……”的定义,您肯定会想要一些权限表。
根据您的地址的地理范围,其中部分或全部似乎是您的权限表的良好开端:

  1. 国家/地区 - 如果您允许超过 1 个国家/地区的地址,那么将权限表设置为
  2. 国家似乎是一件好事(或省) - 国家被分为普罗旺斯和/或州。这些是权威表的良好候选者。
  3. 城市 - 城市(大部分)是具有定义名称的固定实体。另一个候选者表。这应包括城镇、村庄、城市和其他(农村居民可能不住在城市、村庄、城镇或其他任何地方)。
  4. 街道名称 - 街道的名称(或编号)(大部分)是固定的。这是权威表的候选者。街道名称将取决于城市、州和国家的组合,以使其独一无二。 例如 城市“Blah”可能有一条名为“Mainne”的街道,而城市“NotBlah:可能没有一条名为“Mainne”的街道
  5. 邮政编码(美国的邮政编码)很可能是例如,美国没有邮政编码 00001。

Background

Authority table - a reference table that contains the possible values for the "thing" for which it is an authority. For example, a Country authority table would contain all possible values for country.

An Answer

Depending on your definition of "huge amount ..." you definately will want to have some authority tables.
Based on the geographic range of your addresses, some or all of these seem like a good start for your authority tables:

  1. Country - if you allow addresses in more than 1 country, it seems like a good thing to have as an authority table
  2. State (or province) - nations are broken into provences and/or states. These are a good candidate for an authority table.
  3. City - cities are (mostly) fixed entities that have a defined name. Another candidate for an anthority table. This should include towns, villages, cities, and other (rural residents may not live in a city, village, town, or anything else).
  4. Street Name - streets have names (or numbers) that are (mostly) fixed. This is a candidate for an authority table. Street name will depend on a combination of city, state, and country to make it unique. For example City "Blah" may have a street named "Mainne" while city "NotBlah: may not have a street named "Mainne"
  5. Postal Code (zip code in the USA) is a likely candidate for an authority table. For example, there is no zip code 00001 in the USA.
禾厶谷欠 2024-10-23 17:20:16

您当前的设计是否有一个如下所示的 ADDRESS varchar 列:?

"101 MAIN STREET, NEW YORK, NY, 10010"

如果是,那么如果您必须按街道、城市、州、邮政编码或组合进行搜索,那么您的生活就会变得复杂。

我建议使用 ADDRESS 表,其中包含 STREET、CITY、STATE 和 ZIP 的单独列。这样您就可以单独查询每一项。请务必为您使用的每个 WHERE 子句添加索引。

下一个问题是进一步规范化(例如,城市、州、邮政编码的单独表)和联接以获得地址是否有用。我不确定是否有必要,但你可以尝试一下。

Doesyour current design have an ADDRESS varchar column that looks like this:?

"101 MAIN STREET, NEW YORK, NY, 10010"

If yes, then you complicate your life if you have to search by street, city, state, zip or combinations.

I'd recommend an ADDRESS table with separate columns for STREET, CITY, STATE, and ZIP. That way you can query for each one individually. Be sure to add an index for each WHERE clause you use.

The next question is whether it's useful to normalize further (e.g. separate tables for CITY, STATE, ZIP) and JOINing to get an address. I'm not sure that it's necessary, but you can try it.

心碎无痕… 2024-10-23 17:20:16

我假设您的模型看起来像这样:

Address
--------------
Address
City
State
Zip

等等。

如果是这样,则无法消除 City 列中某些值的重复。如果您要创建一个 City 表,那么城市名称显然是自然键的选择,这意味着您的 City 列的实际数据将保持不变。如果您要使用代理键,那么您只需重复该键值而不是城市名称。不过,我不建议在这里使用代理键,因为城市的名称不太可能改变,而且您会添加额外的间接级别,但没有任何好处。

最后,有一些潜在的场景需要 City 表:

  • 您希望将与地址无关的元数据与城市记录相关联
  • 您希望在此字段上强制执行引用完整性。即,您希望确保 City 的所有值都来自已知值列表,您将其存储在城市列表表中。这还允许您向用户呈现城市列表,而不仅仅是允许他们以自由格式文本的形式输入数据。

如果其中任何一个适用于您,那么请务必创建一个城市表。如果没有,那就没有必要了。

I'm assuming that your model looks something like this:

Address
--------------
Address
City
State
Zip

etc.

If so, then there's no way to eliminate the repeating of some value in the City column. If you were to create a City table, the city name would be an obvious choice for a natural key, which would mean that your City column's actual data would remain unchanged. If you were to use a surrogate key, then you'd simply be repeating that key value instead of the city name. I would not suggest a surrogate key here, though, since a city's name is unlikely to change and you'd be adding an additional level of indirection with no benefit.

In the end, there are a couple of potential scenarios that would warrant a City table:

  • You want to associate address-agnostic metadata with a city record
  • You want to enforce referential integrity on this field. I.E. you want to be certain that all values for City come from a list of known values, which you'd store in your city list table. This would also allow you to present the user with a list of cities rather than just allowing them to enter the data as free-form text.

If either of those apply to you, then, by all means, create a city table. If not, then there's no need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文