欢迎来到装配图网! | 帮助中心 装配图网zhuangpeitu.com!
装配图网
ImageVerifierCode 换一换
首页 装配图网 > 资源分类 > DOC文档下载
 

实验6PLSQL程序设计

  • 资源ID:71714936       资源大小:48.50KB        全文页数:11页
  • 资源格式: DOC        下载积分:20积分
快捷下载 游客一键下载
会员登录下载
微信登录下载
三方登录下载: 微信开放平台登录 支付宝登录   QQ登录   微博登录  
二维码
微信扫一扫登录
下载资源需要20积分
邮箱/手机:
温馨提示:
用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
支付方式: 支付宝    微信支付   
验证码:   换一换

 
账号:
密码:
验证码:   换一换
  忘记密码?
    
友情提示
2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

实验6PLSQL程序设计

-实验6 PL/SQL程序设计1 实验目的(1) 掌握PL/SQL程序开发方法。(2) 掌握存储过程、函数、触发器、包的创立于调用。2 实验要求(1) 根据图书销售系统业务要求创立特定的存储过程、函数、触发器。(2) 根据图书销售系统业务要求将图书销售系统相关的函数、存储过程封装到包里。3 实验步骤以bs用户登录BOOKSALES数据库,利用PL/SQL程序编写以下功能模块。(1) 创立一个存储过程,输出不同类型图书的数量、平均价格。SQL> create or replace procedure proc_category_static 2 as 3 -定义游标,获取当前有哪些图书种类 4 cursor c_all_category is select distinct category from books; 5 -图书的平均价格 6 v_avg_cost number; 7 begin 8 -保存图书种类 9 for v_each_category in c_all_category LOOP 10 select avg(retail) into v_avg_cost from books where category=v_each_category.category group by category; 11 dbms_output.put_line('种类为:'|v_each_category.category|',平均价格为:'| v_avg_cost); 12 END LOOP; 13 end proc_category_static; 14 /(2) 创立一个存储过程,以客户号为参数,输出该客户订购的所有图书的名称与数量。create or replace procedure proc_get_orderinfo( 2 p_customer_id customers.customer_id%type) 3 as 4 -声明游标存储客户的订单号 5 cursor c_orderid is select order_id from orders where customer_id=p_customer_id; 6 v_orderid orders.order_id%type; 7 -声明游标存储订单信息 8 cursor c_orderitem is select ISBN, sum(quantity) totalnum from orderitem where order_id=v_orderid group by ISBN; 9 -保存图书的书名 10 v_title books.title%type; 11 12 begin 13 open c_orderid; 14 LOOP 15 fetch c_orderid into v_orderid; 16 e*it when c_orderid%NOTFOUND; 17 for v_orderitem in c_orderitem LOOP 18 select title into v_title from books where ISBN=v_orderitem.ISBN; 19 DBMS_OUTPUT.PUT_LINE(p_customer_id|'订购'|v_title|'的数量是'|v_orderitem.totalnum); 20 end LOOP; 21 end LOOP; 22 close c_orderid; 23 end proc_get_orderinfo; 24 /e*ec proc_get_orderinfoo(1001);(3) 创立一个存储过程,以订单号为参数,输出该订单中所有图书的名称、单价、数量。create or replace procedure proc_get_orderinfoo( p_order_id orderitem.order_id%type)as -声明游标存储订单号的ISBN cursor c_ISBN is select ISBN from orderitem where order_id=p_order_id; v_ISBN orderitem.ISBN%type; -声明游标存储订单信息 cursor c_orderitem is select ISBN,sum(quantity) totalnum from orderitem where ISBN=v_ISBN ; v_title books.title%type; v_retail books.retail%type;begin open c_ISBN; LOOP fetch c_ISBN into v_ISBN; e*it when c_ISBN%NOTFOUND; for v_orderitem in c_orderitem LOOP select title,retail into v_title,v_retail from books where ISBN=v_orderitem.ISBN; DBMS_OUTPUT.PUT_LINE(p_order_id|v_title|v_retail|v_orderitem.totalnum); end LOOP; end LOOP; close c_ISBN;end proc_get_orderinfoo;/(4) 创立一个存储过程,以名为参数,输出该出版的所有图书的名称、ISBN、批发价格、零售价格信息。create or replace procedure proc_get_name( p_title books.title%type)as cursor c_orderid is select order_id from orders where customer_id=p_customer_id; v_orderid orders.order_id%type; cursor c_orderitem is select ISBN, sum(quantity) totalnum from orderitem where order_id=v_orderid group by ISBN; v_title books.title%type;begin open c_orderid; LOOP fetch c_orderid into v_orderid; e*it when c_orderid%NOTFOUND; for v_orderitem in c_orderitem LOOP select title into v_title from books where ISBN=v_orderitem.ISBN; DBMS_OUTPUT.PUT_LINE(p_customer_id|''|v_title|'的数量是'|v_orderitem.totalnum); end LOOP; end LOOP; close c_orderid;end proc_get_orderinfo;/set serveroutput ondeclarev_customer number;beginv_customer :=&*;proc_get_orderinfo(v_customer);end;/(5) 创立一个存储过程,输出每个客户订购的图书的数量、价格总额。create or replace procedure proc_category_staticas cursor c_all_category is select distinct category from books; v_sum_cost number;begin for v_each_category in c_all_category LOOP select sum(retail) into v_sum_cost from books where category=v_each_category.category group by category; dbms_output.put_line('种类为:'|v_each_category.category|',总价格为:'| v_sum_cost); END LOOP;end proc_category_static;/set serveroutput one*ec proc_category_static;/(6) 创立一个存储过程,输出销售数量前3名的图书的信息及销售名次。create or replace procedure proc_category_staticas cursor c_all_category is select distinct category from books; v_sum_retail number;begin for v_each_category in c_all_category LOOP select sum(cost) into v_sum_retail from books where category=v_each_category.category group by category; dbms_output.put_line('种类为:'|v_each_category.category|',数量为:'| v_sum_retail); END LOOP;end proc_category_static;/set serveroutput one*ec proc_category_static; (7) 创立一个存储过程,输出订购图书数量最多的客户的信息及订购图书的数量。(8) 创立一个存储过程,输出各类图书中销售数量最多的图书的信息及销售的数量。(9) 创立一个包,实现查询客户订购图书详细信息的分页显示。create or replace procedure proc_title_staticas cursor c_all_title is select distinct title from books; v_sum_retail number;begin for v_each_title in c_all_title LOOP select sum(cost) into v_sum_retail from books where title=v_each_title.title group by title; dbms_output.put_line('信息为:'|v_each_title.title|',数量为:'| v_sum_retail); END LOOP;end proc_title_static;/(10) 创立一个包,利用集合实现图书销售排行榜的分页显示。(11) 创立一个包,包含一个函数和一个过程。函数以图书类型为参数,返回该类型图书的平均价格。过程输出各种类型图书中价格高于同类型图书平均价格的图书信息。create or replace package pkg_bookas function get_book_avgcost(p_book_category BOOKS.category%type) return number; procedure pro_showbook(p_book_category BOOKS.category%type);end;/create or replace package body pkg_bookas function get_book_avgcost(p_book_category BOOKS.category%type) return number as v_ISBN BOOKS.ISBN%type; cursor c_books is select retail from BOOKS where ISBN=v_ISBN; v_sumcost number(6,2):=0; v_count number(6) :=0; v_avgcost number :=0; v_book_category varchar2(10); begin select ISBN into v_ISBN from BOOKS where category=v_book_category; for v_retail in c_books LOOP v_count:=v_count+1; v_sumcost:= v_sumcost+v_retail.retail; end LOOP; v_avgcost:=v_sumcost/v_count; DBMS_OUTPUT.PUT_LINE(v_book_category| '-'|v_avgcost); return v_avgcost; end; procedure pro_showbook(p_book_category BOOKS.category%type) as v_book_category varchar2(10); cursor c_books is select * from BOOKS where retail>=get_book_avgcost(v_book_category); begin for v_books in c_books loop dbms_output.put_line(v_books.ISBN|' '|v_books.title|' '|v_books.author|' '|v_books.pubdate|' '|v_books.publisher_id|' '|v_books.retail); end loop; end;end;/set serveroutput ondeclare p_book_category BOOKS.category%type; avgcost number;begin p_book_category:='管理' avgcost:=pkg_book.get_book_avgcost(p_book_category); pkg_book.pro_showbook('管理');end;/(12) 创立一个触发器,当客户下完订单后,自动统计该订单所有图书价格总额。create or replace package order_total_costas v_order_id orders.order_id%type;end;/create or replace trigger trg_before_orderbefore insert on ORDERS for each rowbegin order_total_cost.v_order_id:=:new.order_id;end;/set serveroutput oncreate or replace trigger trg_orderafter insert on ORDERitem declare cursor c_orderitem is select ISBN, quantity from orderitem where order_id=order_total_cost.v_order_id; v_ISBN orderitem.ISBN%type; v_quantity orderitem.quantity%type; v_cost books.cost%type; v_sumcost number(6,2):=0;begin for v_orderitem in c_orderitem LOOP if v_orderitem.quantity >10 then select cost into v_cost from books where ISBN = v_orderitem.ISBN; DBMS_OUTPUT.PUT_LINE('1-'|v_cost|':'|v_orderitem.ISBN); elsif v_orderitem.quantity<=10 then select retail into v_cost from books where ISBN = v_orderitem.ISBN; DBMS_OUTPUT.PUT_LINE('2-'|v_cost|':'|v_orderitem.ISBN); else DBMS_OUTPUT.PUT_LINE('number of book is error!'); end if; v_sumcost:= v_sumcost+v_orderitem.quantity*v_cost; DBMS_OUTPUT.PUT_LINE('3*'|'now v_sumcost is'|v_sumcost); end LOOP;end;/(13) 创立一个触发器,制止客户在非工作时间早上8:00之前,晚上17:00之后下订单。(14) 创立一个函数,以客户号为参数,返回该客户订购图书的价格总额。 create or replace function get_sumcost( v_customer_id customers.customer_id%type)return numberas cursor c_orderid is select order_id from orders where customer_id=v_customer_id; v_orderid orders.order_id%type; cursor c_orderitem is select ISBN, quantity from orderitem where order_id=v_orderid; v_ISBN orderitem.ISBN%type; v_quantity orderitem.quantity%type; v_cost books.cost%type; v_sumcost number(6,2):=0;begin open c_orderid; LOOP fetch c_orderid into v_orderid; e*it when c_orderid%NOTFOUND; for v_orderitem in c_orderitem LOOP if v_orderitem.quantity >10 then select cost into v_cost from books where ISBN = v_orderitem.ISBN; DBMS_OUTPUT.PUT_LINE('1-'|v_cost|v_orderitem.ISBN); elsif v_orderitem.quantity<=10 then select retail into v_cost from books where ISBN = v_orderitem.ISBN; DBMS_OUTPUT.PUT_LINE('2-'|v_cost|v_orderitem.ISBN); else DBMS_OUTPUT.PUT_LINE('number of book is error!'); end if; v_sumcost:= v_sumcost+v_orderitem.quantity*v_cost; DBMS_OUTPUT.PUT_LINE('3*'|v_sumcost); end LOOP; end LOOP; close c_orderid; return v_sumcost;end get_sumcost;/set serveroutput ondeclarev_totalMoney BOOKS.cost%type; v_customer number;beginv_customer :=&*;v_totalMoney:=get_sumcost(v_customer);dbms_output.put_line(v_customer|'的购置总额是'|v_totalMoney);end;/(15) 创立一个函数,以订单号为参数,返回该订单订购图书的价格总额。(16) 创立一个函数,以名为参数,返回该出版的图书的平均价格。create or replace function get_pub_avgcost( v_pub_name publishers.name%type)return numberas v_pub_id publishers.publisher_id%type; cursor c_books is select retail from books where publisher_id=v_pub_id; v_sumcost number(6,2):=0; v_count number(6) :=0;begin select publisher_id into v_pub_id from publishers where name=v_pub_name; for v_retail in c_books LOOP v_count:=v_count+1; v_sumcost:= v_sumcost+v_retail.retail; DBMS_OUTPUT.PUT_LINE(v_count| '-'|v_sumcost); end LOOP; return v_sumcost;end get_pub_avgcost;/set serveroutput ondeclarev_avgMoney BOOKS.cost%type; v_pubname publishers.name%type;beginv_pubname :=&*;v_avgMoney:=get_pub_avgcost(v_pubname);dbms_output.put_line(v_pubname|'的出幅员书的平均价格是'|v_avgMoney);end;/(17) 创立一个函数,以客户号为参数,返回该客户可以获得的礼品名称。create or replace function get_gift( v_customer_id customers.customer_id%type)return numberas cursor c_orderid is select order_id from orders where customer_id=v_customer_id; v_orderid orders.order_id%type; cursor c_orderitem is select ISBN, quantity from orderitem where order_id=v_orderid; v_ISBN orderitem.ISBN%type; v_quantity orderitem.quantity%type; v_cost books.cost%type; v_gift number(6,2):=0;begin open c_orderid; LOOP fetch c_orderid into v_orderid; e*it when c_orderid%NOTFOUND; for v_orderitem in c_orderitem LOOP if v_orderitem.quantity >10 then select cost into v_cost from books where ISBN = v_orderitem.ISBN; DBMS_OUTPUT.PUT_LINE('1-'|v_cost|v_orderitem.ISBN); elsif v_orderitem.quantity<=10 then select retail into v_cost from books where ISBN = v_orderitem.ISBN; DBMS_OUTPUT.PUT_LINE('2-'|v_cost|v_orderitem.ISBN); else DBMS_OUTPUT.PUT_LINE('number of book is error!'); end if; v_gift:= v_gift+v_orderitem.quantity*v_cost; DBMS_OUTPUT.PUT_LINE('3*'|v_gift); end LOOP; end LOOP; close c_orderid; return v_gift;end get_gift;/set serveroutput ondeclarev_totalMoney BOOKS.cost%type; v_customer number;beginv_customer :=&*;v_totalMoney:=get_sumcost(v_customer);dbms_output.put_line(v_customer|'的礼物是'|v_totalMoney);end;/(18) 创立一个函数,以图书号为参数,统计该图书被订购的总数量。create or replace function get_sumnum( v_customer_id customers.customer_id%type)return numberas cursor c_orderid is select order_id from orders where customer_id=v_customer_id; v_orderid orders.order_id%type; cursor c_orderitem is select ISBN, quantity from orderitem where order_id=v_orderid; v_ISBN orderitem.ISBN%type; v_quantity orderitem.quantity%type; v_retail books.retail%type; v_sumnum number(6,2):=0;begin open c_orderid; LOOP fetch c_orderid into v_orderid; e*it when c_orderid%NOTFOUND; for v_orderitem in c_orderitem LOOP if v_orderitem.quantity >10 then select retail into v_retail from books where ISBN = v_orderitem.ISBN; DBMS_OUTPUT.PUT_LINE('1-'|v_retail|v_orderitem.ISBN); elsif v_orderitem.quantity<=10 then select retail into v_retail from books where ISBN = v_orderitem.ISBN; DBMS_OUTPUT.PUT_LINE('2-'|v_retail|v_orderitem.ISBN); else DBMS_OUTPUT.PUT_LINE('number of book is error!'); end if; v_sumnum:= v_sumnum+v_orderitem.quantity*v_retail; DBMS_OUTPUT.PUT_LINE('3*'|v_sumnum); end LOOP; end LOOP; close c_orderid; return v_sumnum;end get_sumnum;/set serveroutput ondeclarev_totalMoney BOOKS.cost%type; v_customer number;beginv_customer :=&*;v_totalMoney:=get_sumnum(v_customer);dbms_output.put_line(v_customer|'的数量是'|v_totalMoney);end;/4 实验总结本次实验难度大,代码多,锻炼了我的代码的书写能力和逻辑思维能力,实验过程遇到许多的困难,通过上网查询和求助同学最终将大局部问题解决。. z.

注意事项

本文(实验6PLSQL程序设计)为本站会员(dg****3)主动上传,装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知装配图网(点击联系客服),我们立即给予删除!

温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!