国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Database Mysql Tutorial oracle trigger代碼

oracle trigger代碼

Jun 07, 2016 pm 03:38 PM
oracle sql trigger code

sql 代碼 --[6]// Oracle Trigger -- 實例 1------------------------ -- 創(chuàng)建觸發(fā)器,當(dāng)用戶對 test 表執(zhí)行 DML 語句時,將相關(guān)信息記錄到日志表 -- 創(chuàng)建測試表 CREATE TABLE test ( t_id NUMBER(4), t_name VARCHAR2(20), t_age NUMBER(2), t_sex CHAR );

sql 代碼

  1. --[6]// Oracle Trigger

  2. --實例1------------------------

  3. --創(chuàng)建觸發(fā)器,當(dāng)用戶對test表執(zhí)行DML語句時,將相關(guān)信息記錄到日志表

  4. --創(chuàng)建測試表

  5. CREATE TABLE test

  6. (

  7. t_id NUMBER(4),

  8. t_name VARCHAR2(20),

  9. t_age NUMBER(2),

  10. t_sex CHAR

  11. );

  12. --創(chuàng)建記錄測試表

  13. CREATE TABLE test_log

  14. (

  15. l_user VARCHAR2(15),

  16. l_type VARCHAR2(15),

  17. l_date VARCHAR2(30)

  18. );

  19. --創(chuàng)建觸發(fā)器

  20. CREATE OR REPLACE TRIGGER test_trigger

  21. AFTER DELETE OR INSERT OR UPDATE ON test

  22. DECLARE

  23. v_type test_log.l_type%TYPE;

  24. BEGIN

  25. IF INSERTING THEN --INSERT觸發(fā)

  26. v_type := 'INSERT';

  27. DBMS_OUTPUT.PUT_LINE('記錄已經(jīng)成功插入,并已記錄到日志');

  28. ELSIF UPDATING THEN --UPDATE觸發(fā)

  29. v_type := 'UPDATE';

  30. DBMS_OUTPUT.PUT_LINE('記錄已經(jīng)成功更新,并已記錄到日志');

  31. ELSIF DELETING THEN

  32. v_type := 'DELETE';

  33. DBMS_OUTPUT.PUT_LINE('記錄已經(jīng)成功刪除,并已記錄到日志');

  34. END IF;

  35. INSERT INTO test_log VALUES(user,v_type,

  36. TO_CHAR(sysdate,'yyyy-mm-dd hh24:mi:ss'));

  37. END;

  38. /

  39. --下面我們來分別執(zhí)行DML語句

  40. INSERT INTO test VALUES(101,'zhao',22,'M');

  41. UPDATE test SET t_age = 30 WHERE t_id = 101;

  42. DELETE test WHERE t_id = 101;

  43. --然后查看效果

  44. SELECT * FROM test;

  45. SELECT * FROM test_log;

  46. ?

  47. --實例2------------------------

  48. --創(chuàng)建觸發(fā)器,它將映射emp表中每個部門的總?cè)藬?shù)和總工資

  49. --創(chuàng)建映射表

  50. CREATE TABLE dept_sal

  51. AS

  52. SELECT deptno,COUNT(empno) AS total_emp,SUM(sal) AS total_sal FROM emp GROUP BY deptno;

  53. DESC dept_sal;

  54. --創(chuàng)建觸發(fā)器

  55. CREATE OR REPLACE TRIGGER emp_info

  56. AFTER INSERT OR UPDATE OR DELETE ON emp

  57. DECLARE

  58. CURSOR cur_emp IS

  59. SELECT deptno,COUNT(empno) AS total_emp,SUM(sal) AS total_sal FROM emp GROUP BY deptno;

  60. BEGIN

  61. DELETE dept_sal; --觸發(fā)時首先刪除映射表信息

  62. FOR v_emp IN cur_emp LOOP

  63. --DBMS_OUTPUT.PUT_LINE(v_emp.deptno || v_emp.total_emp || v_emp.total_sal);

  64. --插入數(shù)據(jù)

  65. INSERT INTO dept_sal

  66. VALUES(v_emp.deptno,v_emp.total_emp,v_emp.total_sal);

  67. END LOOP;

  68. END;

  69. /

  70. --emp表進行DML操作

  71. INSERT INTO emp(empno,deptno,sal) VALUES('123','10',10000);

  72. SELECT * FROM dept_sal;

  73. DELETE EMP WHERE empno=123;

  74. SELECT * FROM dept_sal;

  75. ?

  76. --實例3------------------------

  77. --創(chuàng)建觸發(fā)器,它記錄表的刪除數(shù)據(jù)

  78. --創(chuàng)建表

  79. CREATE TABLE employee

  80. (

  81. id VARCHAR2(4) NOT NULL,

  82. name VARCHAR2(15) NOT NULL,

  83. age NUMBER(2) NOT NULL,

  84. sex CHAR NOT NULL

  85. );

  86. DESC employee;

  87. --插入數(shù)據(jù)

  88. INSERT INTO employee VALUES('e101','zhao',23,'M');

  89. INSERT INTO employee VALUES('e102','jian',21,'F');

  90. --創(chuàng)建記錄表

  91. CREATE TABLE old_employee AS

  92. SELECT * FROM employee;

  93. DESC old_employee;

  94. --創(chuàng)建觸發(fā)器

  95. CREATE OR REPLACE TRIGGER tig_old_emp

  96. AFTER DELETE ON employee --

  97. FOR EACH ROW --語句級觸發(fā),即每一行觸發(fā)一次

  98. BEGIN

  99. INSERT INTO old_employee

  100. VALUES(:old.id,:old.name,:old.age,:old.sex); --:old代表舊值

  101. END;

  102. /

  103. --下面進行測試

  104. DELETE employee;

  105. SELECT * FROM old_employee;

  106. ?

  107. --實例4------------------------

  108. --創(chuàng)建觸發(fā)器,利用視圖插入數(shù)據(jù)

  109. --創(chuàng)建表

  110. CREATE TABLE tab1 (tid NUMBER(4) PRIMARY KEY,tname VARCHAR2(20),tage NUMBER(2));

  111. CREATE TABLE tab2 (tid NUMBER(4),ttel VARCHAR2(15),tadr VARCHAR2(30));

  112. --插入數(shù)據(jù)

  113. INSERT INTO tab1 VALUES(101,'zhao',22);

  114. INSERT INTO tab1 VALUES(102,'yang',20);

  115. INSERT INTO tab2 VALUES(101,'13761512841','AnHuiSuZhou');

  116. INSERT INTO tab2 VALUES(102,'13563258514','AnHuiSuZhou');

  117. --創(chuàng)建視圖連接兩張表

  118. CREATE VIEW tab_view AS

  119. SELECT tab1.tid,tname,ttel,tadr FROM tab1,tab2

  120. WHERE tab1.tid = tab2.tid;

  121. --創(chuàng)建觸發(fā)器

  122. CREATE OR REPLACE TRIGGER tab_trigger

  123. INSTEAD OF INSERT ON tab_view

  124. BEGIN

  125. INSERT INTO tab1(tid,tname) VALUES(:new.tid,:new.tname);

  126. INSERT INTO tab2(ttel,tadr) VALUES(:new.ttel,:new.tadr);

  127. END;

  128. /

  129. --現(xiàn)在就可以利用視圖插入數(shù)據(jù)

  130. INSERT INTO tab_view VALUES(105,'zhaoyang','13886681288','beijing');

  131. --查看效果

  132. SELECT * FROM tab_view;

  133. ?

  134. --實例5------------------------

  135. --創(chuàng)建觸發(fā)器,比較emp表中更新的工資

  136. CREATE OR REPLACE TRIGGER sal_emp

  137. BEFORE UPDATE ON emp

  138. FOR EACH ROW

  139. BEGIN

  140. ?

  141. IF :OLD.sal > :NEW.sal THEN

  142. DBMS_OUTPUT.PUT_LINE('工資減少');

  143. ELSIF :OLD.sal
  144. DBMS_OUTPUT.PUT_LINE('工資增加');

  145. ELSE

  146. DBMS_OUTPUT.PUT_LINE('工資未作任何變動');

  147. END IF;

  148. DBMS_OUTPUT.PUT_LINE('更新前工資 :' || :OLD.sal);

  149. DBMS_OUTPUT.PUT_LINE('更新后工資 :' || :NEW.sal);

  150. END;

  151. /

  152. --執(zhí)行UPDATE查看效果

  153. UPDATE emp SET sal = 3000 WHERE empno = '7788';

  154. ?

  155. --實例6------------------------

  156. --創(chuàng)建觸發(fā)器,將操作CREATEDROP存儲在log_info

  157. --創(chuàng)建表

  158. CREATE TABLE log_info

  159. (

  160. manager_user VARCHAR2(15),

  161. manager_date VARCHAR2(15),

  162. manager_type VARCHAR2(15),

  163. obj_name VARCHAR2(15),

  164. obj_type VARCHAR2(15)

  165. );

  166. --創(chuàng)建觸發(fā)器

  167. CREATE OR REPLACE TRIGGER trig_log_info

  168. AFTER CREATE OR DROP ON SCHEMA

  169. BEGIN

  170. INSERT INTO log_info

  171. VALUES(USER,SYSDATE,SYS.DICTIONARY_OBJ_NAME,SYS.DICTIONARY_OBJ_OWNER,

  172. SYS.DICTIONARY_OBJ_TYPE);

  173. END;

  174. /

  175. --測試語句

  176. CREATE TABLE a(id NUMBER);

  177. CREATE TYPE aa AS OBJECT(id NUMBER);

  178. /

  179. DROP TABLE a;

  180. DROP TYPE aa;

  181. --查看效果

  182. SELECT * FROM log_info;

  183. ?

  184. --相關(guān)數(shù)據(jù)字典-----------------------------------------------------//

  185. SELECT * FROM USER_TRIGGERS;

  186. SELECT * FROM ALL_TRIGGERS;

  187. SELECT * FROM DBA_TRIGGERS; --必須以DBA身份登陸才能使用此數(shù)據(jù)字典

  188. ?

  189. --啟用和禁用

  190. ALTER TRIGGER trigger_name DISABLE;

  191. ALTER TRIGGER trigger_name ENABLE;

  192. ------------------------------------------------------------------------------------------End//


