DB課堂練習


各表關系

CUSTOMER(客戶表)

列名 數據類型 為空 注釋
CID char (11) 客戶號
Name varchar2(16) 姓名
Gender Varchar2(3) 性別
Rating int 等級分
Level varchar2(20) 用戶等級

Orders

列名 數據類型 為空 注釋
OID char (12) 訂單號
Order_times date 下單時間
CID char(11) 客戶號
Total number(10,2) 訂單金額

Order_Detail

列名 數據類型 為空 注釋
OID char (12) 訂單號
No int 序號
GID int 貨物編號
Num int 購買數量
Price number(8,2) 購買單價

1.查詢姓名中含"勇"字的男性客戶的客戶號,姓名,用戶等級(3分)

select CID,Name,grade
from customer
where Name like '%勇%' and Gender = 'male'

2.新增一個客戶,客戶號13173201234,姓名"張三",性別"男"(3分)

insert into customer (CID,name,gender)
valus('13173201234',張三,'男')

3.將"謝勇"所有的訂單中的訂單金額打八折(4分)

update orders 
set total=total*0.8
where cid in (
          select cid
          from customer
          where name = '謝勇' );

update orders a
set total=total*0.8
while exits (
      select 1 
      from   customer b
      where a.id=b.id and b.name='謝勇');

4.刪除從來沒下過單的客戶(4分)

delete from customer 
where C_ID not in(
      select C_ID
      from orders)

5.建立一個試圖V1(CID,Name,Gender,Total_Order,Count_Order),視圖表示用戶的所有訂單的總額與訂單總數,列分別為客戶號,姓名,性別,訂單總額,訂單總數(只需要查詢下過單的用戶)(6分)

createview v1 as

select CID,Name,Gender,sum(total) as Total_Order ,count(*) as Count_Order

from customer natural join orders 

group by CID,Name,Gender

6.將訂單金額超過10000元的,進行每滿500減100的優惠,訂單金額超過5000元的進行每滿500減50的優惠,
訂單金額不超過5000元的進行每滿500減30的優惠(6分)

update orders

set total =case 
      when total<=5000 then total -floor(total/500)*30
      when total>5000 **(可以不要)**and total<=10000 then total - floor(total /500)*50
      else total -floor(total/500) *100
end 

7.查詢每個客戶訂單額最大的一筆訂單的訂單號,下單時間、訂單金額(6分)

關聯子查詢

select OID,CID,Order_times,Total

from  Orders a
where Total = (
      select  max(total)
      from Orders b 
      where a.cid=b.cid 
)


select oid a.cid,total
from orders a,
(select cid,max(total) max_total from orders group by cid) b
where a.cid=b.cid and total = max_total

8.查詢對於客戶號為“13173201234”的客戶所買過的所有商品,全部買過的客戶的客戶號和姓名。(6分)

with ta as 
(select cid , gid 
frome orders natural join order_detail)
select  a.cid,a.name
from customer a
where not exists (
 
(select gid from ta where cid = '13173201234')
minus
(select gid from ta b where a.cid=b.cid)

)

兩個not exists的做法
with ta as 
(select cid , gid 
frome orders natural join order_detail)
select  a.cid,a.name
from customer a
where not exists (
select gid from ta c where cid = '13173201234'
and not exists (select 1 from ta b where a.cid=b.cid and c.gid = b.gid)
)

9.基於視圖v1,使用標量子查詢,查詢男、女兩類用戶中訂單總金額最多的前10位用戶。(6分)

select * from(

select a.cid,a.name,a.gender,(select count(*)+1 from v1 b
where b.sum_total>a.sum_total and a.gender = b.gender) rk
from v1 a) where rk>=10

select a.cid,a.name,a.gender
from v1 a
where (select count(*)+1 from v1 b
where b.sun_total > a.sum_total and a.gender = b.gender)>=10

10.統計各用戶等級中男性客戶人數、女性客戶人數和總人數。(6分)

交叉表查詢(不確定是不是叫這個)

select grade,count(*) "總數",
sum (case gender when '男' then 1 else  null end ) "男客戶數"
sum (case gender when '女' then 1 else  null end ) "女客戶數"
from customer group by grade


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM