Oracle-PL/SQL程序设计

PL/SQL程序设计

下面通过一些简单的例子来了解一下:

  • pl/sql程序开发方法
  • 函数的创建与调用
  • 存储过程的创建与调用
  • 触发器的创建与应用
  • 包的创建与应用
  • (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 supprice;
exception
  when no_data_found then
    dbms_output.put_line('the id is invaild!');
end sumprice;
  • (2)创建一个函数,以订单号为参数,返回该订单订购图书的价格总额
create or replace function ordersumprice(
id orderitem.order_id%type)
return books.cost%type
as
ordersumprice books.cost%type;
begin
  select sum(quantity*cost) into ordersumprice from orderitem,books
  where orderitem.isbn = books.isbn and order_id = id;
  return ordersumprice;
exception
  when no_data_found then
    dbms_output.put_line('the id is invaild!');
end orderssumprice;  
  • (3)创建一个函数,以出版社名为参数,返回该出版社出版的图书的平均价格
create or replace function publisher_id_avg(
id books.publisher_id%type)
return books.cost%type
as
publisher_id_avg books.cost%type;
begin
  select avg(cost) into publisher_id_avg from books
  where publisher_id = id;
  return publisher_id_avg;
exception
  when no_data_found then
    dbms_output.put_line('the id is invaild!');
end publisher_id_avg;
  • (4)创建一个函数,以客户号为参数,返回该客户可以获得的礼品名称
create or replace function gift_name(
id customers.customer_id%type)
return promotion.gift_id%type;
as
gift_name promotion.gift_id%type
begin
  select promotion.name into gift_name from customers,promotion
  where customers.customer_id = promotion.gift_id and customers.customer_id = id
  return gift_name;
exception
  when no_data_found then
    dbms_output.put_line('the id is invaild!');
end gift_name;
  • (5)创建一个函数,以图书号为参数,统计该图书被订购的总数量
create or replace function bookquantity(
id orderitem.isbn%type)
return orderitem.quantity%type;
as
bookquantity orderitem.quantity%type
begin
  select sum(quantity) into bookquantity from orderitem
  where orderitem.isbn = id 
  return bookquantity;
exception
  when no_data_found then
    dbms_output.put_line('the id is invaild!')
end bookquantity;
  • (6)创建一个存储过程,输出不同类型图书的数量,平均价格
create or replace procedure books_avgprice()
as
books_avgprice books.cost%type;
begin 
  for emp in (
    select  avg(cost) into books_avgprice from books) loop
    dbms_output.put_line(emp.title||''||books_avgprice);
    end loop;
end books_avgprice;
  • (7)创建一个存储过程,以客户号为参数,输出该客户订购的所有图书的名称与数量
create or replace procedure book_name_quantity(
id customers.customer_id%type,
bookname out books.title%type,
bookquantity out orderitem.quantity%type)
as
begin
  for emp in (
  select books.title into bookname,orderitem.quantity into bookquantity 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) loop
  dbms_output.put_line(emp.bookname||''||emp.bookquantity)
  end loop
exception
  when no_data_found then
    dbms_output.put_line('the id is invaild!')
end book_name_quantity;
  • (8)创建一个存储过程,以订单号为参数,输出该订单中所有图书的名称,单价,数量
create or replace procedure order_book_quantity(
id orderitem.order_id%type,
bookname out books.title%type,
bookcost out books.cost%type,
bookquantity out orderitem.quantity%type)
as
begin
  for emp in (
    select books.title into bookname,books.cost into bookcost,orderitem.quantity into bookquantity from orderitem,books
    where orderitem.isbn = books.isbn and orderitem.order_id = id)loop
    dbms_output.put_line(emp.bookname||''||emp.bookcost||''||emp.bookquantity)
    end loop
exception
  when no_data_found then
    dbms_output.put_line('the id is invaild!')
end order_book_quantity;
  • (9)创建一个存储过程,以出版社名为参数,输出该出版社出版的所有图书的名称,isbn,批发价格,零售价格信息
create or replace procedure publisherbook(
id publishers.publisher_id%type,
booktitle out books.title%type,
bookisbn out books.isbn%type,
bookcost out books.cost%type,
bookretail out books.retail%type)
as
begin
  select books.isbn into bookisbn,books.cost into bookcost,books.title into booktitle,books.retail into bookretail from publishers,books
  where publishers.publisher_id = books.publisher_id and publishers.publisher_id = id
exception
  when no_data_found then
    dbms_output.put_line('the id is invaild!')
end publisherbook;
  • (10)创建一个存储过程,输出每个客户订购的图书的数量,价格总额
create or replace procedure customer_book(
id out customers.customer_id%type,
bookquantity out orderitem.quantity%type,
bookcost out books.cost%type)
as
begin
  select customers.customer_id into id,sum(orderitem.quantity) into bookquantity,sum(quantity*cost) into bookcost from books,orders,customers,orderitem
  where customers.customer_id = orders.customer_id and orders.order_id = orderitem.order_id and orderitem.isbn = books.isbn 
exception
  when no_data_found then
    dbms_output.put_line('the id is invaild!')
end customer_book;
  • (11)创建一个存储过程,输出销售数量前三名的图书的信息及销售名次
create or replace procedure bookinformation(
booktitle books.title%type,
bookquantity books.quantity%type,
totalcost books.cost%type)
as
begin
    for emp in (
      select books.title into booktitle,sum(quantity) into bookquantity,sum(cost*quantity)into totalcost  from books group by books.title desc)
    for count in 1..3 loop(
      dbms_output.put_line(count||''||booktitle||''||bookquantity)
      end loop
end bookinformation
``
-12)创建一个存储过程,输出订购图书数量最多的客户的信息及订购图书的数量
```sql
create or replace procedure customerinformation(
id out customers.customer_id%type,
bookquantity out orderitem.quantity%type)
as
begin
  select customers.customer_id into id,max(sum(quantity)) into bookquantity from customers,books,orders,orderitem
  where customers.customer_id = orders.customer_id and orders.order_id = orderitem.order_id and orderitem.isbn = books.isbn group by customers.customer_id
end customerinformation;
  • (13)创建一个存储过程,输出各类图书中销售数量最多的图书的信息及销售的数量
create or replace procedure bookinformation(
id out books.title%type,
bookquantity out orderitem.quantity%type)
as
begin
  select books.title into id,max(sum(quantity)) into bookquantity from books,orderitem
  where books.isbn = orderitem.isbn group by books.title
end bookinformation;
  • (14)创建一个包,实现查询客户订购图书详细信息的分页显示
create or replace package pkg_emp
as
booktitle books.title%type;
booktuantity orderitem.quantity%type;
exception bound;
procedure customer_book(id out customers.customer_id);
procedure tushuxiaoliang();
end pkg_emp;

create or replace package body pkg_emp
as
procedure customer_book(id out customers.customer_id)
  as
  begin
    select customers.customer_id into id,books.title into booktitle from customers,books.orders,orderitem
    where customers.customer_id = orders.customer_id and orders.order_id = orderitem.order_id and orderitem.isbn = books.isbn
exception 
  when bound then 
    dbms_output.put_line('the id is invaild!')
end customer_book;
  • (15)创建一个包,利用集合实现图书销量排行榜的分页显示
create or replace package body pkg_emp
as
procedure tushuxiaoliang()
  begin
    select books.title into booktitle,sum(quantity) into bookquantity from books,orderitem
    where orderitem.isbn = books.isbn group by books.title desc
end tushuxiaoliang
  • (16)创建一个包,包含一个函数和一个过程。函数以图书类型为参数,返回该类型图书的平均价格。过程输出各种类型图书中价格高于同类型图书平均价格的图书信息
create or replace package pkg_book
as
booktitle books.title%type;
bookcost books.cost%type;
function book_cost();
procedure book_avgcost();
end pkg_book;
  • (17)创建一个触发器,当客户下完订单后,自动统计该订单所有图书价格总额
create or replace trigger trg_emp
after insert on books
declare
totalcost out books.cost%type;
begin
  select sum(quantity*cost) into totalcost from books,orders,orderitem
  where orders.order_id = orderitem.order_id and orderitem.isbn = books.isbn group by customers.customer_id
end trp_emp;
  • (18)创建一个触发器,禁止客户在非工作时间(早上8:00之前,晚上17:00之后)下订单
create or replace trigger trg_book
before insert or update or delete on books
begin 
  if to_char(sysdate,'hh24:mi') not between '08:00' and '18:00'
    or to_char(sysdate,'dy','nls_date_language=american') in ('sat','sun')
    then
      ralse_application_error(-20005,'只能在正常的时间内进行改变。');
      end if;
end trg_book;

创作不易,喜欢的话加个关注点个赞,蟹蟹蟹蟹٩(‘ω’)و

猜你喜欢

转载自blog.csdn.net/xu_benjamin/article/details/93240558