?關(guān)于在 oracle trigger(insert)事件中調(diào)用存儲過程的問題?? ??

?最近用ORACLE觸發(fā)器寫了點東西,不過,確發(fā)現(xiàn) ORA-04092、ora-06512 等錯誤。怎么辦?

最初,我也被弄得手忙腳亂。

于是,我查看 ORA-04092 提示的錯誤信息,是ROLLBACK和COMMIT 命令不能出現(xiàn)在觸發(fā)器中。

于是,開始解決問題。發(fā)現(xiàn),當(dāng)在觸發(fā)器里加入事務(wù)性操作時,會有如下錯誤:ORA-04092 cannot string in a trigger. 解決辦法,在觸發(fā)器里加入聲明PRAGMA AUTONOMOUS_TRANSACTION,好了,不在彈出錯誤信息。

可是,我通過觸發(fā)器調(diào)用存儲過程,卻發(fā)現(xiàn)存儲過程執(zhí)行后,數(shù)據(jù)沒任何變化(我的存儲過程是執(zhí)行一次,刪除一次中間表數(shù)據(jù)。)。難道是我的存儲過程出了問題?單步調(diào)試,沒任何問題。執(zhí)行結(jié)果正確。

通過多次測試,發(fā)現(xiàn)原來是觸發(fā)觸發(fā)器的時候(觸發(fā)事件為 insert after)由于沒有提交(COMMIT)數(shù)據(jù)。在中間表中就查不到任何數(shù)據(jù)。處理方法,

