Oracle觸發(fā)器
觸發(fā)器是個(gè)特殊的存儲(chǔ)過(guò)程,它的執(zhí)行不是由程序調(diào)用,也不是手工啟動(dòng),而是由事件來(lái)觸發(fā),下文中就為大家詳細(xì)講解Oracle觸發(fā)器。
創(chuàng)建觸發(fā)器(行級(jí)觸發(fā)器)
create or replace trigger tri_update_emp_bak
after update
on emp_bak
for each row 每更新一行 就觸發(fā)一次
begin
Oracle 里面 對(duì)觸發(fā)器 也提供了特殊的對(duì)象 :NEW :OLD 來(lái)訪問(wèn) 更新前后的數(shù)據(jù)
dbms_output.put_line('更新后' || :NEW.sal);
dbms_output.put_line('更新前' || :OLD.sal);
if updating then
end if;
if inserting
end;
創(chuàng)建觸發(fā)器(行級(jí)觸發(fā)器)(前置)
create or replace trigger tri3_update_emp_bak
before update
on emp_bak
for each row 每更新一行 就觸發(fā)一次
begin
oracle 里面 對(duì)觸發(fā)器 也提供了特殊的對(duì)象 :NEW :OLD 來(lái)訪問(wèn) 更新前后的數(shù)據(jù)
dbms_output.put_line('更新后' || :NEW.sal);
dbms_output.put_line('更新前' || :OLD.sal);
end;
select * from emp_bak
update emp_bak set sal = 1000 where empno in (7788)
創(chuàng)建觸發(fā)器(表級(jí)觸發(fā)器)
//表級(jí)別觸發(fā)器里面 不允許使用 :NEW :OLD 變量
create or replace trigger tri2_update_emp_bak
after update
on emp_bak
begin
dbms_output.put_line('更新后' || :NEW.sal);
dbms_output.put_line('更新前' || :OLD.sal);
end;
創(chuàng)建觸發(fā)器(行級(jí)觸發(fā)器)
create or replace trigger tri4_update_emp_bak
after update of sal
on emp_bak
for each row 每更新一行 就觸發(fā)一次
begin
oracle 里面 對(duì)觸發(fā)器 也提供了特殊的對(duì)象 :NEW :OLD 來(lái)訪問(wèn) 更新前后的數(shù)據(jù)
dbms_output.put_line('更新后' || :NEW.sal);
dbms_output.put_line('更新前' || :OLD.sal);
end;
create table userinfo
(
userid number(4) PRimary key,
username varchar2(20)
)
create table addrinfo
(
addrid number(4) primary key,
addname varchar2(20),
userid number(4) references userinfo(userid)
)
insert into userinfo values (1,'李四')
insert into addrinfo values (1,'湖北武漢',1)
select * from addrinfo
delete from userinfo where userid =1
級(jí)聯(lián)刪除
create or replace trigger tri_userinfo_delete
before delete
on userinfo
for each row
begin
delete from addrinfo where userid = :OLD.userid;
insert into
end;
create table dept_bak as select * from dept
create or replace view myview
as
select a.empno,a.ename,a.sal,b.dname
from emp a inner join dept b
update myview set sal = 1000,dname='aaaaa' where empno = 7499
替代觸發(fā)器 用在視圖上面 只能是行級(jí)的
替代觸發(fā)器 特點(diǎn)是 真正的操作已經(jīng) 變成了一個(gè)動(dòng)作而已 功能由觸發(fā)器來(lái)完成
create or replace trigger tri_myview
instead of update
on myview
begin
dbms_output.put_line('刪除操作執(zhí)行了。。。');
update emp set sal = :NEW.sal where empno = :OLD.empno;
update dept set dname = :NEW.dname where dname = :OLD.dname;
delete from emp where empno = :OLD.empno;
end;
select * from emp;
select * from dept;
update emp_bak set ename ='aaaa',sal = 10000 where empno in (7788)
系統(tǒng)包
產(chǎn)生隨機(jī)數(shù)
**** **** **** ****
dbms_random.value 0-1 之間的隨機(jī)小數(shù)
dbms_random.random 隨機(jī)整數(shù)
select dbms_random.value from dual;
create or replace procedure proc_cardno(mycardno out varchar2)
as
tempcard varchar2(50);
cardno varchar2(19);
begin
tempcard:= dbms_random.value;
cardno:=substr(tempcard,2,4)||' '||substr(tempcard,6,4)||' '||substr(tempcard,10,4)||' '||substr(tempcard,14,4);
mycardno:=cardno;
dbms_output.put_line(tempcard);
dbms_output.put_line(cardno);
end;
使用UTL_FILE包讀寫(xiě)文件
create directory MY_DIR as 'd:\temp';
create directory MY_DIR2 as 'd:\temp2';
declare
定義文件對(duì)象
myfile utl_file.file_type;
定義變量 用來(lái)存儲(chǔ)每讀出一行的數(shù)據(jù)
linestr varchar2(200);
begin
打開(kāi)文件
myfile:= utl_file.fopen('MY_DIR','oracle.log','r');
進(jìn)行讀取(循環(huán))
loop
utl_file.get_line(myfile,linestr);
dbms_output.put_line(linestr);
end loop;
exception
when others then
utl_file.fclose(myfile);
dbms_output.put_line('讀取完畢!');
end;
declare
定義文件對(duì)象
myfile utl_file.file_type;
定義變量 用來(lái)存儲(chǔ)每讀出一行的數(shù)據(jù)
linestr varchar2(200);
begin
打開(kāi)文件
myfile:= utl_file.fopen('MY_DIR','oracle.log','a');
追加數(shù)據(jù)
utl_file.get_line(myfile,linestr);
utl_file.put_line(myfile,'你好,這是新的數(shù)據(jù)',true);
utl_file.fclose(myfile);
exception
when others then
utl_file.fclose(myfile);
dbms_output.put_line('讀取完畢!');
end;
拷貝
declare
定義文件對(duì)象
sourcefile utl_file.file_type;
targetfile utl_file.file_type;
定義變量 用來(lái)存儲(chǔ)每讀出一行的數(shù)據(jù)
linestr varchar2(200);
begin
打開(kāi)文件
sourcefile:= utl_file.fopen('MY_DIR','oracle.log','r');
targetfile:= utl_file.fopen('MY_DIR2','copy.txt','a');
進(jìn)行讀取(循環(huán)) 并且寫(xiě)入到新的文件里面
loop
utl_file.get_line(sourcefile,linestr);
utl_file.put_line(targetfile,linestr,true);
dbms_output.put_line(linestr);
end loop;
exception
when others then
utl_file.fclose(sourcefile);
utl_file.fclose(targetfile);
dbms_output.put_line('拷貝完畢!');
end;
關(guān)于Oracle觸發(fā)器的問(wèn)題就為大家講解到這里,還有很多Oracle觸發(fā)器的知識(shí)這里沒(méi)有涉及到,以后我還會(huì)繼續(xù)為大家講解更多的關(guān)于Oracle觸發(fā)器的知識(shí)點(diǎn),希望對(duì)大家能夠有所幫助。