oracle视图触发器问题
问题是这样的我现在有两个表 table_a 和 table_b, 还有一个视图 view_a, 表和视图的结构如下
但我对 table_b 没有任何操作权限,只对 table_a 和 view_a 有操作权限, 我现在要实现的功能是 table_b 中的数据有变化(inser, update, delete)时,就将 table_a 中的记录对应更新, 于是建立了下面这个触发器:
现在问题就来了, 对方(有权限操作table_b的)执行
时,并没有触发触发器 trg_insurance, 而只有执行
时,触发器 trg_insurance 才会执行.
也就是说只有直接在 view_a 上作对应的操作时触发器 trg_insurance 才会被执行, 而直接操作实体表 table_b 时,触发器无法被执行, 请问我该怎么创建触发器才能在直接对实体表的数据进行修改(insert, update, delete)时,才能将触发器 trg_insurance 触发。(特别注意的是,我对 table_b 没有任何权限, 所以不可能直接在 table_b 上创建触发器)
-- Create table
create table table_a
(
a number,
b varchar2(30),
c varchar2(30)
);
-- Create/Recreate primary, unique and foreign key constraints
alter table table_a
add constraint PK_table_a primary key (A);
===================================================================
-- Create table
create table table_b
(
a number,
b varchar2(30),
c varchar2(30)
);
-- Create/Recreate primary, unique and foreign key constraints
alter table table_b
add constraint PK_table_b primary key (A);
===================================================================
create view view_a as select * from table_b;
但我对 table_b 没有任何操作权限,只对 table_a 和 view_a 有操作权限, 我现在要实现的功能是 table_b 中的数据有变化(inser, update, delete)时,就将 table_a 中的记录对应更新, 于是建立了下面这个触发器:
create or replace trigger trg_insurance
instead of update or insert or delete on view_a
begin
if inserting then
insert into table_a(a,b,c) values(:new.a,:new.b,:new.c);
elsif updating then
if (:new.b=1) then
insert into table_a(a,b,c) values(:new.a,:new.b,:new.c);
elsif (:new.b=0) then
delete from table_a where a=:old.a;
end if;
elsif deleting then
delete from table_a where a=:old.a;
end if;
end;
现在问题就来了, 对方(有权限操作table_b的)执行
//将 insert 改为对应的 update, delete 语句时一样不会触发
insert into table_b values(1,'1','1');
commit;
时,并没有触发触发器 trg_insurance, 而只有执行
//将 insert 改为对应的 update, delete 语句时也会执行触发器trg_insurance
insert into view_a values(1,'1','1');
commit;
时,触发器 trg_insurance 才会执行.
也就是说只有直接在 view_a 上作对应的操作时触发器 trg_insurance 才会被执行, 而直接操作实体表 table_b 时,触发器无法被执行, 请问我该怎么创建触发器才能在直接对实体表的数据进行修改(insert, update, delete)时,才能将触发器 trg_insurance 触发。(特别注意的是,我对 table_b 没有任何权限, 所以不可能直接在 table_b 上创建触发器)
...全文
相关推荐
Oracle中基于多表的可更新视图的实现与应用.pdf
同时,基于多表联合的可更新视图的应用也需要考虑到系统的性能和安全性等问题。
ORACLE PL_SQL编程之八:把触发器说透.pdf
理解并熟练运用触发器是Oracle PL/SQL编程的关键技能之一,但同时也需要注意合理使用,避免引入不必要的复杂性和潜在的性能问题。
在Oracle中向视图中插入数据的方法
通过了解这些概念和技巧,你可以更有效地管理和操作Oracle数据库中的视图,特别是在需要在视图上执行插入操作时。同时,注意权限问题,确保你有足够的权限执行这些操作。
Oracle-把触发器看透.docx
DML触发器可以针对单个行或整个语句操作,对于无法直接对多表视图进行操作的情况,Oracle提供了替代触发器。2.
Oracle应用项目—— 触发器实例.pdf
然而,需要注意的是,过度使用触发器可能会导致性能问题,因此在设计数据库时应谨慎使用。理解触发器的工作原理和最佳实践对于任何Oracle数据库的开发者和管理员来说都是至关重要的。