第一步、新建一個中間表,做為觸發(fā)器觸發(fā)事件發(fā)生的中間表。

第二步、在中間表插入數(shù)據(jù),提交。

第三步、在第一步新建的中間表中插入數(shù)據(jù),提交執(zhí)行成功。

第四步、檢查結(jié)果,正常。


?能否在表的觸發(fā)器中當(dāng)一記錄變化修改另一記錄??

SQL> create or replace trigger addnum
? 2? before update on test1.test1
? 3? for each row
? 4? begin
? 5? update test1 set group1=(:new.group1+1) where group1=2;
? 6? end;
? 7? /

觸發(fā)器已創(chuàng)建

已用時間:? 00: 00: 00.00
SQL> update test1 set group1=7 where group1=1;
update test1 set group1=7 where group1=1
?????? *
ERROR
位于第 1
:
ORA-04091:
TEST1.TEST1 發(fā)生了變化,觸發(fā)器/函數(shù)不能讀

ORA-06512:
"TEST1.ADDNUM", line 2
ORA-04088:
觸發(fā)器 'TEST1.ADDNUM' 執(zhí)行過程中出錯

?
這樣是肯定不行的,因為觸發(fā)器會引起死循環(huán)。用表層觸發(fā)器,但表層是不能用:new:old?


SQL> create or replace procedure test3(i number,m number)
? 2? as
? 3? a number;
? 4? str char(1000);
? 5? begin
? 6? update test1 set group1=i where group1=m;
? 7? execute immediate 'COMMIT';
? 8? a:=i;
? 9? for a in i..m loop
?10? update test1 set group1=i+1 where group1=i;
?11? execute immediate 'COMMIT';
?12? end loop;
?13? end test3;
?14? /

過程已創(chuàng)建。

已用時間:? 00: 00: 00.00
SQL> exec test3(2,4);
BEGIN test3(2,4); END;

*
ERROR
位于第 1
:
ORA-04091:
TEST1.TEST1 發(fā)生了變化,觸發(fā)器/函數(shù)不能讀

ORA-06512:
"TEST1.ADDNUM", line 2
ORA-04088:
觸發(fā)器 'TEST1.ADDNUM' 執(zhí)行過程中出錯

ORA-06512:
"TEST1.TEST3", line 6
ORA-06512:
line 1

?
?


1
、使用視圖或者臨時表

You can do:
? 1. Rename your table with another name;
? 2. Create a view for select * from the table with the original name of the table;
? 3. Create a instead of trigger on the view, in the view you can access and update yur base

table.


Example of instead of trigger for this case:

[ Code Start ]
create table t1(
? id??????? number(6)??? primary key
?,pid?????? number(6)
?,value???? number(15,2)
?,f1??????? varchar2(10)
?,f2??????? varchar2(20)
);


