达梦数据库练习笔记

达梦数据库练习笔记

  • 1. 数据表和约束管理
    • 1.1 使用SQL命令完成数据表的创建
    • 1.2 添加 Vspace 项目中数据表之间的关联系
  • 2. 数据查询练习
    • 2.1 查询价格在 50.0 到 100.0 元之间的商品信息
    • 2.2 查询商品标题中包含”肉”的商品信息
    • 2.3 查询购买订单数量超过 11 个的用户手机号码
    • 2.4 使用 top, limit 和伪列三种方式实现商品信息的分页查询
      • 2.4.1 使用 top 分页查询
      • 2.4.2 limit 分页查询
      • 2.4.3 伪列分页查询
  • 3. 高级查询以及业务管理
    • 3.1 使用多表内联接(两种方式) 查询用户手机号码、密码、注册时间、昵称信息
    • 3.2 查询用户手机号、昵称、下单时间、订单商品标题、商品数量、总价等信息
    • 3.3 分页查询订单编号、用户身份证号、商品名称、商品数据量、总价
    • 3.4 同步更新数据
    • 3.5 统计二级类型最多的一级类型
    • 3.6 查询统计每年销量最好的商品
    • 3.7 查询统计在去年的基础上今年每个季度的销售总额增长情况
    • 3.8 查询今年购物消费前 10 名的用户信息
    • 3.9 将销售订单量最少的商品下架
    • 3.10 查询今年的销售额相对于历年销售额的增长率
  • 4. 总结

| 本次SQL语句练习使用的是国产的达梦数据库, 数据库数据和练习题目由@Teacher熊 提供

1. 数据表和约束管理

1.1 使用SQL命令完成数据表的创建

-- 创建用户表
create table vspace.i_user (
	phone char(11) not null  primary key,
	password varchar(20) not null
);

-- 创建基本信息表
create table vspace.i_basic(
	phone char(11) not null  primary key,
	name varchar(30) not null,
	id_card varchar(18) not null,
	birthday datetime not null,
	reg_date datetime not null,
	last_login_date datetime not null,
	head_images varchar(100) not null
);

-- 创建商品分类表
create table vspace.i_category(
	CATEGORY_ID	int not null primary key,
	CATEGORY_LEVEL	int not null,
	CATEGORY_NAME varchar(20) not null,
	PARENT_ID int not null
);

-- 创建商品表
create table vspace.i_goods(
	GOODS_ID bigint not null primary key,
	GOODS_TITLE	varchar(1024),
	PRICE double,
	DISCOUNT double,
	SPECIFICATION varchar(100),
	DESCRIPTION text,
	CATEGORY_ID int,
	AMMOUNT	int,
	IMAGE_URL text,
	UP_DATE datetime,	
	DOWN_DATE datetime
);

-- 创建购物车信息表
create table vspace.i_cart(
	CART_ID	bigint not null primary key,
	PHONE char(11),
	JOIN_DATE datetime,	
	GOODS_ID bigint,
	AMMOUNT int
);

-- 创建订单表
create table vspace.i_order(
	ORDER_ID bigint not null primary key,
	PHONE char(11),
	ORDER_DATE	datetime,
	GOODS_ID bigint,
	AMMOUNT	int,
	SUM_PRICE	number(10,2),
	ORDER_STATUS varchar(50),
	RECEIVE_ADDR_ID bigint
);

-- 创建配送地址表
create table vspace.i_receive_addr(
	ADDR_ID bigint not null primary key,
	PROV varchar(50),
	CITY varchar(50),
	SECT	varchar(50),
	DETAIL	varchar(100),
	RECEIVE_NAME varchar(30),	
	RECEIVE_PHONE char(11),	
	OWN_USER_PHONE char(11)
);

1.2 添加 Vspace 项目中数据表之间的关联系

alter table vspace.i_basic 
add constraint FK_basic_phone 
foreign key (phone) references vspace.i_user(phone);

alter table vspace.i_goods 
add constraint FK_category_id 
foreign key (category_id) references vspace.i_category(category_id);

