oracle_day5_程序包

---1創建一個程序包,完成如下要求:
  --1:根據輸入的商品編號,查找該商品價格和庫存。
  --2:根據輸入的用戶名稱,獲取該用戶的訂單信息
  --3:獲取當前時間的三天前時間 






  --4:查詢當前時間的一週前時間
  select sYSDATE-7 from   DUAL;
  create or replace package package01
  is
  
  procedure  select_pro(vid number,info out sys_refcursor);
  procedure  select_orderinfo(vrelname VARCHAR2,oinfo out sys_refcursor);
  function    seltime1() return date;
    function    seltime2() return date;
  
  end package01;
  
    create or replace package body  package01
  is
  
  procedure  select_pro(vid number,info out sys_refcursor)
    is
    begin
    open info for  select price,stockcount from es_product where id=vid;
      end;
  procedure  select_orderinfo(vrelname VARCHAR2,oinfo out sys_refcursor)
    is
     begin
       open oinfo for  
         select es_order.* from 
  es_user,es_order
  where es_user.id=es_order.user_id
  and es_user.realname=vrelname;
       end;
       
         function  seltime1  return date
           is
           dat1  date;
           begin
            select sysdate-3 into dat1 from   DUAL; 
            return   dat1 ;
             end;
         
    function  seltime2 return date
      is
         dat2  date;
           begin
                select sYSDATE-7 into dat2 from   DUAL; 
                return    dat2;
             end;
  
  end package01;
  
  
  ----------------------------
  
  
  declare 
 info  sys_refcursor;
oinfo  sys_refcursor;
  vprice es_product.price%type;
   vso es_product.stockcount%type;
   vrelname es_user.realname%type;
   orderinfo es_order%rowtype;
   times1  date;
    times2  date;
 
  begin
    package01.select_pro(1,info);
    package01.select_orderinfo('張管',oinfo);
   times1:=package01.seltime1();
   times2:=package01.seltime2();
    loop
      fetch info into  vprice,vso;
      exit when info%notfound ;
     dbms_output.put_line(vprice); 
      end loop;
      loop
      fetch oinfo into  orderinfo;
      exit when oinfo%notfound ;
     dbms_output.put_line('訂單編號:'||orderinfo.id||',下單人'||orderinfo.realname); 
        end loop;
        dbms_output.put_line(to_char(times1,'yyyy-mm-dd hh24:mi.ss')); 
          dbms_output.put_line(to_char(times2,'yyyy-mm-dd hh24:mi.ss'));
    end;
  
  
  
------------------------------------
-2 監控用戶表的增刪改操作,如果增加的
   用戶usertype爲2要在監控表中做一個記錄


   如果刪除usertype爲2記錄的用戶,扔出系統異常不允許刪除
   如果修改usertype爲2記錄的用戶,修改成普通用戶,也需要在監控表中記錄
   
   create table jiank(
   dec varchar2(100),
   time date
   )
   
   
   create or replace trigger tri_userinfo
   before update or delete or insert on es_user for each row
  
   begin
      if inserting then
       insert into jiank values('記錄添加',sysdate);
       elsif  deleting then
       insert into jiank values('記錄刪除',sysdate);
       elsif updating then
       insert into jiank values('記錄修改了',sysdate);
     else
       insert into jiank values('xxxx',sysdate);
       end if;
     end;


--------------------------------------

create table userlog
(
  event varchar2(20),
  descx  varchar2(30),
  dotime date
)


create trigger user_add_trig
after insert  on es_user
for each row
begin
  if :new.usertype=2 then
    insert into userlog values ('insert','增加了一個管理員',sysdate);
  end if;
end;




create trigger user_del_trig
after delete  on es_user
for each row
begin
  if :old.usertype=2 then
     raise_application_error(-20010,'管理員不允許刪');
  end if;
end;




create trigger user_mod_trig
after update  on es_user
for each row
begin
  if :old.usertype=2 then
     insert into userlog values ('update','修改管理員爲其他用戶',sysdate);
  end if;
end;

------------------------------------------


  
 insert into ES_USER (id, username, password, realname, tel, address, zip, email, usertype)
values (10, 'vipuser', 'vipuser', '陳紅xxxx', '13801000104', '北京市中關村4號', '100100', '[email protected]', 2);
  update  ES_USER set  realname='ssss' where id=10;
  
  delete from es_user where id=10
  select * from es_user;
  
  
  select * from jiank;
  
  --3 創建一個學員表,並且爲學員表創建一個序列,插入5條數據,主鍵值用序列的值
  drop table stui;
  create table stui(
  id number(4) primary key,
  name varchar2(100)
  )
  drop sequence st_seqss;
  create sequence st_seqss
  start with 1
  increment by 1
  ;
  declare
  begin
    for ind in 1..5 loop
        insert   into stui  values(st_seqss.nextval,'sutdent');
      end loop;
    end;
    
    truncate table stui;


  select * from stui;
  
  
  
  
  


--4 在商品表的價格列上創建索引,在商品表的類型列上添加位圖索引
drop index product_price_index;
create index index_product_price on es_product(price);
create bitmap index index_product_sort_id on ES_PRODUCT(sort_id);
select * from es_product;


--5 創建一個視圖,顯示商品名,價格,類型名,庫存等

--5創建視圖
create view  product_viewx as
select t.name,t.stockcount,t.price,s.sortname from es_product t ,es_sort s 
where t.sort_id= s.id




create trigger product_viewx_insert 
instead of insert on product_viewx
declare
  v_sortid  number;
begin
  --根據類別名查類別id
   select id into v_sortid from es_sort t 
   where t.sortname=:new.sortname; 
   --插入商品表數據
   insert into es_product(id,name,price,stockcount,sort_id)
   values(product_seq.nextval,:new.name,:new.price,:new.stockcount,v_sortid);
end;

--------------------

create sequence product_seq start with 16


insert into product_viewx values('xx',55,5,'類別8')

發佈了29 篇原創文章 · 獲贊 3 · 訪問量 9970
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章