现有一个商店的数据库,记录顾客及其购物情况,根据要求解答问题

题目: 现有一个商店的数据库,记录顾客及其购物情况,由下面三个表组成:
商品product(商品号productid,商品名productname,单价unitprice,商品类别category,供应商provider);
顾客customer(顾客号customerid,姓名name,住址location);
购买purcase(顾客号customerid,商品号productid,购买数量quantity);
每个顾客可以购买多件商品,每件商品可以被多个顾客购买,属于多对多的关系。
试用SQL语言完成下列功能:
1 建表,在定义中要求声明如下约束:
(1)每个表的主外键;
(2)顾客的姓名和商品名不能为空值;
(3)单价必须大于0,购买数量必须再0到20之间;
2 往表中插入数据:
商品(M01,佳洁士,8.00,牙膏,宝洁;
      M02,高露洁,6.50,牙膏,高露洁;
      M03,洁诺,5.00,牙膏,联合利华;
      M04,舒肤佳,3.00,香皂,宝洁;
      M05,夏士莲,5.00,香皂,联合利华;
      M06,雕牌,2.50,洗衣粉,纳爱斯
      M07,中华,3.50,牙膏,联合利华;
      M08,汰渍,3.00,洗衣粉,宝洁;
      M09,碧浪,4.00,洗衣粉,宝洁;)
顾客(C01,Dennis,海淀;
      C02,John,朝阳;
      C03,Tom,东城;
      C04,Jenny,东城;
      C05,Rick,西城;) 
购买(C01,M01,3;     
     C01,M05,2;
     C01,M08,2;    
     C02,M02,5;
     C02,M06,4;     
     C03,M01,1;
     C03,M05,1;     
     C03,M06,3; 
     C03,M08,1;    
     C04,M03,7;
     C04,M04,3;       
     C05,M06,2; 
     C05,M07,8;)
商品有9 条,顾客有5条,购买有13条
3 用PL/SQL块编程完成下列查询要求:
(1)求购买了供应商"宝洁"产品的所有顾客;
(2)求购买的商品包含了顾客"Dennis"所购买的所有商品的顾客(姓名);
(3)求牙膏卖出数量最多的供应商。
4 将所有的牙膏商品单价增加10%。(SQL语句)
5 删除从未被购买的商品记录。(SQL语句)
 
 
 
解题思路分析:
第一步:创建表,确定数据类型,建立约束
--删除数据表
drop table purcase;
drop table product;
drop table customer;
 
---创建数据表
---解题思路分析:
---第一步:创建表,确定数据类型,建立约束
----创建商品表product
 
create table product (
    productid  varchar2(10) ,
    productname varchar2(20) NOT NULL,
    unitprice number,
    category varchar2(20),
    provider varchar2(20),
    CONSTRAINT pk_productid primary key (productid),
    CONSTRAINT CK_unitprice CHECK (unitprice>0)    
);
 
--创建顾客表customer:
create table customer(
    customerid varchar2(10),
    name varchar2(20) NOT NULL,
    location varchar2(20),
    CONSTRAINT pk_customerid primary key(customerid)
);
 
--创建购买记录表 purcase:
create table purcase(
    customerid varchar2(10),
    productid varchar2(10),
    quantity number,
    CONSTRAINT FK_customerid FOREIGN KEY(customerid) REFERENCES customer(customerid) on delete cascade,
    CONSTRAINT FK_productid FOREIGN KEY(productid) REFERENCES product(productid) on delete cascade,
    CONSTRAINT CK_quantity CHECK(quantity BETWEEN 0 AND 20)
);
 
 
---测试数据的编写:
insert into product (productid,productname,unitprice,category,provider)
values('M01','佳洁士',8.00,'牙膏','宝洁');
 
insert into product (productid,productname,unitprice,category,provider)
values('M02','高露洁',6.50,'牙膏','高露洁');
 
insert into product (productid,productname,unitprice,category,provider)
values('M03','洁诺',5.00,'牙膏','联合利华');
 
insert into product (productid,productname,unitprice,category,provider)
values('M04','舒肤佳',3.00,'香皂','宝洁');
 
insert into product (productid,productname,unitprice,category,provider)
values('M05','夏士莲',5.00,'香皂','联合利华');
 
insert into product (productid,productname,unitprice,category,provider)
values('M06','雕牌',8.00,'洗衣粉','纳爱斯');
 
insert into product (productid,productname,unitprice,category,provider)
values('M07','中华',3.50,'牙膏','联合利华');
 
insert into product (productid,productname,unitprice,category,provider)
values('M08','汰渍',3.00,'洗衣粉','宝洁');
 
insert into product (productid,productname,unitprice,category,provider)
values('M09','碧浪',4.00,'洗衣粉','宝洁');
 
 
insert into customer (customerid, name ,location)
values('C01','Dennis','海淀');
 
 
insert into customer (customerid, name ,location)
values('C02','John','朝阳');
 
 
insert into customer (customerid, name ,location)
values('C03','Tom','东城');
 
 
insert into customer (customerid, name ,location)
values('C04','Jenny','东城');
 
 
insert into customer (customerid, name ,location)
values('C05','Rick','西城');
 
 
insert into purcase(customerid,productid,quantity)
values('C01','M01',3);
 