alter table vspace.i_cart 
add constraint FK_user_phone 
foreign key (phone) references vspace.i_user(phone);

alter table vspace.i_cart 
add constraint FK_goods_id 
foreign key (goods_id) references vspace.i_goods(goods_id);

alter table vspace.i_receive_addr 
add constraint FK_own_user_phone 
foreign key(OWN_USER_PHONE) references vspace.i_user(phone);

达梦数据库练习笔记_第1张图片

2. 数据查询练习

2.1 查询价格在 50.0 到 100.0 元之间的商品信息

select * from vspace.i_goods where price>=50 and price<=100

达梦数据库练习笔记_第2张图片

2.2 查询商品标题中包含”肉”的商品信息

select * from vspace.i_goods where description like '%肉%';

达梦数据库练习笔记_第3张图片

2.3 查询购买订单数量超过 11 个的用户手机号码

select phone from (
		select count(*) cnt, phone 
		from vspace.i_order 
		group by phone
) d1
where d1.cnt>11;

达梦数据库练习笔记_第4张图片

2.4 使用 top, limit 和伪列三种方式实现商品信息的分页查询

使用 top, limit 和伪列三种方式实现商品信息的分页查询

2.4.1 使用 top 分页查询

select top 3 * from vspace.i_goods 
where goods_id not in (
		select top 12 goods_id from vspace.i_goods
);

2.4.2 limit 分页查询

select * from (
		select rownum num , * 
		from vspace.i_goods 
		where rownum <= 15
)t where t.num > 12;

2.4.3 伪列分页查询

select * from (
		select rownum num , * 
		from vspace.i_goods 
		where rownum <= 15
)t where t.num > 12;

达梦数据库练习笔记_第5张图片

3. 高级查询以及业务管理

3.1 使用多表内联接(两种方式) 查询用户手机号码、密码、注册时间、昵称信息

select u.phone, u.password, b.reg_date, b.name
from vspace.i_user u, vspace.i_basic b
where u.phone = b.phone
select u.phone, u.password, b.reg_date, b.name
from vspace.i_user u inner join vspace.i_basic b on (u.phone = b.phone)

达梦数据库练习笔记_第6张图片

3.2 查询用户手机号、昵称、下单时间、订单商品标题、商品数量、总价等信息

select u.phone, b.name, o.order_date, g.goods_title, o.ammount, o.sum_price
from vspace.i_user u, vspace.i_basic b, vspace.i_order o, vspace.i_goods g
where b.phone=o.phone and o.goods_id=g.goods_id

达梦数据库练习笔记_第7张图片

3.3 分页查询订单编号、用户身份证号、商品名称、商品数据量、总价

要求每页显示 7 条,显示第 5 页的数据

select o.order_id, b.id_card,
	(select goods_title from vspace.i_goods where goods_id=o.goods_id),
o.ammount, o.sum_price
from vspace.i_order o, vspace.i_basic b
where o.phone=b.phone
limit 28, 7;

达梦数据库练习笔记_第8张图片

3.4 同步更新数据

同步更新数据: 每个订单的总价 = 该订单中商品的单价 * 购买数量

update vspace.i_order o
set sum_price = 
		(select price from vspace.i_goods where goods_id=o.goods_id) * ammount;
commit;
select * from vspace.i_order;

达梦数据库练习笔记_第9张图片

  • 由于考虑到订单数量特别大, 如果直接进行数据更新效率非常低,考虑 分批次进行更新
    -- 比如:第一批次
    update vspace.i_order o
    set sum_price = 
    	(select price from vspace.i_goods where goods_id=o.goods_id) * ammount
    where order_id in (
    	-- 使用分页查询技巧
    	select order_id from (
    		select rownum num, * from (
    		 select * from vsapce.i_order
    		) t2 where rownum <= 100000 
    	)t1 where tq.num >= 0 
    );
    
    commit;
    

