oracle综合实战--DDL&DML

综合实战,熟练oracle操作:
建立三张表(商品信息表,人员信息表,购买表),并插入数据,脚本执行如下:

--删除数据
drop table purcase purge;
drop table product purge;
drop table customer purge;
--创建表
create table product (
                     prodectid varchar2(3) ,
                     prodectname varchar(50) not null,
                     unitprice number not null,
                     category varchar2(20) ,
                     provider varchar2(20),
                     constraint ck_unitprice check(unitprice>0),
                     constraint pk_prodertid primary key(prodectid)
                     );
create table customer (
                     customerid varchar2(3) ,
                     name varchar(20) not null,
                     location varchar(100) not null,
                     constraint pk_customerid primary key(customerid)
                     );
create table purcase(
                     customerid varchar2(3),
                     prodectid varchar2(3),
                     quantity number,
                     constraint fk_customerid foreign key(customerid) references customer(customerid) on delete cascade,
                     constraint fk_productid foreign key (prodectid) references product(prodectid) on delete cascade,
                     constraint ck_quantity check(quantity between 0 and 20)
                     );
                     
--增加商品数据
insert into product(prodectid,prodectname,unitprice,category,provider) values ('M01','佳洁士',8.00,'牙膏','保洁');
insert into product(prodectid,prodectname,unitprice,category,provider) values ('M02','高露洁',6.00,'牙膏','高露洁');
insert into product(prodectid,prodectname,unitprice,category,provider) values ('M03','黑人',9.00,'牙膏','黑人');
insert into product(prodectid,prodectname,unitprice,category,provider) values ('M04','舒肤佳',7.50,'香皂','保洁');
insert into product(prodectid,prodectname,unitprice,category,provider) values ('M05','雕牌',5.60,'洗衣粉','黑人');
insert into product(prodectid,prodectname,unitprice,category,provider) values ('M06','中华',4.00,'牙膏','中华');
insert into product(prodectid,prodectname,unitprice,category,provider) values ('M07','汰渍',6.00,'洗衣粉','高露洁');
insert into product(prodectid,prodectname,unitprice,category,provider) values ('M08','碧浪',8.00,'洗衣粉','保洁');
insert into product(prodectid,prodectname,unitprice,category,provider) values ('M09','立白',7.50,'洗衣粉','保洁');
insert into customer(customerid,name,location) values('C01','张三','北京');
insert into customer(customerid,name,location) values('C02','李四','上海');
insert into customer(customerid,name,location) values('C03','王五','四川');
insert into customer(customerid,name,location) values('C04','麻六','成都');
insert into customer(customerid,name,location) values('C05','小明','河南');
insert into purcase(customerid,prodectid,quantity) values('C01','M01',3);
insert into purcase(customerid,prodectid,quantity) values('C01','M02',2);
insert into purcase(customerid,prodectid,quantity) values('C01','M03',8);
insert into purcase(customerid,prodectid,quantity) values('C02','M01',6);
insert into purcase(customerid,prodectid,quantity) values('C02','M02',4);
insert into purcase(customerid,prodectid,quantity) values('C02','M05',5);
insert into purcase(customerid,prodectid,quantity) values('C03','M01',2);
insert into purcase(customerid,prodectid,quantity) values('C03','M02',6);
insert into purcase(customerid,prodectid,quantity) values('C04','M01',7);
insert into purcase(customerid,prodectid,quantity) values('C04','M04',5);
insert into purcase(customerid,prodectid,quantity) values('C04','M09',2);

脚本执行完毕,开始演练查询:
1、求购买了供应商“保洁”产品的所有顾客:
第一步:先求出供应商“保洁”的所有产品

select prodectid from product where provider = '保洁'

第二步:根据购买表查出买保洁产品的顾客编号

select customerid
		 from purcase
		  where prodectid in (
		  								select prodectid 
		  								from product 
		  								where provider = '保洁')

第三步:根据顾客信息表查出顾客姓名:

select name 
		  from customer
		  where customerid in(
		  			select customerid
					from purcase
					where prodectid in (
								select prodectid 
								from product 
								where provider = '保洁')
		  									)

2、求购买的商品包含了张三购买的所有商品的顾客信息:
分析:先要知道王五买了什么商品。

select p.prodectid 
			from purcase p 
			where customerid = (
								select customerid
								 from customer
								  where name = '王五');

