【正文】
l_quantity+。 end if。end loop。end loop。(||39。39。||v_total_quantity||39。39。||v_total_cost)。v_total_cost:=0。v_total_quantity:=0。end loop。close c_customer。end。(26)查詢比1號訂單中圖書數(shù)量多的其它訂單信息。select , from orders,orderitem where quantity (select quantity from orderitem where order_id=1001 and item_id=1)(27)查詢所以客戶及其訂購圖書的信息。select * from customers,orders where =(28)查詢沒有訂購任何圖書的客戶信息。select * from customers where customer_id not in(select customer_id from orders)(29) 查詢訂購金額最高的客戶信息。select ,, from customers, orders, orderitem where = and = and (select max(quantity) from orderitem)。(30)查詢名為“趙敏”的客戶訂購圖書的訂單信息、訂單明細(xì)。select * from orders,orderitem,customers where = and =39。趙敏39。五、實驗體會和收獲 實驗項目名稱: PL/SQL程序設(shè)計 實驗學(xué)時: 4 同組學(xué)生姓名: 實驗地點(diǎn): 1316 實驗日期: 2015/5/29 實驗成績: 批改教師: 陳愛萍 批改時間: 實驗6:PL/SQL程序設(shè)計一、實驗?zāi)康暮鸵螅?)掌握PL/SQL程序開發(fā)方法。(2)掌握函數(shù)的創(chuàng)建與調(diào)用。(3)掌握存儲過程的創(chuàng)建與調(diào)用。(4)掌握觸發(fā)器的創(chuàng)建與應(yīng)用。(5)掌握包的創(chuàng)建與應(yīng)用。(6)根據(jù)圖書銷售系統(tǒng)業(yè)務(wù)要求創(chuàng)建實現(xiàn)特定功能的函數(shù)。(7)根據(jù)圖書銷售系統(tǒng)業(yè)務(wù)要求創(chuàng)建實現(xiàn)特定功能的存儲過程。(8)根據(jù)圖書銷售系統(tǒng)業(yè)務(wù)要求創(chuàng)建實現(xiàn)特定功能的觸發(fā)器。(9)根據(jù)圖書銷售系統(tǒng)業(yè)務(wù)要求將圖書銷售系統(tǒng)相關(guān)的函數(shù)、存儲過程封裝到包里。二、實驗設(shè)備、環(huán)境 設(shè)備:奔騰Ⅳ或奔騰Ⅳ以上計算機(jī)環(huán)境:WINDOWS ORACLE 11g中文版三、實驗步驟以bs用戶登錄BOOKSALES數(shù)據(jù)庫,利用PL/SQL程序編寫下列功能模塊。(1)創(chuàng)建一個函數(shù),以客戶號為參數(shù),返回該客戶訂購圖書的價格總額。(2)創(chuàng)建一個函數(shù),以訂單號為參數(shù),返回該訂單訂購圖書的價格總額。(3)創(chuàng)建一個函數(shù),以出版社名為參數(shù),返回該出版社出版的圖書的平均價格。(4)創(chuàng)建一個函數(shù),以客戶號為參數(shù),返回該客戶可以獲得的禮品名稱。(5)創(chuàng)建一個函數(shù),以圖書號為參數(shù),統(tǒng)計該圖書被訂購的總數(shù)量。(6)創(chuàng)建一個存儲過程,輸出不同類型圖書的數(shù)量、平均價格。(7)創(chuàng)建一個存儲過程,以客戶號為參數(shù),輸出該客戶訂購的所有圖書的名稱與數(shù)量。(8)創(chuàng)建一個存儲過程,以訂單號為單數(shù),輸出該訂單中所有圖書的名稱、單價、數(shù)量。(9)創(chuàng)建一個存儲過程,以出版社名為參數(shù),輸出該出版社出版的所有圖書的名稱、ISBN、批發(fā)價格、零售價格信息。(10)創(chuàng)建一個存儲過程,輸出每個客戶訂購的圖書的數(shù)量、價格總額。(11)創(chuàng)建一個存儲過程,輸出銷售數(shù)量前3名的圖書的信息,及銷售名次。(12)創(chuàng)建一個存儲過程,輸出訂購圖書數(shù)量最多的客戶的信息,及訂購圖書的數(shù)量。四、問題解答及實驗結(jié)果(1)創(chuàng)建一個函數(shù),以客戶號為參數(shù),返回該客戶訂購圖書的價格總額。create or replace function func(p_customer_id %type)return numberas cursor c_order is (select from orderitem,orders where = and =p_customer_id)。v_order c_order%rowtype。v_total number。 v_quantity %type。v_ISBN %type。v_price %type。begin open c_order。loopfetch c_order into v_order。exit when c_order%notfound。for v_item in (select quantity,ISBN from orderitem where =)loopif 10 thenselect cost into v_price from books ,orderitem where =。elseifselect retail into v_price from books ,orderitem where =。v_total:=v_total+v_price*。end if。end loop。end loop。close c_order。return v_total。end。set serveroutput ondeclaretotal_cost %type。customer number。begin customer:=amp。x。total_cost=func(costomer)。(customer||39。39。||total_cost)。end。(2)創(chuàng)建一個函數(shù),以訂單號為參數(shù),返回該訂單訂購圖書的價格總額。create or replace function func(p_order_id %type)return numberas cursor c_order is select ISBN,quantity from orderitem where order_id=p_order_id。v_total number:=0。v_quantity %type。v_price %type。v_ISBN %type。beginfor v in c_orderloopif v_quantity10 thenselect cost into v_price from books ,orderitem where =v_ISBN。elseifselect retail into v_price from books ,orderitem where =v_ISBN。endif。v_total:= v_price*v_quantity。endif。end loop。return v_total。end。set serveroutput ondeclaretotal %type。v_order_id=%type。beginpublisher_name:=amp。x。avg_price:=func(publisher_name)。(publisher_name||39。39。||avg_price)。end。(3)創(chuàng)建一個函數(shù),以出版社名為參數(shù),返回該出版社出版的圖書的平均價格。create or replace function func(p_name %type)return numberas v_price number。beginselect avg(retail) into v_price from books,publishers where = and =p_name。return v_price。end。set serveroutput ondeclareavg_price %type。publisher_name %type。beginpublisher_name:=amp。x。avg_price:=func(publisher_name)。(publisher_name||39。39。||avg_price)。end。(4)創(chuàng)建一個函數(shù),以客戶號為參數(shù),返回該客戶可以獲得的禮品名稱。create or replace function func(p_customer_id %type)return %typeascursor c_order is select ISBN,quantity from orderitemwhere order_id in(select order_id from orders where customer_id=p_customer_id)。v_sum number:=0。v_cost number。v_name char(20)。begin for v in c_order loopselect cost,retail into v_cost,v_retail from books where ISBN=。if =10 thenv_sum:=v_sum+*v_retail。elsev_sum:=v_sum+*v_cost。endif。end loop。select name into v_name from promotion where v_sum between minretail and maxtail。return v_name。end。(5)創(chuàng)建一個函數(shù),以圖書號為參數(shù),統(tǒng)計該圖書被訂購的總數(shù)量。create or replace function func(p_ISBN %type)return numberasv_total_quantity number。beginselect sum(quantity) into v_total_quantity from orderitem where ISBN=p_ISBN。return v_total_quantity。end。(6)創(chuàng)建一個存儲過程,輸出不同類型圖書的數(shù)量、平均價格。create or replace procedure procasbeginfor v in(select category,count(*) num ,avg(cost) price from books group by category)loop(||’’||||’’||)。end loop。end。(7)創(chuàng)建一個存儲過程,以客戶號為參數(shù),輸出該客戶訂購的所有圖書的名稱與數(shù)量。create or replace procedure proc(p_customer_id %type)ascursor c_order is select title,sum(quantity) num from(select title,quantity from books join orderitem on =where order_id in(select order_id from orders where customer_id=p_customer_id))。beginfor v in c_order loop(||’’||)。end。(8)創(chuàng)建一個存儲過程,以訂單號為單數(shù),輸出該訂單中所有圖書的名稱、單價、數(shù)量。create or replace procedure proc(p_order_id %type)ascursor c_item is select title,cost,quantity from orderitem join books on order_id=p_order_id and =。beginfor v_item in c_itemloop(v_title||39。39。||v_cost||39。39。||)。end loop。end。(9)創(chuàng)建一個存儲過程,以出版社名為參數(shù),輸出該出版社出版的所有圖書的名稱、ISBN、批發(fā)價格、零售價格信息。create or replace procedure proc(p_pubName %type)\asbegincursor c_pub is select title,ISBN,cost,retail from books join publishers on = and =p_pubName。for v in c_publoop(||’’||||’’||||’’||)。end loop。end。(10)創(chuàng)建一個存儲過程,輸出每個客戶訂購的圖書的數(shù)量、價格總額。create or replace procedure procascursor c_cus is select customer_id from orders。v_sum number:=0。v_cost number。v_retail number。v_name char(20)。v_quantity number:=0。beginfor v_cus in c_cusloopfor v in (select order_id,ISBN,quantity from orderitem join orders on = and =)。loopv_quantity:=v_qua