如何在 oracle 中对特定列执行删除或更新触发器?
How to execute trigger on delete or update on specific column happen in oracle?
我想编写触发器,在删除或更新发生时将旧数据复制到新数据中table。
所以我有两个 tables tableA 和 tableB
tableA 具有以下属性:rollno、name 和 status
tableB 具有以下属性:rollno、name
所以,首先每当对 tableA 进行删除操作时,我想将旧值复制到 tableB 或第二个每当 table 上的状态属性的值时对特定值的更改说 'C' 然后我也必须复制旧值。
我编写了触发器,每当执行删除或更新时,它会将旧值从 tableA 复制到 tableB,但触发器也会针对 [=39= 上的任何更新执行]A.
这是我的触发代码
create or replace trigger my_trigger
before delete or update
on tableA
for each row
begin
insert into tableB values(:OLD.rollno,:OLD.name);
end;
那么如果状态属性更新了,如何执行触发器呢?
我可以通过在触发器中使用 if 语句来检查,如果 :NEW 值为 'c' 然后执行触发器,但我也想执行 delete 语句的触发器,我该怎么做?
我想我可以使用两个触发器,一个用于删除,一个用于更新,内部更新触发器我可以检查我的情况,但我可以只在 oracle 中使用一个触发器吗?
您可以按以下方式进行操作:
create or replace trigger my_trigger
before delete or update of status
on tableA
for each row
WHEN (NEW.status= 'C' OR NEW.status IS NULL)
begin
insert into tableB values(:OLD.rollno,:OLD.name);
end;
仅查找具有 update of status
的列状态的更新。然后仅在新列值匹配某些条件时执行触发器 WHEN (NEW.status= 'C' OR NEW.status IS NULL)
检查您的值或 null(在删除情况下)。
是的,IF 可以提供帮助。
这是一个基于 Scott 架构的示例;看看。
这是一个 log table:
SQL> create table deptb as select * From dept where 1 = 2;
Table created.
触发器:
SQL> create or replace trigger trg_bdu_dept
2 before delete or update on dept
3 for each row
4 begin
5 if deleting then
6 insert into deptb values (:old.deptno, :old.dname, :old.loc);
7 elsif updating and :old.loc = 'NEW YORK' then
8 insert into deptb values (:old.deptno, :old.dname, :old.loc);
9 end if;
10 end;
11 /
Trigger created.
SQL>
测试:
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------------- --------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> delete from dept where deptno = 40;
1 row deleted.
SQL> update dept set loc = 'NY' where loc = 'NEW YORK';
1 row updated.
SQL> update dept set loc = 'dallas' where loc = 'DALLAS';
1 row updated.
SQL> select * from deptb;
DEPTNO DNAME LOC
---------- -------------------- --------------------
40 OPERATIONS BOSTON
10 ACCOUNTING NEW YORK
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------------- --------------------
10 ACCOUNTING NY
20 RESEARCH dallas
30 SALES CHICAGO
SQL>
我想编写触发器,在删除或更新发生时将旧数据复制到新数据中table。
所以我有两个 tables tableA 和 tableB
tableA 具有以下属性:rollno、name 和 status
tableB 具有以下属性:rollno、name
所以,首先每当对 tableA 进行删除操作时,我想将旧值复制到 tableB 或第二个每当 table 上的状态属性的值时对特定值的更改说 'C' 然后我也必须复制旧值。
我编写了触发器,每当执行删除或更新时,它会将旧值从 tableA 复制到 tableB,但触发器也会针对 [=39= 上的任何更新执行]A.
这是我的触发代码
create or replace trigger my_trigger
before delete or update
on tableA
for each row
begin
insert into tableB values(:OLD.rollno,:OLD.name);
end;
那么如果状态属性更新了,如何执行触发器呢? 我可以通过在触发器中使用 if 语句来检查,如果 :NEW 值为 'c' 然后执行触发器,但我也想执行 delete 语句的触发器,我该怎么做?
我想我可以使用两个触发器,一个用于删除,一个用于更新,内部更新触发器我可以检查我的情况,但我可以只在 oracle 中使用一个触发器吗?
您可以按以下方式进行操作:
create or replace trigger my_trigger
before delete or update of status
on tableA
for each row
WHEN (NEW.status= 'C' OR NEW.status IS NULL)
begin
insert into tableB values(:OLD.rollno,:OLD.name);
end;
仅查找具有 update of status
的列状态的更新。然后仅在新列值匹配某些条件时执行触发器 WHEN (NEW.status= 'C' OR NEW.status IS NULL)
检查您的值或 null(在删除情况下)。
是的,IF 可以提供帮助。
这是一个基于 Scott 架构的示例;看看。
这是一个 log table:
SQL> create table deptb as select * From dept where 1 = 2;
Table created.
触发器:
SQL> create or replace trigger trg_bdu_dept
2 before delete or update on dept
3 for each row
4 begin
5 if deleting then
6 insert into deptb values (:old.deptno, :old.dname, :old.loc);
7 elsif updating and :old.loc = 'NEW YORK' then
8 insert into deptb values (:old.deptno, :old.dname, :old.loc);
9 end if;
10 end;
11 /
Trigger created.
SQL>
测试:
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------------- --------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> delete from dept where deptno = 40;
1 row deleted.
SQL> update dept set loc = 'NY' where loc = 'NEW YORK';
1 row updated.
SQL> update dept set loc = 'dallas' where loc = 'DALLAS';
1 row updated.
SQL> select * from deptb;
DEPTNO DNAME LOC
---------- -------------------- --------------------
40 OPERATIONS BOSTON
10 ACCOUNTING NEW YORK
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------------- --------------------
10 ACCOUNTING NY
20 RESEARCH dallas
30 SALES CHICAGO
SQL>