標籤:
21,預存程序,簡化複雜操作,增加資料獨立性,提高安全性,提高效能
與函數建立對比:
create or replace function fun_01(v_01 in number) return number--必須要求有傳回值 as result number; begin result := power(v_01,2); return result; end; /Function created.select fun_01(9) from dual; FUN_01(9)---------- 81
預存程序建立樣本,不要求有傳回值:
create or replace procedure pro_01(v_01 in number) as result number; begin result := power(v_01,2); dbms_output.put_line(result); end; /Procedure created.execute pro_01(9);81PL/SQL procedure successfully completed.
22,觸發器,一種特殊的預存程序
create table log_tab--建立一個日誌表 ( l_id number(3), l_old varchar2(20),--刪除和已更新的主鍵 l_type varchar2(10),--執行的操作類型 l_new varchar2(20),--添加和已更新的主鍵 l_date date,--更改的日期 constraint pk_log_tab primary key(l_id) );Table created.
create sequence log_tab_id--建立一個序列 minvalue 100 maxvalue 300 start with 100 increment by 1;Sequence created.
create or replace trigger tr_student02--建立一個觸發器 after insert or update or delete –後插入更新刪除 on student02--針對student02表 for each row—針對行觸發 begin case when inserting then—插入觸發 insert into log_tab values ( log_tab_id.nextval, null, ‘insert‘, :new.sno,--匯入inserted表中資料 sysdate); when deleting then—刪除觸發 insert into log_tab values ( log_tab_id.nextval, :old.sno,--匯入deleted表中資料 ‘delete‘, null, sysdate); when updating then—更新觸發 insert into log_tab values ( log_tab_id.nextval, :old.sno, ‘update‘, :new.sno, sysdate); end case; end;
delete from student02 where rownum = 1;insert into student02(sno,sname,ssex) values(123,‘hook‘,‘m‘); update student02 set sname = ‘tokl‘ where sno = 123; select * from log_tab; L_ID L_OLD L_TYPE L_NEW L_DATE---------- -------------------- ---------- -------------------- --------- 101 109 delete 28-OCT-15 102 insert 123 28-OCT-15 103 123 update 123 28-OCT-15
23,Oracle別名as使用的時候,不能使用在表上,只能使用在列上,表使用空格別名。。
24,萬用字元‘_‘使用的時候,如果欄位使用的char型,固定長度,需要用萬用字元補全
25,排序 空值在前在後 nulls first ..nulls last
Oracle資料庫零散知識03