瑙﹀彂鍣ㄥ涔犵瑪璁?:new 锛?old鐢ㄦ硶)
杩欐槸鎴戠殑瑙﹀彂鍣ㄥ涔犵瑪璁帮紝鍙兘鏈変簺涔憋紝鎶辨瓑
瑙﹀彂鍣ㄦ槸鏁版嵁搴撳彂鐢熸煇涓搷浣滄椂鑷姩杩愯鐨勪竴绫荤殑绋嬪簭
鐢ㄤ簬淇濇寔鏁版嵁鐨勫畬鏁存€ф垨璁板綍鏁版嵁搴撴搷浣滀俊鎭柟闈?
瑙﹀彂鍣ㄤ笉鑳藉琚洿鎺ヨ皟鐢紝鍙兘澶熷湪鏌愪簺浜嬩欢鍙戠敓鏃惰瑙﹀彂锛屼篃灏辨槸绯荤粺鑷姩杩涜璋?nbsp; 鐢?
瑙﹀彂鍣ㄧ殑鏋勫缓璇硶 create [or replace] trigger trigger_name
before|after
event1 [ or event2 or event3 鈥
on table_name [for each row]
begin
statement;
鈥?
end;
event閫氬父鏄痠nsert銆乨elete鎴杣pdate绛塂ML鎿嶄綔
璇存槑锛?/span>
For each row鐨勬剰涔夋槸锛氬湪涓€娆℃搷浣滆〃鐨勮鍙ヤ腑锛屾瘡鎿嶄綔鎴愬姛涓€琛屽氨浼氳Е鍙戜竴娆★紱涓嶅啓鐨?nbsp; 璇濓紝琛ㄧず鏄〃绾цЕ鍙戝櫒锛屽垯鏃犺鎿嶄綔澶氬皯琛岋紝閮藉彧瑙﹀彂涓€娆★紱
When鏉′欢鐨勫嚭鐜拌鏄庝簡锛屽湪DML鎿嶄綔鐨勬椂鍊欎篃璁镐竴瀹氫細瑙﹀彂瑙﹀彂鍣紝浣嗘槸瑙﹀彂鍣ㄤ笉涓€瀹氫細鍋氬疄闄呯殑宸ヤ綔锛屾瘮濡倃hen 鍚庣殑鏉′欢涓嶄负鐪熺殑鏃跺€欙紝瑙﹀彂鍣ㄥ彧鏄畝鍗曞湴璺宠繃浜哖L/SQL鍧?
瑙﹀彂鍣ㄥ垎涓鸿鍙ョ骇瑙﹀彂鍣ㄥ拰琛岀骇瑙﹀彂鍣?/span>
璇彞绾цЕ鍙戝櫒鏄寚姣忔墽琛屼竴鏉ML璇彞锛岃瑙﹀彂鍣ㄥ氨鎵ц涓€娆?
琛岀骇瑙﹀彂鍣ㄦ槸鎸囨瘡涓狣ML鎿嶄綔褰卞搷鍑犳潯璁板綍锛屽氨浼氳Е鍙戝嚑娆?for each row)
琛岀骇瑙﹀彂鍣ㄤ腑鐢变簬娑夊強鍒颁簡姣忔潯璁板綍鐨勬暟鎹彉鍔紝鎵€浠ュ浜庢瘡鏉¤褰曠殑鏁版嵁鏉ヨ灏辨湁鏂板€煎拰鏃у€间箣鍒?
鍏抽敭瀛楋細
:NEW 鍜?OLD浣跨敤鏂规硶鍜屾剰涔夛紝new 鍙嚭鐜板湪insert鍜寀pdate鏃讹紝old鍙嚭鐜板湪update鍜宒elete鏃躲€傚湪insert鏃秐ew琛ㄧず鏂版彃鍏ョ殑琛屾暟鎹紝update鏃秐ew琛ㄧず瑕佹浛鎹㈢殑鏂版暟鎹€乷ld琛ㄧず瑕佽鏇存敼鐨勫師鏉ョ殑鏁版嵁琛岋紝delete鏃秓ld琛ㄧず瑕佽鍒犻櫎鐨勬暟鎹€?
绀轰緥(1)
--璁板綍鎿嶄綔鏁版嵁搴撲俊鎭柟闈?
--鍒涘缓涓€涓猼_emp2_log琛ㄧ敤浜庡瓨鍌ㄧ敤鎴峰emp2琛ㄦ墍鍋氱殑鎿嶄綔淇℃伅
create table t_emp2_log(
t_id number(10) primary key, --缂栧彿
t_date date, --淇敼鏃ユ湡
t_user varchar2(20), --鎿嶄綔鐢ㄦ埛
action varchar(20) --璇ョ敤鎴锋墍鍋氭搷浣?
);
--鍒涘缓瑙﹀彂鍣╰_emp2锛屽綋鏈夌敤鎴峰琛╡mp2鎿嶄綔鐨勬椂鍊欙紝灏变細瑙﹀彂璇ヨЕ鍙戝櫒锛岃褰曟敼鐢ㄦ埛瀵硅〃emp2鎵€鍋氱殑鎿嶄綔
create or replace trigger t_emp2
after update or delete or insert on emp2
begin
if updating then --褰撴墽琛屾洿鏂版搷浣滄椂鎵ц
insert into t_emp2_log values(seq_t_emp2_log.nextval,sysdate, user, 'update');
elsif deleting then --褰撴墽琛屽垹闄ゆ搷浣滄椂鎵ц
insert into t_emp2_log values(seq_t_emp2_log.nextval,sysdate, user, 'delete');
elsif inserting then -- 褰撴墽琛屾彃鍏ユ搷浣滄椂
insert into t_emp2_log values(seq_t_emp2_log.nextval, sysdate, user, 'inserting');
end if;
end;
绀轰緥(2)
--淇濇寔鏁版嵁鐨勫畬鏁存€?..
create table dept2 as select * from dept;
--褰撳瓨鍦ㄤ緷璧栬〃鏃讹紝褰撲富琛ㄧ殑淇℃伅鍙戠敓鏀瑰彉鏃讹紝渚濊禆琛ㄧ殑鐩稿簲淇℃伅涔熶細鍙戠敓鏀瑰彉
create or replace trigger tr_emp2
--褰撲富琛╠ept2鍙戠敓鏀瑰彉鏃?
after update on dept2 for each row
begin
--渚濊禆琛ㄤ篃浼氬彂鐢熺浉搴旂殑鍙樺寲锛屼互淇濇寔鏁版嵁鐨