1

我有一张大表,将美国所有的州和县信息集中在一个地方。

表结构是

    fullCode   countyName   stateName   stateCode
1   01001        nowhere      AL          01
2   01003        somewhere    AL          01
3   02100        other        AK          02

状态代码始终由fullCode列中的前两个数字标识。这些对于每个州都是独一无二的,因此没有一个州将有超过一组前两个数字。县代码的最后三个数字。

我使用下面的查询创建了一个包含所有状态的表

select distinct stateName, statecode
into tblStates
from tblCounties

我很好奇如何一举为每个州的县创建一张表格(如果可能的话)。就像是

select distinct  fullCode, countyName
into tblAlabamaCounties
from tblCounties
where stateName='AL'

但对于每个州。当然,我必须处理所有的 PK/FK 问题。只是想知道可能用于执行此类操作的方法。

编辑:如果这是一个设计错误,我还能如何关联特定州的县名?我可以把所有东西都放在一张大桌子上,但这似乎是糟糕的设计,想法?

4

2 回答 2

2

您可能会考虑以下更好的设计:

状态表:

STATE_ID
STATE_ABBREV
STATE_NAME

县表:

COUNTY_ID
STATE_ID
COUNTY_NAME

样本数据:

STATE_ID   STATE_ABBREV   STATE_NAME
01         AL             Alabama
02         AK             Alaska

COUNTY_ID  STATE_ID       COUNTY_NAME
001        01             Nowhere
003        01             Somewhere
100        02             Other

您的状态表的主键将是 STATE_ID
您的县表的主键将是 COUNTY_ID ---AND---- STATE_ID (COMBINED)

使用这种结构,您只有 2 个表,您可以使用它们轻松复制原始“大”表以及“完整代码”字段。此外,基于这种(更简单/更正常)的结构,更新、查询、创建过程、函数等将变得容易得多。

FWIW

于 2012-06-12T19:48:53.050 回答
1

If you use Oracle, you could create a PL-SQL procedure where you create first of all the different tables, and then you only have to populate the data into the new tables recently create. IF you use PL SQL procedure you could do it.

On the other hand you can create the different tables, and create a triggers before insert, and you will insert the data in the specific table before insert in your master table.

Sorry for my English I hope you can understand all and help you.

于 2012-06-12T19:53:45.717 回答