日期:2014-05-16 浏览次数:20372 次
语法:
CREATE OR REPLACE TRIGGER trigger_name INSTEAD OF operation --delete,update,insert,create and so on ON view_name FOR EACH ROW BEGIN ... END;
hr@ORCL> CREATE TABLE A (ID NUMBER(5),COL VARCHAR2(5)); Table created. hr@ORCL> CREATE TABLE B (ID NUMBER(5),COL VARCHAR2(5)); Table created. hr@ORCL> CREATE OR REPLACE VIEW V_AB AS 2 SELECT A.ID,A.COL AS COLA,B.COL AS COLB 3 FROM A,B 4 WHERE A.ID=B.ID 5 / View created. hr@ORCL> CREATE OR REPLACE TRIGGER TRG_V_AB 2 INSTEAD OF INSERT ON V_AB 3 FOR EACH ROW 4 BEGIN 5 INSERT INTO A(ID,COL)VALUES(:NEW.ID,:NEW.COLA); 6 INSERT INTO B(ID,COL)VALUES(:NEW.ID,:NEW.COLB); 7 END; 8 / Trigger created. hr@ORCL> SELECT TRIGGER_NAME,TRIGGER_TYPE FROM USER_TRIGGERS; TRIGGER_NAME TRIGGER_TYPE ------------------------------ ---------------- TRG_V_AB INSTEAD OF hr@ORCL> INSERT INTO V_AB VALUES(1,'AA','BB'); 1 row created. hr@ORCL> COMMIT; Commit complete. hr@ORCL> select * from a; ID COL ---------- ----- 1 AA hr@ORCL> select * from b; ID COL ---------- ----- 1 BB hr@ORCL> CREATE OR REPLACE VIEW V_AB AS 2 SELECT A.ID,B.COL AS COLA,A.COL AS COLB 3 FROM A,B 4 WHERE A.ID=B.ID 5 / View created. hr@ORCL> INSERT INTO V_AB VALUES(1,'AA','BB'); INSERT INTO V_AB VALUES(1,'AA','BB') * ERROR at line 1: ORA-01779: cannot modify a column which maps to a non key-preserved table hr@ORCL> SELECT TRIGGER_NAME,TRIGGER_TYPE FROM USER_TRIGGERS; no rows selected.