现有一个商店的数据库,记录顾客及其购物情况,由下面三个表组成:
商品tbl_product(商品号productid,商品名productname,单价unitprice,商品类别category,供应商provider);
顾客tbl_customer(顾客号customerid,姓名name,住址location);
购买tbl_purchase(顾客号customerid,商品号productid,购买数量quantity);
试用SQL语言完成下列功能:
1 建表,在定义中要求声明:
(1)表中有需要的话添加主外键;
(2)顾客的姓名和商品名不能为空值;
(3)单价必须大于0,购买数量必须在0到20之间;
2 往表中插入数据:
商品(1,佳洁士,8.00,牙膏,宝洁;
2,高露洁,6.50,牙膏,高露洁;
3,洁诺,5.00,牙膏,联合利华;
4,舒肤佳,3.00,香皂,宝洁;
5,夏士莲,5.00,香皂,联合利华;
6,雕牌,2.50,洗衣粉,纳爱斯
7,中华,3.50,牙膏,联合利华;
8,汰渍,3.00,洗衣粉,宝洁;
9,碧浪,4.00,洗衣粉,宝洁;)
顾客(1,Dennis,海淀;
2,John,朝阳;
3,Tom,东城;
4,Jenny,东城;
5,Rick,西城;)
购买(1,1,3;
1,5,2;
1,8,2;
2,2,5;
2,6,4;
3,1,1;
3,5,1;
3,6,3;
3,8,1;
4,3,7;
4,4,3;
5,6,2;
5,7,8;)
商品有9 条,顾客有5条,购买有13条
3 用SQL语句完成下列查询:
(1)求购买了供应商"宝洁"产品的所有顾客;
(2)求购买的商品包含了顾客"Dennis"所购买的所有商品的顾客(姓名);
(3)求牙膏卖出数量最多的供应商。
(4)将所有的牙膏商品单价增加10%。
(5)删除从未被购买的商品记录。
-- 删除购买表
drop table if exists tbl_purchase;
-- 删除产品表
drop table if exists tbl_product;
-- 删除顾客表
drop table if exists tbl_customer;
-- 创建产品表
create table tbl_product(
productid int primary key,
productname varchar(20) not null,
unitprice double UNSIGNED,
category varchar(20),
provider varchar(20)
);
-- 插入数据
insert into tbl_product values(1,'佳洁士',8.00,'牙膏','宝洁');
insert into tbl_product values(2,'高露洁',6.50,'牙膏','高露洁');
insert into tbl_product values(3,'洁诺',5.00,'牙膏','联合利华');
insert into tbl_product values(4,'舒肤佳',3.00,'香皂','宝洁');
insert into tbl_product values(5,'夏士莲',5.00,'香皂','联合利华');
insert into tbl_product values(6,'雕牌',2.50,'洗衣粉','纳爱斯');
insert into tbl_product values(7,'中华',3.50,'牙膏','联合利华');
insert into tbl_product values(8,'汰渍',3.00,'洗衣粉','宝洁');
insert into tbl_product values(9,'碧浪',4.00,'洗衣粉','宝洁');
-- 创建顾客表
create table tbl_customer(
customerid int primary key,
name varchar(20) not null,
location varchar(20)
);
-- 插入数据
insert into tbl_customer values(1,'Dennis','海淀');
insert into tbl_customer values(2,'John','朝阳');
insert into tbl_customer values(3,'Tom','东城');
insert into tbl_customer values(4,'Jenny','东城');
insert into tbl_customer values(5,'Rick','西城');
-- 创建购买表
create table tbl_purchase(
customerid int,
productid int,
quantity int UNSIGNED,
constraint tbl_purchase_cid_fk foreign key(customerid)
references tbl_customer(customerid),
constraint tbl_purchase_pid_fk foreign key(productid)
references tbl_product(productid)
);
-- 插入数据
insert into tbl_purchase values(1,1,3);
insert into tbl_purchase values(1,5,2);
insert into tbl_purchase values(1,8,2);
insert into tbl_purchase values(2,2,5);
insert into tbl_purchase values(2,6,4);
insert into tbl_purchase values(3,1,1);
insert into tbl_purchase values(3,5,1);
insert into tbl_purchase values(3,6,3);
insert into tbl_purchase values(3,8,1);
insert into tbl_purchase values(4,3,7);
insert into tbl_purchase values(4,4,3);
insert into tbl_purchase values(5,6,2);
insert into tbl_purchase values(5,7,8);
-- 提交
commit;
(1)求购买了供应商"宝洁"产品的所有顾客;(三表连接)
SELECT distinct c.name,p.provider
from tbl_customer c join tbl_purchase pu on c.customerid=pu.customerid
join tbl_product p on pu.productid=p.productid
WHERE p.provider = '宝洁';
select distinct c.name,p.provider
from
tbl_customer c join tbl_purchase pu on c.customerid=pu.customerid
join tbl_product p on pu.productid=p.productid
where p.provider='宝洁'
(2)求购买的商品包含了顾客"Dennis"所购买的所有商品的顾客(姓名);
1.查询Dennis购买的商品 in(集合) 1 5 8
2.谁的购买的产品包含1集合的全部
3.查这些人的姓名
select name from tbl_customer
where customerid in
(select customerid from tbl_purchase
where productid in (
select pu.productid from tbl_customer c,tbl_purchase pu where c.customerid=pu.customerid and c.name='Dennis'
)
group by customerid
having count(productid)>=
(
select count(pu.productid) from tbl_customer c,tbl_purchase pu where c.customerid=pu.customerid and c.name='Dennis'
)
)and
name!='Dennis';
(3)求牙膏卖出数量最多的供应商。
--having 后可以使用组函数,where 不可以
/*这里查找到的是每个供应商卖出牙膏的数量
SELECT p.provider ,sum(quantity)
FROM tbl_product p,tbl_purchase pu,tbl_customer c
WHERE p.productid = pu.productid and pu.customerid = c.customerid AND category = '牙膏'
GROUP BY p.provider
#*/
select p.provider ,sum(quantity)
from tbl_product p,tbl_purchase pu,tbl_customer c
where p.productid=pu.productid and pu.customerid=c.customerid and category='牙膏'
group by p.provider
having sum(quantity) >= all(
select sum(quantity)
from tbl_product p,tbl_purchase pu,tbl_customer c
where p.productid=pu.productid and pu.customerid=c.customerid and category='牙膏'
group by p.provider
)
(4)将所有的牙膏商品单价增加10%。
update tbl_product set unitprice =unitprice*1.1 where category='牙膏'
(5)删除从未被购买的商品记录。
delete from tbl_product where productid not in(select productid from tbl_purchase);