Oracle PL/SQL程式設計 實驗六
實驗內容
以bs用戶登錄BOOKSALES資料庫,利用PL/SQL程式撰寫下列功能模塊。
SQL>CONN bs/bs@booksales
(1)創建一個函式,以客戶號為引數,回傳該客戶訂購圖書的價格總額。
create or replace function sumprice(
id customers.customer_id%type)
return books.cost%type
as
sumprice books.cost%type;
begin
select sum(quantity*cost) into sumprice from customers,books,orders,orderitem
where customers.customer_id = orders.customer_id and orders.order_id = orderitem.order_id and orderitem.isbn = books.isbn and customers.customer_id = id;
return sumprice;
exception
when no_data_found then
dbms_output.put_line('the id is invaild!');
end sumprice;
/[/code]
(6)創建一個存盤程序,輸出不同型別圖書的數量、平均價格。
create or replace procedure books_msg
as
books_count orderitem.quantity%type;
begin
for emp in (
select title,ISBN,cost from books
)
loop
select sum(quantity) into books_count from orderitem where ISBN=emp.ISBN;
dbms_output.put_line(emp.title||' 單價:'||emp.cost||' 數量:'||books_count);
end loop;
end books_msg;
/
(18)創建一個觸發器,禁止客戶在非作業時間(早上8:00之前,晚上17:00之后)下訂單。
CREATE OR REPLACE TRIGGER trg_orderitem
BEFORE INSERT OR UPDATE OR DELETE ON orderitem
BEGIN
IF TO_CHAR(SYSDATE,'HH24:MI') NOT BETWEEN '08:00' AND '17:00'
OR TO_CHAR(SYSDATE,'DY','NLS_DATE_LANGUAGE=AMERICAN') IN ('SAT','SUN')
THEN
RAISE_APPLICATION_ERROR(-20005,'只能在正常的時間內進行改變。');
END IF;
END trg_orderitem;
/[/code]
檢查
呼叫函式看結果;
declare
sprice books.cost%type;
begin
sprice:=sumprice(1);
dbms_output.put_line('價格'||sprice);
exception
when no_data_found then
dbms_output.put_line('not found');
end;
/

執行存盤程序看結果;

觸發觸發器看結果
insert into orderitem values('1000','1','978-7-121-18619-8','5');
uj5u.com熱心網友回復:
感謝分享,建議記錄在博客中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/11534.html
標籤:基礎和管理
上一篇:大佬們這個怎么解決呀我快哭了