create or replace view t1_v as select * from t1;

create or replace trigger bug_t1_v
instead of update on t1_v
for each row
declare
? procedure update_parents(i_id in number, i_value in number);
? procedure update_parents(i_id in number, i_value in number) is
? begin
??? declare
????? l_pid t1.pid%type;
??? begin
????? select pid into l_pid
??????? from t1
?????? where id = i_id;
????? if l_pid 0 then?
??????? update t1 set value = nvl(value,0) + nvl(i_value,0)
???????? where id = l_pid;
??????? update_parents(l_pid, i_value);
????? end if;
??? exception
????? when no_data_found then
??????? null;?
??? end;???????
? end update_parents;
begin
? --
? -- Update Value Field for current record and Parent records?
? --
? if nvl(:new.value,0) - nvl(:old.value,0) 0 then
???? update t1 set value = value + nvl(:new.value,0) - nvl(:old.value,0)
????? where id = :new.id;
???? update_parents(:new.id, nvl(:new.value,0) - nvl(:old.value,0));?
? end if;
? --
? -- Update Others Fields
? --
? update t1 set f1 = :new.f1
?????????????? ,f2 = :new.f2
?? where id = :new.id;
end;
?
--
-- Testing
--
-- With this view: t1_v
--
begin
? for i in 1..50 loop
??? Insert into t1_v values(i, i-1, 0, '', '');
? end loop;
end;
/

delete from t1_v;

begin
? for i in 1..50 loop
??? Insert into t1_v values(i, i-1, 0, '', '');
? end loop;
end;
/

update t1_v set f1 = 'TEST' where id = 49;


update t1_v set value = value + 5 , f1 = 'AA', F2 = 'BB'
where id = 50;


[ Code End ]

使用臨時表

11:04:36 SQL> CREATE GLOBAL TEMPORARY TABLE Tmp(
11:04:39?? 2? rid varchar2(20))ON COMMIT DELETE ROWS;

表已創(chuàng)建。

已用時間:? 00: 00: 00.16
11:07:30 SQL> create or replace trigger trg_tb
11:07:36?? 2? after insert on tb
11:07:36?? 3? for each row
11:07:36?? 4? begin
11:07:36?? 5? if :new.col2='laji' then
11:07:36?? 6?? insert into tmp values(:new.rowid);
11:07:36?? 7? end if;
11:07:36?? 8? end trg_tb;
11:07:36?? 9? /

觸發(fā)器已創(chuàng)建

已用時間:? 00: 00: 00.47
11:07:36 SQL> create or replace trigger trg_tb_del
11:07:44?? 2? after insert on tb
11:07:46?? 3? begin
11:07:46?? 4? delete from tb where exists(select 1 from tmp where rid=tb.rowid);
11:07:46?? 5? end trg_tb_del;
11:07:46?? 6? /

觸發(fā)器已創(chuàng)建

已用時間:? 00: 00: 00.31
11:08:59 SQL> select count(1) from tb;

? COUNT(1)
----------
??????? 22

已用時間:? 00: 00: 00.00
11:09:08 SQL> insert into tb(col1,col2) values('aaa','laji');

已創(chuàng)建 1 行。

已用時間:? 00: 00: 00.00
11:09:12 SQL> select count(1) from tb;

? COUNT(1)
----------
??????? 22

已用時間:? 00: 00: 00.16
11:09:14 SQL>

From Metalink

Problem Description -------------------
Oracle does not allow you to read a mutating table in a row trigger because if you can read it,

the information may be incorrect (not read consistent). If you attempt this, the following error

is returned:
ORA-04091
Table %s.%s is mutating, trigger/function may not see it
However, you can perform this operation in a statement trigger.
One way to handle this situation is to use a package PL/SQL table to store ROWIDs of updated

records in a row trigger, and reprocess the updated
?records in a statement trigger.
Below is an example.

?Important Note --------------
Note that there are concurrency issues with this if more than one session tries to perform

operations simultaneously.
This is not intended as a total solution, but as the framework to help show one option.
?Example Workaround ------------------
[code:1:50147eb56b]
create or replace package emp_pkg as
type
emp_tab_type is table of rowid index by binary_integer;
emp_tab
?emp_tab_type;
emp_index binary_integer;
?end emp_pkg;
?/
create or replace trigger emp_bef_stm_all
?before insert or update or delete on emp
?begin
/*
Remember to reset the pl/sql table before each statement
*/
emp_pkg.emp_index := 0;
?end;
?/
create or replace trigger emp_aft_row_all
?after insert or update or delete on emp
?for each row
?begin
/*
Store the rowid of updated record into global pl/sql table
*/
emp_pkg.emp_index := emp_pkg.emp_index + 1;
emp_pkg.emp_tab(emp_pkg.emp_index) := :new.rowid;
?end;
?/


