⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 作业预定book_trigger.txt

📁 orale培训教材包括了所有的sql说明和实例
💻 TXT
字号:

--orderbook2--有书直接插入预定表booked,用它的trigger update book表
create or replace procedure orderbook2 ( p_isbn in char , p_number in number)
is 
  v_name book.name%TYPE;--用type方法声明变量
  v_price number;
  v_book number:=0; --判断有几条纪录
begin
  select count(*) into v_book from book where isbn=p_isbn; --有无此书
  if v_book =0 then 
    dbms_output.put_line('sorry 。没有此书号的图书.您的定购未成功');
  else
    --有书直接插入预定表booked,用它的trigger update book表
    --update book set shuliang=shuliang - p_number where isbn=p_isbn;
    --更新图书表
    insert into booked values( p_isbn, p_number,user);
    --更新预定信息
    commit;
    dbms_output.put_line('您的图书订购成功 ' );
    select name,price into v_name,v_price from book
    where isbn = p_isbn;
    dbms_output.put_line('ISBN : ' || p_isbn);
    dbms_output.put_line('书名 : ' || v_name);
    dbms_output.put_line('售价 : ' || v_price);
    dbms_output.put_line('您订购了 : ' || p_number || '本');
  end if;
end;

--直接插入预定表booked,用它的trigger update book表
create or replace trigger updatebook after insert on booked for each row
begin
  update book set shuliang=shuliang - :new.benshu where isbn=:new.isbn;
end;

call orderbook2('7-5053-6548-2',30);

--注意:此时不管任何方式插入 booked表都会触发此trigger.所以,调用orderbook本身
--将调用两次update book.

--目前已经缺货的图书
create table quehuo(
isbn char(13),
shuliang number(4,0));

--update book之后 trigger 触发,更新quehuo
create or replace trigger set_quehuo after update on book
 for each row
begin
  if :new.shuliang <0 then 
   update quehuo set shuliang=:new.shuliang where isbn=:new.isbn ;
   if sql%notfound then 
	--如果原来不缺货,则插入新纪录。使用sql%notfound判断update是否更新了纪录
     insert into quehuo values(:new.isbn,:new.shuliang);
   end if;
  end if;
end;

--测试
 call orderbook2('7-5053-6548-2',999);
  select * from book;
 call orderbook2('7-5053-6548-7',2);
 select * from book;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -