Oracle大型数据库 作业9

Posted 霜序0.2℃

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle大型数据库 作业9相关的知识,希望对你有一定的参考价值。

以bs用户登录BOOKSALES数据库,利用PL/SQL程序编写下列功能模块。

1

创建一个函数,以客户号为参数,返回该客户订购图书的价格总额。

create or replace function func_customer_qty(p_custoer_id customers,customer_id%type)
return books.retail%type
as
v_qty books.retail%type;
begin
select books.retail*orderitem.quantity into v_qty from books.orderitem,orders where books.isbn=orderitem.isbn and orders.customer_id=p_customer_id and oirders.order_id=orderitem.order_id;
return v_qty;
end;

2

创建一个函数,以订单号为参数,返回该订单订购图书的价格总额。

create or replace function func_order_qty(
p_order_id orders.order_id%type)
return books.retail%type
as
v_qty books.retail%typr;
begin
select sum(books.retail*orderitem.quantity) into v_qty from books.orderitem.orders
where books.isbn=ordertiem.isbn and orders.order_id=p_order_id and orders.order_id=orderitem.order_id;
return v_qty;
exception
when no_data_found then
dbms_output.put_line('th order_id is invalid');
end;

3

创建一个函数,以出版社名为参数,返回该出版社出版的图书的平均价格。

create or replace function func_publish_avg(p_name publshers.name%type)
return numberas v_avg number;
begin
   select avg(cost) into v_avg from books where publisher_id=(select publisher_id from publishers where name=p_name);
   return v_avg;
end;

4

创建一个函数,以客户号为参数,返回该客户可以获得的礼品名称。

create or replace function func_customer_gift(
p_custmoer_id customers, customer_id%type)
return promotion.name%type
as v_gift promotion.name%type;
v_qty books.retail%type;
begin
select sum(books.retail*orderitem.quantity) into v_qty from books.orderitem,orders where books.isbn=orderitem.isbn and orders.customer_id = p_customer_id and orders.order_id=orderitem.order_id;
end if;
return v_gift;
exception
when no_data_found then
dbms_putput.put_line('the customer_id is error');
end;

5

创建一个函数,以图书号为参数,统计该图书被订购的总数量。

create or replace function func_order_totle(p_isbn books,isbn%type)
return number 
as
v_total number:=0;
begin
select sum(quantity) into v_total form orderitem where isbn=p_isbn;
return v_total;
end;

6

创建一个存储过程,输出不同类型图书的数量、平均价格。

create or replace procedure proc)_book_total
as 
begin
for v in(
select category,conut(*) c, avg(cost) a from books group by category)
loop
dbms_output.put_line(v.category||' '||v.v||' ' ||v.a);
end loop;
end;

7

创建一个存储过程,以客户号为参数,输出该客户订购的所有图书的名称与数量。

create or repace procedure proc_cid_total(p_cid customers,customer_id%type)
as
cursor c_book is select title,quantity num from
(select title,quantity from books join orderitem on books.inbn=orderitem.isbn)
begin
for v in v_book loop
dbms_output.put_line(v.title||' ' ||v.numn);
end loop;
end;

8

创建一个存储过程,以订单号为参数,输出该订单中所有图书的名称、单价、数量。

create or replace procedure proc_order_total
(p_oid orders. order id%type) ascursor c book is
select title, cost, quantity from books join orderitem on books. isbn-orderitem. isbn where order_id=p oid;beginend;for v in c book 
loop
dbms_output.put_line (v. titlell' , Ilv. quantityll' ,Ilv.cost); 
end loop;
end;

9

创建一个存储过程,以出版社名为参数,输出该出版社出版的所有图书的名称、ISBN、批发价格、零售价格信息。

create or replace procedure proc_title_static
as
      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

创建一个存储过程,输出每个客户订购的图书的数量、价格总额。

  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;



11

创建一个存储过程,输出销售数量前3名的图书的信息及销售名次。

set serveroutput on
declare
 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_cost
as 
  v_order_id  orders.order_id%type;
end;
/
create or replace trigger trg_before_order
before insert  on ORDERS 
for each row
begin
   order_total_cost.v_order_id:=:new.order_id;
end;
/
set serveroutput on
create or replace trigger trg_order
after 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

创建一个存储过程,输出各类图书中销售数量最多的图书的信息及销售的数量。

create or replace package order_total_cost
as 
 v_order_id  orders.order_id%type;
end;
/
create or replace trigger trg_before_order
before insert  on ORDERS 
for each row
begin
  order_total_cost.v_order_id:=:new.order_id;
end;
/
set serveroutput on
create or replace trigger trg_order
after 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;

14

创建一个包,实现查询客户订购图书详细信息的分页显示。


create or replace function get_sumcost(
    v_customer_id customers.customer_id%type)
return number
as
   
   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;
      exit 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 on
declare
	v_totalMoney BOOKS.cost%type;
        v_customer number;
begin
	v_customer :=&x;
		v_totalMoney:=get_sumcost(v_customer);
		dbms_output.put_line(v_customer||v_totalMoney);
	
end;


15

创建一个包,利用集合实现图书销量排行榜的分页显示。

create or replace function get_sumcost(
   v_customer_id customers.customer_id%type)
return number
as
  
  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;
     exit 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 on
declare
   v_totalMoney BOOKS.cost%type;
       v_customer number;
begin
   v_customer :=&x;
   	v_totalMoney:=get_sumcost(v_customer);
   	dbms_output.put_line(v_customer||v_totalMoney);
   
end;


16

创建一个包,包含一个函数和一个过程。函数以图书类型为参数,返回该类型图书的平均价格。过程输出各种类型图书中价格高于同类型图书平均价格的图书信息。

create or replace function get_pub_avgcost(
   v_pub_name publishers.name%type)
return number
as
  
  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 on
declare
   v_avgMoney BOOKS.cost%type;
       v_pubname publishers.name%type;
begin
   v_pubname :=&x;
   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 number
as
   
   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;
      exit 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 on
declare
	v_totalMoney BOOKS.cost%type;
        v_customer number;
begin
	v_customer :=&x;
		v_totalMoney:=get_sumcost(v_customer);
		dbms_output.put_line(v_customer||'的礼物是'||v_totalMoney);
	
end;

18

创建一个触发器,禁止客户在非工作时间(早上8:00之前,晚上17:00之后)下订单。

create or replace function get_sumnum(
    v_customer_id customers.customer_id%type)
return number
as
   
   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 orderitemOracle大型数据库 作业7 (空)

Oracle大型数据库 作业5 (空)

Oracle大型数据库 作业6 (别看了,真的是空的,补作业用的)

Oracle大型数据库 作业10

Oracle大型数据库 作业11

大型数据库作业