題目: 現有一個商店的數據庫,記錄顧客及其購物情況,由下面三個表組成:
商品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
)
);