create or replace trigger emp_aft_stm_all
?after
insert or update or delete on emp
?begin
for i in 1 .. emp_pkg.emp_index loop
?/*

?Re-process the updated records.
?There is no restriction here.
?*/
?dbms_output.put_line(emp_pkg.emp_tab(i));
end loop;
emp_pkg.emp_index := 0;
?end;
?/
怎么樣把提交的數(shù)據(jù)通過觸發(fā)器進行驗證,如果不合條件,直接刪除?

樓主andywzw()2004-09-30 11:04:40 Oracle / 開發(fā)提問

在應(yīng)用程序中,有些數(shù)據(jù)是垃圾數(shù)據(jù),現(xiàn)在程序在前段提交的時候沒有驗證機制,補做代價比較大,我現(xiàn)在想能不能在后臺oracle通過觸發(fā)器,對插入動作進行驗證,如果發(fā)現(xiàn)垃圾數(shù)據(jù),直接刪除,這樣比做存儲過程進行定時監(jiān)控要減少系統(tǒng)資源的占用,但是我不知道如何實現(xiàn),請高手指教!問題點數(shù):100、回復(fù)次數(shù):32Top

1 drabitsquare回復(fù)于 2004-09-30 11:14:23 得分 0

這樣做違反事務(wù)的一致性要求,oracle不會提供這種手段的。一次插入的數(shù)據(jù)要么都插入,要么都不插入。Top

2 bluelambbluelamb回復(fù)于 2004-09-30 11:18:48 得分 0

只能夠在提交前驗證Top

3 bzszpSongZip回復(fù)于 2004-09-30 11:19:44 得分100

使用臨時表 ?
? ?
? 11:04:36 ? SQL> ? CREATE ? GLOBAL ? TEMPORARY ? TABLE ? Tmp( ?
? 11:04:39 ? ? ? 2 ? ? rid ? varchar2(20))ON ? COMMIT ? DELETE ? ROWS; ?
? ?
?
表已創(chuàng)建。 ?
? ?
?
已用時間: ? ? 00: ? 00: ? 00.16 ?
? 11:07:30 ? SQL> ? create ? or ? replace ? trigger ? trg_tb ? ?
? 11:07:36 ? ? ? 2 ? ? after ? insert ? on ? tb ?
? 11:07:36 ? ? ? 3 ? ? for ? each ? row ?
? 11:07:36 ? ? ? 4 ? ? begin ?
? 11:07:36 ? ? ? 5 ? ? if ? :new.col2='laji' ? then ?
? 11:07:36 ? ? ? 6 ? ? ? insert ? into ? tmp ? values(:new.rowid); ?
? 11:07:36 ? ? ? 7 ? ? end ? if; ?
? 11:07:36 ? ? ? 8 ? ? end ? trg_tb; ?
? 11:07:36 ? ? ? 9 ? ? / ?
? ?
?
觸發(fā)器已創(chuàng)建 ?
? ?
?
已用時間: ? ? 00: ? 00: ? 00.47 ?
? 11:07:36 ? SQL> ? create ? or ? replace ? trigger ? trg_tb_del ?
? 11:07:44 ? ? ? 2 ? ? after ? insert ? on ? tb ?
? 11:07:46 ? ? ? 3 ? ? begin ?
? 11:07:46 ? ? ? 4 ? ? delete ? from ? tb ? where ? exists(select ? 1 ? from ? tmp ? where ? rid=tb.rowid); ?
? 11:07:46 ? ? ? 5 ? ? end ? trg_tb_del; ?
? 11:07:46 ? ? ? 6 ? ? / ?
? ?
?
觸發(fā)器已創(chuàng)建 ?
? ?
?
已用時間: ? ? 00: ? 00: ? 00.31 ?
? 11:08:59 ? SQL> ? select ? count(1) ? from ? tb; ?
? ?
? ? ? COUNT(1) ?
? ---------- ?
? ? ? ? ? ? ? ? ? 22 ?
? ?
?
已用時間: ? ? 00: ? 00: ? 00.00 ?
? 11:09:08 ? SQL> ? insert ? into ? tb(col1,col2) ? values('aaa','laji'); ?
? ?
?
已創(chuàng)建 ? 1 ? 行。 ?
? ?
?
已用時間: ? ? 00: ? 00: ? 00.00 ?
? 11:09:12 ? SQL> ? select ? count(1) ? from ? tb; ?
? ?
? ? ? COUNT(1) ?
? ---------- ?
? ? ? ? ? ? ? ? ? 22 ?
? ?
?
已用時間: ? ? 00: ? 00: ? 00.16 ?
? 11:09:14 ? SQL> ? ?
? ?
?
說明剛才插入的數(shù)據(jù)已經(jīng)被刪除掉了。Top

4 andywzw()回復(fù)于 2004-09-30 11:20:15 得分 0

