杨艳华oracle实验6,Oracle实验六 PL/SQL程序设计

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;

/

(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;

/

检查

调用函数看结果;

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;

/

3c2f199a7a69d5ea7eb020bed735a376.png

执行存储过程看结果;

f358d5780843cdae52bd670fa46e429c.png

触发触发器看结果

insert into orderitem values('1000','1','978-7-121-18619-8','5');

b0e104b0aea44f859c420c10aa7e0131.png

标签:customers,orderitem,books,sumprice,SQL,Oracle,type,id,PL

来源: https://blog.csdn.net/weixin_43721535/article/details/105941400

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值