1. 程式人生 > 資料庫 >MySql之綜合練習

MySql之綜合練習

現有一個商店的資料庫,記錄顧客及其購物情況,由下面三個表組成:
商品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,'夏士蓮','聯合利華');
insert into tbl_product values(6,'雕牌',2.50,'洗衣粉','納愛斯');
insert into tbl_product values(7,'中華',3.50,'聯合利華');
insert into tbl_product values(8,'汰漬','寶潔');
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);
insert into tbl_purchase values(3,3);
insert into tbl_purchase values(3,1);
insert into tbl_purchase values(4,3,7);
insert into tbl_purchase values(4,4,3);
insert into tbl_purchase values(5,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_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_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);