实验要求
本实验是一个简单的数据仓库模型,事实表保存销售记录,维表保存用户、时间、产品、销售人员、商店等信息,这些维表具有维度的层次结构。比如时间维度的年、月、日等。
在这个数据仓库上,我们可以运行数据仓库分析查询,获得一些汇总结果,帮助我们对业务有所了解,以便安排后续的商业活动。
本实例的目的是让读者对关系数据库、OLAP、事实表、维表、维度层次结构等,有进一步的认识。
实验过程
模型
销售表为事实表,周围有5个维表,分别表示销售人员、产品、客户,也就是谁把什么卖给了谁。还有日期、和商店,也就是什么时间,在哪个地方。
日期维度,具有年、季度、月、日层次结构。
商店维度,具有国家、省份、城市层次结构。
产品维度,具有产品大类、产品层次结构。
建表
(以下使用Navicat作为数据库前端可视化。)
Dim_saleperson 的建表 sql 语句如下。
create table dim_saleperson (
saleperson_id char(18) primary key not null,
name char(16),
gender char(1),
birth_day char(8)
);
Dim_product 的建表 sql 语句如下。
create table dim_product (
product_id char(18) primary key not null,
name char(16),
category char(1), -- F 表示视频,D 表示日用品
price numeric(16,3)
);
Dim_customer 的建表 sql 语句如下。
create table dim_customer (
customer_id char(18) primary key not null,
name char(16),
gender char(1),
birth_day char(8)
);
Dim_date 的建表 sql 语句如下。
create table dim_date (
date_id char(18) primary key not null,
date char(8),
day integer,
month integer,
quarter integer,
year integer
);
Dim_store 的建表 sql 语句如下。
create table dim_store (
store_id char(18) primary key not null,
name char(16),
city char(16),
province char(16),
country char(16)
);
Fact_sales 的建表 sql 语句如下。
create table fact_sales (
Date_id char(18) primary key not null,
Saleperson_id char(18),
Store_id char(18),
Customer_id char(18),
Product_id char(18),
price numeric(16.3),
quantity integer,
amount numeric(16,3)
);
注入数据
- Dim_saleperson 的 insert 语句如下。
4 个销售人员:
S001, 王涛
S002, 李刚
S003, 张莉
S004, 王雪
insert into dim_saleperson values('s001', 'wang tao', 'M', '19990303');
insert into dim_saleperson values('s002', 'li gang', 'M', '19980303')