實際上我的想法是這樣的,先讓它插入目標(biāo)表,同時觸發(fā)檢查這個剛才插入的語句是否符合條件,如果不符合就從目標(biāo)表中直接刪除,如果符合,就不做任何操作。Top

5 bzszpSongZip回復(fù)于 2004-09-30 11:21:37 得分 0

通過一個行級觸發(fā)器+一個表級觸發(fā)器+臨時表解決。Top

6 andywzw()回復(fù)于 2004-09-30 11:25:09 得分 0

那我不用再改寫前臺的應(yīng)用程序了嗎?前臺的插入是通過java實現(xiàn)的,臨時表是否影響正是數(shù)據(jù)的插入?Top

7 andywzw()回復(fù)于 2004-09-30 11:28:35 得分 0

bzszp(SongZip) ? 的方案都是很專業(yè)的,我太佩服了!我做個實驗看結(jié)果!Top

8 bzszpSongZip回復(fù)于 2004-09-30 11:31:19 得分 0

不會的,用ON ? ? COMMIT ? ? DELETE ? ? ROWS; ? 方式創(chuàng)建臨時表,在會話 ?
? commit
以后oracle自動清空數(shù)據(jù)。 ?
? ? ?
? Top

9 andywzw()回復(fù)于 2004-09-30 11:38:26 得分 0

對了,你的臨時表只有一個字段啊,能滿足我對多個字段的驗證嗎?Top

10 bzszpSongZip回復(fù)于 2004-09-30 11:41:33 得分 0

沒有問題,這個是存放的rowid,對于每一行數(shù)據(jù)都是唯一的。 ?
?
判斷是在行級觸發(fā)器內(nèi)部判斷,如果是垃圾數(shù)據(jù) ?
?
紀(jì)錄:new.rowid到臨時表 ?
?
在表級觸發(fā)器里面集中進行刪除。Top

11 andywzw()回復(fù)于 2004-09-30 11:44:18 得分 0

我覺得這樣的方式比我們昨天討論的解決問題的方案更好吧。這樣可以避免不斷的執(zhí)行存儲過程導(dǎo)致系統(tǒng)性能有影響。Top

12 drabitsquare回復(fù)于 2004-09-30 12:10:08 得分 0

studyTop

13 fightintokyo()回復(fù)于 2004-09-30 13:11:24 得分 0

bzszp(SongZip),good! ?
? ?
?
請教一個初級的問題。上面的解決方案中,行級觸發(fā)器會確保在表級觸發(fā)器前被觸發(fā)么?(對觸發(fā)器不太了解)Top

14 zhaokeke2004(男人·海洋)回復(fù)于 2004-09-30 13:27:06 得分 0

學(xué)習(xí)Top

15 bzszpSongZip回復(fù)于 2004-09-30 13:34:32 得分 0

tofightintokyo(黑龍) ? ?
?
是的 ?
? ?
?
性能方面應(yīng)該會好一些吧。 ?
?
畢竟直接通過rowid進行刪除,而且是在commit之前就刪除了 ?
?
這樣那些不需要的數(shù)據(jù)就沒有寫回數(shù)據(jù)文件。Top

16 andywzw()回復(fù)于 2004-09-30 13:34:53 得分 0

bzszp(SongZip),請把你的tb表,desc ? tb ? 給我看,我怎么做不下去了!Top

17 andywzw()回復(fù)于 2004-09-30 13:39:25 得分 0

bzszp(SongZip),是個專家級的人物,解決問題準(zhǔn)確,耐心,細(xì)致,大家向他致敬!??!Top

18 fightintokyo()回復(fù)于 2004-09-30 13:56:51 得分 0