3.5 统计二级类型最多的一级类型

  • 按照 parent_id 进行分组统计每一个一级类型下的子类型个数

    select parent_id num from vspace.i_category
    group by parent_id;
    
  • 对上述结果进行筛选,得到最多的一级类型

    select parent_id,count(category_id) num 
    from vspace.i_category 
    where parent_id != 0 
    group by parent_id;
    
    • 可对第一步查询统计的结果进行降序排列取第一个

          select parent_id, count(category_id) num 
          from vspace.i_category 
          where parent_id != 0 
          group by parent_id 
          order by count(category_id) desc 
          limit 0,1;
      
    • 使用max() 聚合函数

        select max(t.num) 
        from (
        	select parent_id,count(category_id) num 
        	from vspace.i_category 
        	where parent_id != 0 
        	group by parent_id
        ) t;
        ```
      
  • 使用子查询得到详细数据

    select * from vspace.i_category 
    where category_id =(
    	select parent_id 
    	from vspace.i_category 
    	where parent_id != 0 
    	group by parent_id 
    	order by count(category_id) desc 
    	limit 0,1
    );
    
    达梦数据库练习笔记_第10张图片

3.6 查询统计每年销量最好的商品

  • 按照每年每种商品的销售数量(按照年份、编号、销量分组)

    select year(order_date), goods_id, sum(ammount) total
    from vspace.i_order 
    group by year(order_date), goods_id
    
  • 按年份分组,使用 max() 聚合函数

    select t.y, max(t.total) max_total
    from (
    	select year(order_date) y, goods_id, sum(ammount) total
    	from vspace.i_order 
    	group by year(order_date), goods_id
    ) t
    group by t.y
    
  • 将第一步结果作为虚拟表 a, 第二步中结果当作虚拟表 b, 然后对 ab 表做内联查询

    select a.* from (
    	select year(order_date) y, goods_id, sum(ammount) total
    	from vspace.i_order 
    	group by year(order_date), goods_id
    ) a, (
    	select y, max(total) max_total
    	from (
    		select year(order_date) y, goods_id, sum(ammount) total
    		from vspace.i_order 
    		group by year(order_date), goods_id
    	) t
    	group by y
    ) b
    where a.y = b.y and a.total = b.max_total
    
    达梦数据库练习笔记_第11张图片

3.7 查询统计在去年的基础上今年每个季度的销售总额增长情况

同比数据分析

  • 查询统计出去年每个季度销售总额

    select datepart(QUARTER, order_date), sum(sum_price) total
    from vsapce.i_order
    where year(order_date) = year(now()) - 1
    group by datepart(QUARTER, order_date);
    
  • 查询统计出今年每个季度销售总额

    select datepart(QUARTER, order_date), sum(sum_price) total
    from vsapce.i_order
    where year(order_date) = year(now())
    group by datepart(QUARTER, order_date);
    
  • 求增量

    select a.season Season, 
    			 concat(round( (b.total - a.total) / a.total * 100, 2), '%') "Growth"
    from (
    	select datepart(QUARTER, order_date) season, sum(sum_price) total
    	from vspace.i_order
    	where year(order_date) = year(now()) - 1
    	group by datepart(QUARTER, order_date)
    ) a, (
    	select datepart(QUARTER, order_date) season, sum(sum_price) total
    	from vspace.i_order
    	where year(order_date) = year(now())
    	group by datepart(QUARTER, order_date)
    ) b
    where a.season = b.season
    order by a.season asc;
    
    达梦数据库练习笔记_第12张图片

3.8 查询今年购物消费前 10 名的用户信息

  • 统计每个用户今年消费总额

    select phone, sum(sum_price) sp from vspace.i_order 
    where year(order_date) = year(now())
    group by phone;
    
  • 按照消费总数降序取出前十名

    select phone, sum(sum_price) "Consumption" 
    from vspace.i_order 
    where year(order_date) = year(now()) - 1
    group by phone
    order by sum(sum_price) desc
    limit 0, 10;
    

    考虑消费总额相同的情况

    • 使用 distinct 关键字去掉重复的总销售额, 然后降序去前10个销售额度

      select distinct sum(sum_price) num from vspace.i_order 
      where year(order_date) = year(now())
      group by phone
      order by num desc limit 0, 10;
      
    • 联查

      select a.* from (
      	select sum(sum_price) num, phone
      	from vspace.i_order where year(order_date) = year(now())
      	group by phone
      ) a, (
      	select distinct sum(sum_price) num from vspace.i_order 
      	where year(order_date) = year(now())
      	group by phone
      	order by num desc limit 0, 10
      ) b
      where a.num = b.num
      
  • 获取用户信息

    select *
    from vspace.i_basic b, (
    	select phone, sum(sum_price) "Consumption"  from vspace.i_order 
    	where year(order_date) = year(now()) - 1
    	group by phone
    	order by sum(sum_price) desc
    	limit 0, 10
    )top10
    where b.phone = top10.phone;
    
    达梦数据库练习笔记_第13张图片

3.9 将销售订单量最少的商品下架

| 下架: 将商品下架日期设置为当前系统日期

  1. 查询每种商品的订单量

    select goods_id, sum(ammount) num
    from vspace.i_order
    group by goods_id
    
  2. 查询最少的订单量

    select min(num)
    from (
    	select goods_id, sum(ammount) num
    	from vspace.i_order
    	group by goods_id
    )
    
  3. 查询订单量最少的商品

    select t1.goods_id, t2.min_num
    from (
    	select goods_id, sum(ammount) num
    	from vspace.i_order
    	group by goods_id
    )t1, (
    	select min(num) min_num
    	from (
    		select goods_id, sum(ammount) num
    		from vspace.i_order
    		group by goods_id
    	)
    )t2
    where t1.num = t2.min_num;
    
  4. 将销售订单量最少的商品下架

    update vspace.i_goods
    set down_date = curdate()
    where goods_id in (
    	select t1.goods_id
    	from (
    		select goods_id, sum(ammount) num
    		from vspace.i_order
    		group by goods_id
    	)t1, (
    		select min(num) min_num
    		from (
    			select goods_id, sum(ammount) num
    			from vspace.i_order
    			group by goods_id
    		)
    	)t2
    	where t1.num = t2.min_num
    );
    
  5. 查看是否修改成功

    select * 
    from vspace.i_goods g, (
    	select t1.goods_id, t2.min_num
    	from (
    		select goods_id, sum(ammount) num
    		from vspace.i_order
    		group by goods_id
    	)t1, (
    		select min(num) min_num
    		from (
    			select goods_id, sum(ammount) num
    			from vspace.i_order
    			group by goods_id
    		)
    	)t2
    	where t1.num = t2.min_num
    )t
    where t.goods_id = g.goods_id;
    

3.10 查询今年的销售额相对于历年销售额的增长率

  1. 按年提取往年销售额

    select year(order_date), sum(SUM_PRICE)
    from vspace.i_order
    where year(order_date) != year(now())
    group by year(order_date);
    
  2. 提取今年销售额

    select year(now()), sum(SUM_PRICE)
    from vspace.i_order
    where year(order_date)=year(now());
    
  3. 使用 往年数据与今年数据进行计算

    select b.y YEAR, 
    		concat(round((a.sp - b.sp) / b.sp * 100, 3), '%') "Growth Compared with 2020"
    from (
    	select year(now()), sum(SUM_PRICE) sp
    	from vspace.i_order
    	where year(order_date)=year(now())
    ) a , (
    	select year(order_date) y, sum(SUM_PRICE) sp
    	from vspace.i_order
    	where year(order_date) != year(now()) 
    	group by year(order_date)
    ) b
    order by b.y asc;
    
    达梦数据库练习笔记_第14张图片

4. 总结

在大学数据库课程的学习中,主要关注的是数据库相关的理论知识以及SQL语句的基本用法。对于多个表联查练习得较少。通过本次练习,我对于数据库得数据查询操作更加熟练。对于复杂得查询,需要将查询操作一步一步拆分,最终通过 嵌套查询(子查询) 获得最终所需要的数据。

你可能感兴趣的:(数据库,sql)