insert into purcase(customerid,productid,quantity)
values('C01','M05',2);
 
insert into purcase(customerid,productid,quantity)
values('C01','M08',2);
 
insert into purcase(customerid,productid,quantity)
values('C02','M02',5);
 
insert into purcase(customerid,productid,quantity)
values('C02','M06',4);
 
insert into purcase(customerid,productid,quantity)
values('C03','M01',1);
 
insert into purcase(customerid,productid,quantity)
values('C03','M05',1);
 
insert into purcase(customerid,productid,quantity)
values('C03','M06',3);
 
insert into purcase(customerid,productid,quantity)
values('C03','M08',1);
 
 
insert into purcase(customerid,productid,quantity)
values('C04','M03',7);
 
insert into purcase(customerid,productid,quantity)
values('C04','M04',3);
 
insert into purcase(customerid,productid,quantity)
values('C05','M06',2);
 
insert into purcase(customerid,productid,quantity)
values('C05','M07',8);
 
---提交事务
commit;
 
---问题分析
--(1)求购买了供应商"宝洁"产品的所有顾客;
 
1、确定要使用的表
    product 表:供应商信息
    customer表:顾客信息
    purcase表:顾客的购买记录
2、确定关联关系
     purcase.customerid=customer.customerid;
     purcase.productid=customer.productid;
 
第一步:先确定供应商为‘宝洁’的所有产品的产品id
  select productid from product where provider='宝洁';
 
第二步:在购买记录表中找出购买了‘宝洁’商品的客户id
 
select customerid from purcase where productid in(select productid from product where provider='宝洁');
第三步:在顾客表中,找出对应的顾客信息
 
select * from customer where customerid in (select customerid from purcase where productid in(select productid from product where provider='宝洁'));
 
 
 
---(2)求购买的商品包含了顾客"Dennis"所购买的所有商品的顾客(姓名);
        买了Dennis所购买的商品的所有的顾客信息
 
1、确定需要使用的表
   customer表:顾客id,顾客name
   purcase表:顾客的购买记录,查找所购买的物品id
   
2、确定关联的关系
     purcase.customerid=customer.customerid;
     purcase.productid=product.productid;
 
 
1、确定Dennis所购买的商品id
select customerid from customer where name='Dennis';
 
select productid from purcase where customerid =(
select customerid from customer where name='Dennis'
);
 
 
2、找出购买了Dennis的商品的顾客信息,这样的做法只能找到与Dennis买的东西有一样的就可以了
select distinct customerid from purcase where productid in ('M01','M05','M08');
 
 
select name from customer where customerid in (select distinct customerid from purcase where productid in (select productid from purcase where customerid =(
select customerid from customer where name='Dennis'
)));
 
update purcase set productid='M06' where customerid='C05' and quantity=2;
 
 
2、题目的意思是说,找出买了Dennis所有买的东西的客户,采用数学的求差的运算,求差且不再这里面的数据
select * from customer ca
where not exists(
    select productid from purcase where customerid=(
        select customerid from customer where name='Dennis'
)
    MINUS
    SELECT PRODUCTID FROM PURCASE WHERE CUSTOMERID=CA.CUSTOMERID
    AND CA.NAME<>'Dennis'
);
 
 
---(3)求牙膏卖出数量最多的供应商。
1、确定需要使用的表
   purcase表:顾客的购买记录,根据产品id查找卖出数量最多的产品
   product 表:找到牙膏的产品id,供应商
   
2、确定关联的关系
purcase.productid=product.productid;
 
1、先找出牙膏的id
select productid,provider from product where category='牙膏';
 
2、然后关联两张表,根据供应商分组后求出数量最大的值
 
select  temp.provider ,sum(quantity) he
from purcase p,(
select productid,provider from product where category='牙膏'
) temp
where p.productid in (select productid from product where category='牙膏')
and  p.productid=temp.productid
group by temp.provider;
 
update purcase set productid='M07' where customerid='C05' and quantity=8;
 
 
select max(he)
from (
select  temp.provider pro  ,sum(quantity) he
from purcase p,(
select productid,provider from product where category='牙膏'
) temp
where p.productid in (select productid from product where category='牙膏')
and  p.productid=temp.productid
group by temp.provider
) ;
 
 
 
 
select provider
from product
where productid=(
    select productid
    from purcase
    where productid in (
        select productid from product where category='牙膏'    
)
    group by productid
    having sum(quantity)=(
        select max(sum(quantity))
        from purcase
        where productid in (
        select productid from product where category='牙膏'    
)
    group by productid
)
);
 
 
 
 
 
 
posted @ 2018-07-08 13:10  Etude  阅读(4176)  评论(0编辑  收藏  举报