謝謝bzszp(SongZip) ?
? ?
?
查了一下資料,果然觸發(fā)器是按照以下順序執(zhí)行的。 ?
? 1.
執(zhí)行before表級觸發(fā)器。 ?
? 2.
以受影響的行進行循環(huán)。 ?
? ? ? a.
執(zhí)行before行級觸發(fā)器。(commit為止該行被lock ?
? ? ? b.
執(zhí)行after行級觸發(fā)器。 ?
? 3.
目標(biāo)表定義的整合性check ?
? 4.
執(zhí)行after表級觸發(fā)器。 ?
? ?
?
再請教一個問題。 ?
?
為什么不在行級觸發(fā)器中直接把不符合條件的數(shù)據(jù)delete了呢? ?
?
這樣就不需要臨時表和行級觸發(fā)器了亞。Top

19 andywzw()回復(fù)于 2004-09-30 14:00:30 得分 0

bzszp(SongZip),請把你的tb表,desc ? tb ? 給我看,我怎么做不下去了!Top

20 bzszpSongZip回復(fù)于 2004-09-30 14:01:30 得分 0

這樣會有問題的,行級觸發(fā)器對于正在處理的表數(shù)據(jù)有操作限制。Top

21 bzszpSongZip回復(fù)于 2004-09-30 14:01:52 得分 0

13:50:23 ? SQL> ? desc ? tb; ?
? ?
名稱 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 類型 ?
? ? ----------------------------------------- ? -------- ? -------------- ?
? ? COL1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? VARCHAR2(10) ?
? ? COL2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? VARCHAR2(30) ?
? ? COLNEW ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? VARCHAR2(20) ?
? ?
? 13:52:32 ? SQL>Top

22 andywzw()回復(fù)于 2004-09-30 14:17:20 得分 0

bzszp(SongZip) ? ? 這辦法絕對很棒,但是問題是 ?
? ? create ? or ? replace ? trigger ? trg_tb ? ?
? after ? insert ? on ? tb ? for ? each ? row ?
? begin ?
? if ? :new.col2='laji' ? then ?
? ? insert ? into ? tmp ? values(:new.rowid); ?
? end ? if; ?
? end ? trg_tb; ?
? / ?
?
這個里面的你現(xiàn)在指定了col2,我要指定多列是不是用 ? ?
? if ? :new.col2='laji' ? or ? :new.col1='xxx' ? or ? ? :new.coln='nnn' ? ? then ?
?
另外,這里的=后面的值是唯一的有局限性,我怎么從一個表,例如bad_word里面獲得這些值?Top

23 andywzw()回復(fù)于 2004-09-30 14:23:43 得分 0

SQL> ? desc ? bad_word ?
? ?
名稱 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 類型 ?
? ? ----------------------------------------- ? -------- ? ------------- ?
? ? ID ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NUMBER(10) ?
? ? KEY_WORD ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? VARCHAR2(60)Top

24 bzszpSongZip回復(fù)于 2004-09-30 14:27:22 得分 0

select ? 判斷,另外聲明一個v_num ? number的變量。 ?
? select ? count(*) ? into ? v_num ? from ? bad_word ? ?
? where ? bad_word.colname=:new.col1; ?
? if ? v_num>0 ? then ? --
屬于包含垃圾信息的行 ?
? ? ? ... ?
? end ? if;Top

25 bzszpSongZip回復(fù)于 2004-09-30 14:28:25 得分 0

select ? count(*) ? into ? v_num ? from ? bad_word ? ?
? where ? key_word=:new.col1;Top

26 andywzw()回復(fù)于 2004-09-30 14:58:01 得分 0

不好意思,我不知道怎么申明v_num ? 這個變量 ? :)Top

27 bzszpSongZip回復(fù)于 2004-09-30 15:02:09 得分 0

如: ?
? create ? ? or ? ? replace ? ? trigger ? ? trg_tb ? ? ? ? ?
? after ? ? insert ? ? on ? ? tb ? ? for ? ? each ? ? row ?
? declare ? ? ? ? ? ? ? ? ? ? --
聲明變量開始 ? ?
? v_num ? number; ? ? --
聲明number類型的變量v_num ?
? begin ? ? ? ?
? ... ?
? Top

28 andywzw()回復(fù)于 2004-09-30 15:23:44 得分 0

bzszp(SongZip) ? 我試驗成功了!再問一下您,如果這種辦法用于留言版后臺過濾程序是不是很有價值的?比在前臺更加主動,減少維護量。Top

29 bzszpSongZip回復(fù)于 2004-09-30 15:28:50 得分 0

減少了前臺的工作量 ?
?
但是增加了后臺的工作量 ?
?
這個根據(jù)實際情況來定吧。Top

30 andywzw()回復(fù)于 2004-09-30 16:32:34 得分 0

謝謝,結(jié)分,祝bzszp(SongZip) ? 及其各位國慶節(jié)愉快!Top

31 andywzw()回復(fù)于 2004-09-30 18:13:13 得分 0

create ? or ? replace ? trigger ? TRG_LEAVE_WORD ?
? after ? insert ? on ? LEAVE_WORD ? for ? each ? row ?
? declare ? ?
? v_num ? number; ?
? begin ?
? select ? count(*) ? into ? v_num ? from ? bad_word ? ?
? where ? key_word ? like ? '%'||:new.TITLE||'%' ? or ? key_word ? like ? '%'||:new.CONTENT||'%'; ?
? if ? v_num>0 ? then ? ? ?
? ? insert ? into ? tmp ? values(:new.rowid); ?
? end ? if; ?
? end ? TRG_LEAVE_WORD; ?
? / ?
? ?
?
這個觸發(fā)器還是有問題的,過濾了CONTENT但不能過濾TITLE ? ,如何解決,難道我的 ? or條件有問題?Top

32 andywzw()回復(fù)于 2004-10-01 02:02:28 得分 0

