一、觸發(fā)器
1.觸發(fā)器在數(shù)據(jù)庫里以獨(dú)立的對(duì)象存儲(chǔ),
2.觸發(fā)器不需要調(diào)用,它由一個(gè)事件來觸發(fā)運(yùn)行
3.觸發(fā)器不能接收參數(shù)
--觸發(fā)器的應(yīng)用
舉個(gè)例子:校內(nèi)網(wǎng)、開心網(wǎng)、facebook,當(dāng)你發(fā)一個(gè)日志,自動(dòng)通知好友,其實(shí)就是在增加日志的時(shí)候做一個(gè)出發(fā),再向表中寫入條目。
--觸發(fā)器的效率很高
舉例:論壇的發(fā)帖,每插入一個(gè)帖子都希望將版面表中的最后發(fā)帖時(shí)間,帖子總數(shù)字段進(jìn)行同步更新,這時(shí)使用觸發(fā)器效率會(huì)很高。
二、Oracle 使用 PL/SQL 編寫觸發(fā)器
1.--PL/SQL創(chuàng)建觸發(fā)器的一般語法
create [or replace] trigger trigger_name{before | after}{insert | delete | update [of column[,column ... ]]} on table_name[for each row][where condition]--trigger_body;begin end;2.--練習(xí)
--問題3.使用:old 和 :new 操作符create or replace trigger tri_updateafterupdate on employeesfor each row begin dbms_output.put_line('更新前:'||:old.salary||' 更新后:'||:new.salary);end;--問題2.編寫一個(gè)觸發(fā)器,在向 emp 表中插入記錄時(shí) 打印'hello'create or replace trigger tri_updateafterinsert on empbegin dbms_output.put_line('ok');end;--問題1.一個(gè)helloworld級(jí)別的觸發(fā)器--創(chuàng)建一個(gè)觸發(fā)器,在更新employees表的時(shí)候觸發(fā)create or replace trigger tri_updateafterupdate on employeesfor each row --想在最后執(zhí)行完打印一個(gè)ok,把這句話去掉begin dbms_output.put_line('ok');end;--執(zhí)行update employeesset salary = salary+1where department_id = 80三、在MySql 使用觸發(fā)器
--假設(shè)有兩張表 board 和 articlecreate table board( id int primary key auto_increment, name varchar(50), articleCount int);create table article( id int primary key auto_increment, title varchar(50), bid int references board(id));--創(chuàng)建一個(gè)觸發(fā)器delimiter $$create trigger insertArticle_trigger after insert on article for each rowbegin update board set articleCount=articleCount+1where id = new.bid;end;$$delimiter ;--當(dāng)我們對(duì)article表執(zhí)行插入操作的是后就會(huì)觸發(fā)這個(gè)觸發(fā)器insert into board values(null,'test_boardname',0);insert into article values(null,'test_title',1);--執(zhí)行完這條插入語句后,board表中的articleCount字段值回+1;這個(gè)操作由觸發(fā)器完成。
以上所述是小編給大家介紹的Oracle使用觸發(fā)器和mysql中使用觸發(fā)器的案例比較,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選