数据库设计问题:如何引用不同类型的地理区域
Question on DB design: how to reference to different type of geo regions
我目前正在为宏观经济统计开发 BD,其中将包括不同国家和地区的 GDP、inflation、人口和其他时间序列。您能否建议如何为它设计架构的最佳方法?我目前正在努力如何引用地理区域。
有不同类型的地理区域——宏观区域(欧盟、欧洲、中东和非洲、新兴市场等,由多个国家组成)、单一国家和微观区域(如美国各州)。将来可能会有其他类型的区域。大多数经济指标将归因于单个国家,但有些可能与宏观区域或微观区域相关联。在数据库中实现这个的最佳方法是什么?为每种类型的区域(国家、宏观和微观区域)创建单独的 table 更好,还是应该使用不同类型的单个 table?从带有指标 (tIndicator_values) 的 table 到带有 counties/regions 的 table 的参考的最佳方法是什么?
如果您能就 DB 与宏观经济统计的实施提出一些 resources/examples 的建议,我将不胜感激。
我会选择一个 table region
来标识区域的类型并有一个外键返回到它的 "parent region"
create table region_type
(
id integer primary key,
name varchar(20) not null unique
);
create table region
(
id integer primary key,
name varchar(100) not null,
type_id integer not null references region_type,
parent_region_id integer references region
);
然后你会有这样的行:
insert into region_type
(id, name)
values
(1, 'political union'),
(2, 'geographical'),
(3, 'country'),
(4, 'state');
insert into region
(id, name, type, parent_region_id)
values
(1, 'EMEA', 2, null),
(2, 'EU', 1, 1),
(3, 'Germany', 3, 1),
(4, 'Bavaria', 4, 3);
然后您的指标 table 将通过外键引用区域 table。
region
table 中的层次结构允许您根据需要在不同级别上进行汇总,即使指标仅链接到例如国家。
我目前正在为宏观经济统计开发 BD,其中将包括不同国家和地区的 GDP、inflation、人口和其他时间序列。您能否建议如何为它设计架构的最佳方法?我目前正在努力如何引用地理区域。 有不同类型的地理区域——宏观区域(欧盟、欧洲、中东和非洲、新兴市场等,由多个国家组成)、单一国家和微观区域(如美国各州)。将来可能会有其他类型的区域。大多数经济指标将归因于单个国家,但有些可能与宏观区域或微观区域相关联。在数据库中实现这个的最佳方法是什么?为每种类型的区域(国家、宏观和微观区域)创建单独的 table 更好,还是应该使用不同类型的单个 table?从带有指标 (tIndicator_values) 的 table 到带有 counties/regions 的 table 的参考的最佳方法是什么? 如果您能就 DB 与宏观经济统计的实施提出一些 resources/examples 的建议,我将不胜感激。
我会选择一个 table region
来标识区域的类型并有一个外键返回到它的 "parent region"
create table region_type
(
id integer primary key,
name varchar(20) not null unique
);
create table region
(
id integer primary key,
name varchar(100) not null,
type_id integer not null references region_type,
parent_region_id integer references region
);
然后你会有这样的行:
insert into region_type
(id, name)
values
(1, 'political union'),
(2, 'geographical'),
(3, 'country'),
(4, 'state');
insert into region
(id, name, type, parent_region_id)
values
(1, 'EMEA', 2, null),
(2, 'EU', 1, 1),
(3, 'Germany', 3, 1),
(4, 'Bavaria', 4, 3);
然后您的指标 table 将通过外键引用区域 table。
region
table 中的层次结构允许您根据需要在不同级别上进行汇总,即使指标仅链接到例如国家。