我找到解決的辦法了,改寫成如下樣子完全可以使用: ?
? create ? or ? replace ? trigger ? TRG_LEAVE_WORD ?
? after ? insert ? on ? LEAVE_WORD ? for ? each ? row ?
? declare ? ?
? v_num ? number; ?
? begin ?
? ? select ? count(*) ? into ? v_num ? from ? BAD_WORD ? ?
? ? ? where ? instr(:new.TITLE,KEY_WORD)>0 ? or ? instr(:new.CONTENT,KEY_WORD)>0; ?
? ? if ? v_num>0 ? then ? ? ?
? ? ? insert ? into ? tmp ? values(:new.rowid); ?
? ? end ? if; ?
? end ? TRG_LEAVE_WORD; ?

?

?

?

?

?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL vs. Oracle: Licensing, Features, and Benefits MySQL vs. Oracle: Licensing, Features, and Benefits May 08, 2025 am 12:05 AM

The main difference between MySQL and Oracle is licenses, features, and advantages. 1. License: MySQL provides a GPL license for free use, and Oracle adopts a proprietary license, which is expensive. 2. Function: MySQL has simple functions and is suitable for web applications and small and medium-sized enterprises. Oracle has powerful functions and is suitable for large-scale data and complex businesses. 3. Advantages: MySQL is open source free, suitable for startups, and Oracle is reliable in performance, suitable for large enterprises.

MySQL: A Practical Application of SQL MySQL: A Practical Application of SQL May 08, 2025 am 12:12 AM

MySQL is popular because of its excellent performance and ease of use and maintenance. 1. Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2. Insert and query data: operate data through INSERTINTO and SELECT statements. 3. Optimize query: Use indexes and EXPLAIN statements to improve performance.

MySQL vs. Oracle: Understanding Licensing and Cost MySQL vs. Oracle: Understanding Licensing and Cost May 03, 2025 am 12:19 AM

MySQL uses GPL and commercial licenses for small and open source projects; Oracle uses commercial licenses for enterprises that require high performance. MySQL's GPL license is free, and commercial licenses require payment; Oracle license fees are calculated based on processors or users, and the cost is relatively high.

Comparing SQL and MySQL: Syntax and Features Comparing SQL and MySQL: Syntax and Features May 07, 2025 am 12:11 AM

The difference and connection between SQL and MySQL are as follows: 1.SQL is a standard language used to manage relational databases, and MySQL is a database management system based on SQL. 2.SQL provides basic CRUD operations, and MySQL adds stored procedures, triggers and other functions on this basis. 3. SQL syntax standardization, MySQL has been improved in some places, such as LIMIT used to limit the number of returned rows. 4. In the usage example, the query syntax of SQL and MySQL is slightly different, and the JOIN and GROUPBY of MySQL are more intuitive. 5. Common errors include syntax errors and performance issues. MySQL's EXPLAIN command can be used for debugging and optimizing queries.

How to learn Java without taking detours. Share methods and techniques for efficiently learning Java How to learn Java without taking detours. Share methods and techniques for efficiently learning Java May 20, 2025 pm 08:24 PM

The key to learning Java without taking detours is: 1. Understand core concepts and grammar; 2. Practice more; 3. Understand memory management and garbage collection; 4. Join online communities; 5. Read other people’s code; 6. Understand common libraries and frameworks; 7. Learn to deal with common mistakes; 8. Make a learning plan and proceed step by step. These methods can help you master Java programming efficiently.

MongoDB vs. Oracle: Exploring NoSQL and Relational Approaches MongoDB vs. Oracle: Exploring NoSQL and Relational Approaches May 07, 2025 am 12:02 AM

In different application scenarios, choosing MongoDB or Oracle depends on specific needs: 1) If you need to process a large amount of unstructured data and do not have high requirements for data consistency, choose MongoDB; 2) If you need strict data consistency and complex queries, choose Oracle.

What to learn Java? A summary of Java learning routes and essential knowledge points What to learn Java? A summary of Java learning routes and essential knowledge points May 20, 2025 pm 08:15 PM

Learning Java requires learning basic syntax, object-oriented programming, collection frameworks, exception handling, multithreading, I/O streaming, JDBC, network programming, and advanced features such as reflection and annotation. 1. The basic syntax includes variables, data types, operators and control flow statements. 2. Object-oriented programming covers classes, objects, inheritance, polymorphism, encapsulation and abstraction. 3. The collection framework involves ArrayList, LinkedList, HashSet, and HashMap. 4. Exception handling ensures program robustness through try-catch block. 5. Multithreaded programming requires understanding of thread life cycle and synchronization. 6. I/O streams are used for data reading, writing and file operations. 7. JDBC is used to interact with databases. 8. Network programming passes S

Oracle Software: Maximizing Efficiency and Performance Oracle Software: Maximizing Efficiency and Performance May 06, 2025 am 12:07 AM

Oracle software can improve performance in a variety of ways. 1) Optimize SQL queries and reduce data transmission; 2) Appropriately manage indexes to balance query speed and maintenance costs; 3) Reasonably configure memory, optimize SGA and PGA; 4) Reduce I/O operations and use appropriate storage devices.

See all articles