1.数据库设计
数据库的设计是基础,数据库设计的目标是为用户和各种应用系统提供一个基础的信息设施和高效的运行环境。
1.1 数据库设计三个范式
-
1NF: 所有的域是原子性的
表中的每一列应该是不可拆分的最小单元。最低要求。
-
2NF: 所有的非主键字段必须与主键相关,不能与部分主键相关(联合主键)
每张表中描述和表示一类数据,多种不同的数据需要拆分到多张表中。
-
3NF: 所有非主键字段必须与主键直接相关,而不能依赖于其他非主键字段
数据不能具有传递依赖,每个字段与主键的关系是直接的而非间接的。
1.2 E-R模型
Entity Relationship Model: 实体关系模型
提供了表示实体类型、属性和联系的方法,用来描述现实世界的概念模型。
1)表之间的关系
-
一对一: 表A的一条记录对应表B的一条记录,反之亦然(个人信息表和档案表)
实现:
- 外键添加唯一约束
2)主键做外键
-
一对多: 表A的一条记录对应表B的多条记录。(部门表和雇员表)
实现:
添加外键(多方)
-
多对多: 表A的一条记录可以对应表B的多条记录,表B的一条记录也可以对应表A的多条记录。
实现:
1)添加关系表
2)设置联合主键
2)实体关系模型图(ER)
矩形框:表示实体,在框中记入实体名。
菱形框:表示联系,在框中记入联系名。
椭圆形框:表示实体或联系的属性,将属性名记入框中。对于主属性名,则在其名称下划一下划线。
连线:实体与属性之间;实体与联系之间;联系与属性之间用直线相连,并在直线上标注联系的类型。
- 示例
3)常用的数据库设计工具
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n49" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">powerdesinger </pre>
2.商城3C产品数据分析案例
设计实体及其关系
-
数据分析案例
-
统计各个城市销售额的前10名
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="sql" cid="n59" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">1) 创建城市维度表: 包含区域信息,省份信息和城市信息
create table china_orgin_dim(
d_id int,
d_name varchar(40), # 区域信息
p_id int,
p_name varchar(40), # 省份信息
c_id int,
c_name varchar(40) # 城市信息
);- 向城市维度表插入数据
select distinct length(c_id) from china;
c_id为3表示区域,6代表省份,9代表城市,12代表区。
insert into china_orgin_dim
select c.,c3.c_id c_id,c3.c_name c_name from china c3,
(select c1.c_id d_id,c1.c_name dname,c2.c_id p_id,c2.c_name p_name from china c2,
(select * from china where superior_c_id = 0) c1
where c2.superior_c_id = c1.c_id) c
where c3.superior_c_id = c.p_id;
3)查看各个城市的销售额
set @rank=0; #设置变量0
select p_name,c_name,sales,
case
when length(sales_e) >8 then concat(round(sales_e/100000000,2),'亿')
when length(sales_e) >7 then concat(round(sales_e/10000000,2),'千万')
end sales_e,@rank:=@rank+1 rank from
(select p_name,c_name,sum(amount) sales,
sum(priceamount) sales_e from product
inner join order_detail on product.pid = order_detail.pid
inner join users on users.user_no = order_detail.user_no
inner join china_orgin_dim on users.c_id = china_orgin_dim.c_id
group by p_name,c_name order by sales_e desc limit 10) sale;</pre>[图片上传失败...(image-d76cb5-1588486793844)]
- 向城市维度表插入数据
-
统计不同年龄层次的消费及其占比情况
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="sql" cid="n64" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">1)创建年龄分级表
create table age_level(
grade int primary key auto_increment,
lowage int not null,
hiage int not null
);是否包含? [10,20)
insert into AGE_LEVEL(lowage, hiage)
values (10, 20),(20, 30),(30, 40),(40, 50),(50, 80);
2)查看比例字符串连接 # 分组之后实现销量(order_detail)聚合运算
select concat(lowage,'-',hiage),sum(amount) sales,
流程函数(销售额) sum(price * amount) 按照年龄分组实现销售额的聚合
case
when length(sum(priceamount)) >8 then concat(round(sum(priceamount)/100000000,2),'亿')
when length(sum(priceamount)) >7 then concat(round(sum(priceamount)/10000000,2),'千万')
end sales_e,比例 该年龄层级的销售额/总销售额 * 100 %
concat(round(sum(priceamount)/(select sum(priceamount) from order_detail
inner join product on order_detail.pid = product.pid)*100,2),'%')from order_detail
inner join users on order_detail.user_no = users.user_no
inner join product on order_detail.pid = product.pid
inner join age_level on age >= lowage and age < hiage # 年龄范围[10,20)
group by grade;
</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n62" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">* 统计2018年度月销售量及环比变化比例
select thisMonth.period as Period,
thisMonth.amount as thisMonthAmount,lastmonth.amount as lastMonthAmount,
concat(round((thisMonth.amount-lastmonth.amount)*100/lastmonth.amount,2),'%') as Rate
from提取(从日期中提取指定的部分)
(select extract(YEAR_MONTH from order_date) as period,sum(amount) as amount
from order_detail
group by period) as thisMonth
left join
(select extract(YEAR_MONTH from (DATE_ADD(order_date,INTERVAL 1 MONTH))) as period,sum(amount) as amount
from order_detail
group by period) as lastMonth
on thisMonth.period=lastMonth.period </pre>[图片上传失败...(image-7aa35f-1588486793844)]
-
[图片上传失败...(image-606d26-1588486793844)]