在这里插入图片描述
这时我们发现王五买了M01、M02。只要其他顾客也购买过这两种商品,那么这名顾客就复合要求。
怎么查出是否复合要求呢?
我们现在希望满足此条件的显示出来,不满足的不显示出。通过这我们可以想到in,但是in表示只要满足其中一个就可以显示,显然不合适。
这里就需要【not】exists()来判断了
【not】exists()判断:
in操作功能是进行一个范围的验证,并且in要求是一个数字,in只针对一个数据列的验证,但exists是针对一个数据行的验证,并且exists在验证的时候所有的数据行信息都要进行验证
我们举个例子:select * from customer where exists(select 1 from dual where 1=1)
运行此代码:在这里插入图片描述
他把所有数据全部显示出来了,此判断的含义就是将customer每一行记录与和exists的子查询的结果进行比较。现在程序是判断子查询中是否有数据返回,如果有数据就通过,则当前比较行的数据就会显示,如果没有数据返回,就不返回customer数据

我们可以使用exists来判断,判断是否有满足条件的内容出现,如果没有,则结果是空,就不显示数据,否则就显示数据;
则我们的代码就可以这样写:

select * from customer ca
 where [not]exists(
 					是否可以匹配的查询)

我们的关键就在于这个查询怎么写:
我们可以这样分析,首先,我们知道王五购买的商品是 01,02

select p.prodectid from purcase p where customerid = (select customerid customerwhere name = '王五');

我们在看一下张三购买的商品:01,02,03

select p.prodectid from purcase p where customerid = (select customerid customerwhere name = '张三');

我们这样想:如果用王五的减去张三的呢,既取交集

select p.prodectid from purcase p where customerid = (select customerid customerwhere name = '王五');
miuns
select p.prodectid from purcase p where customerid = (select customerid customerwhere name = '张三');

结果为null

扫描二维码关注公众号,回复: 5709579 查看本文章

我们再来看一下麻六购买的商品:01,04

select p.prodectid from purcase p where customerid = (select customerid customerwhere name = '麻六');

王五的减去麻六的:

select p.prodectid from purcase p where customerid = (select customerid customerwhere name = '王五');
miuns
select p.prodectid from purcase p where customerid = (select customerid customerwhere name = '张三');

我们发现01,02-01,04=02
我们发现符合要求的交集为null,不符合要求的交集为不为空
我们就可以根据not exists来判断,我们这样写代码:

 select * from customer  ca
where not exists (
        select p.prodectid from purcase p where customerid = (select customerid  from customer where name = '王五')
         minus
         select p.prodectid from purcase p where customerid = ca.customerid  
         ) and ca.name <>'王五'

解释:我们用王五购买的商品与customer中的第一个人购买的商品做交集运算,如果值为空,则说明第一个人复合要求,因为用了not exists,所以值为空的会被现实出来。
然后再与customer中的第二个人购买的商品做交集…
直至与customer每一行数据既每一个人做交集运算,这样就会把符合要求的全部显示出来。当然要把王五去掉

3、求牙膏卖出最多的供应商:
先分析用到的表:
product:供应商信息
purcase:统计信息
关系:product.prodectid=purcase .prodectid
第一步,连表求出品牌为牙膏的供应商,和所卖出的数量

select product.provider,sum(purcase.quantity)
from product,purcase
where product.prodectid=purcase .prodectid and product.category = '牙膏'
group by product.provider

第二步:求出卖出最多的供应商数量(这个时候我们就需要嵌套统计函数了,但是我们在嵌套统计函数的时候,查询的不能再跟分组条件了)

select max(sum(purcase.quantity))
from product,purcase
where product.prodectid=purcase .prodectid and product.category = '牙膏'
group by product.provider

第三步:求出卖出数量最多的供应商信息,根据最大的数量来得出

select product.provider,sum(purcase.quantity) sum
from product,purcase
where product.prodectid=purcase .prodectid and product.category = '牙膏'
group by product.provider
having  sum(purcase.quantity) = (
						    select max(sum(purcase.quantity))
						    from product,purcase
						    where product.prodectid=purcase .prodectid and product.category = '牙膏'
						    group by product.provider)

猜你喜欢

转载自blog.csdn.net/enbaoIT/article/details/88261517