2013-11-20 60 views
-1

我有这种说法INSTEAD OF触发器,插上视图

CREATE VIEW NaveTiconderoga AS 
SELECT nume, tip, cate_arme, diametru_tun, deplasament, Nave.clasa, anul_lansarii 
FROM Clase, Nave 
WHERE Clase.clasa = Nave.Clasa AND Nave.Clasa = 'Ticonderoga'; 

我想创建一个触发器,以允许通过此视图插入。

我写了下面的代码,但我确信它是不正确的,直到从SELECT的WHERE子句。 任何指针?

CREATE OR REPLACE TRIGGER ticonderoga 
instead of insert on NaveTiconderoga 
referencing new as new old as old 
begin 
insert into clase (clasa, tip, cate_arme, diametru_tun, deplasament) 
values (:new.clasa, :new.tip, :new.cate_arme, :new.diametru_tun, :new.deplasament); 

insert into nave (nume, clasa, anul_lansarii) 
values (:new.nume, :new.clasa, :new.anul_lansarii); 
end; 
+4

什么“不正确”?你还没有发布你的表格定义。您没有向我们展示您正在使用的'INSERT'语句的示例。你没有告诉我们触发器实际上做了什么。你没有告诉我们这与你想要的触发器有什么不同。 –

+0

你的意思是说它允许你在视图中插入一条记录,然后它不会出现在视图中;因为你可以插入一个不是“Ticonderoga”的clasa值? –

+0

我想说的是,我将如何解释这在触发器内部“WHERE Clase.clasa = Nave.Clasa AND Nave.Clasa ='Ticonderoga';” –

回答

0

如果你想限制插入视图(因此底层表)的值,所以你不能把在一些视图本身不会显示,你不能使用支票约束;但你可以测试触发器内的值,如果你看到的东西抛出一个异常,你不喜欢:

CREATE OR REPLACE TRIGGER ticonderoga 
instead of insert on NaveTiconderoga 
referencing new as new old as old 
begin 
    if :new.clasa is null or :new.clasa != 'Ticonderoga' then 
    raise_application_error(-20001, 'Invalid clasa'); 
    end if; 

    insert into clase (clasa, tip, cate_arme, diametru_tun, deplasament) 
... 

什么,我觉得你担心SQL Fiddle。如果将触发器更改为:

create trigger tr42 
instead of insert on v42 
begin 
if :new.id != 1 then 
raise_application_error(-20001, 'Invalid ID'); 
end if; 
insert into t42 (id) values (:new.id); 
end; 
/

...然后第二次插入将失败。无论如何,我认为这就是你想要发生的事情。

相关问题