DB2 trigger on a view

This article will discuss the trigger on a view, other than a table.

Firstly, let's define updatable view, then continue define trigger on view.


1. Updatable View
   A view is read-only if one or more of the following statements is true of its definition:
- Start of changeThe first FROM clause identifies more than one table or view, or identifies a table function, a nested table expression, a common table expression, or a collection-derived table.End of change
- The first SELECT clause specifies the keyword DISTINCT.
- The outer fullselect contains a GROUP BY clause.
- The outer fullselect contains a HAVING clause.
- The first SELECT clause contains an aggregate function.
- It contains a subquery such that the base object of the outer fullselect, and of the subquery, is the same table.
- The first FROM clause identifies a read-only view.
- The first FROM clause identifies a system-maintained materialized query table.
- The outer fullselect is not a subselect (contains a set operator).

   Even when a view is updatable, there are difference between Deletable, Insertable, and Updatable, see https://mainframealldtime.wordpress.com/2012/05/01/views-in-db2/

2. Trigger on View
   You cannot define a general trigger on a a view, except INSTEAD OF trigger, and in fact, INSTEAD OF trigger could only be defined on view.
   INSTEAD OF triggers are triggers that execute instead of the INSERT, UPDATE, or DELETE statement that activates the trigger.
   
   INSTEAD OF trigger:
   When specify that a trigger is an INSTEAD OF trigger, DB2 will not try to interpret the view definition for the updating operation, "Instead of" doing that, DB2 will execute the trigger body actions, replaces the original INSERT, UPDATE, and DELETE action. In other words, the original INSERT, UPDATE, or DELETE operation is skipped, the trigger action will be executed.
   - Could only be defined on a view
   - Support row-level trigger only, i.e., FOR EACH ROW only
   - For each UPDATE, INSERT, and DELETE only on trigger could be created.
 
3. Cases of table trigger fire condition when updating on a view

CREATE TABLE TESTTAB(A INT, B CHAR(4), C CHAR(4) DEFAULT '0000')
CREATE VIEW  TESTVIEW(A, B) AS SELECT A, B FROM TESTTAB

-- CREATE TRIGGER ON TABLE TESTTAB
CREATE TRIGGER TESTTRG_TESTTAB AFTER INSERT ON TESTTAB REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL VALUES(SELECT 1 FROM SYSIBM.SYSDUMMY1)

INSERT INTO TESTVIEW VALUES(1, '1111')
-- TABLE TRIGGER TESTTRG_TESTTAB IS FIRED
-- THAT MEANS AS LONG AS DB2 CAN MAP THE VIEW INSERT STATEMENT INTO TABLE OPERATION, IT WORKS AS TABLE INSERT STATEMENT

-- CREATE TRIGGER ON VIEW TESTVIEW
CREATE TRIGGER TESTTRG_TESTVIEW INSTEAD OF INSERT ON TESTVIEW REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL VALUES(SELECT 1 FROM SYSIBM.SYSDUMMY1)

INSERT INTO TESTVIEW VALUES(2, '2222')
-- VIEW TRIGGER TESTTRG_TESTVIEW IS FIRED, BUT
-- TABLE TRIGGER TESTTRG_TESTTAB IS NOT FIRED

-- RECREATE TRIGGER ON VIEW TESTVIEW, WITH ACTION TO INSERT ON TABLE
DROP TRIGGER TESTTRG_TESTVIEW
CREATE TRIGGER TESTTRG_TESTVIEW INSTEAD OF INSERT ON TESTVIEW REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL INSERT INTO TESTTAB(A, B, C) VALUES(N.A, N.B, '0000')	

INSERT INTO TESTVIEW VALUES(3, '3333')
-- VIEW TRIGGER TESTTRG_TESTVIEW IS FIRED, AND
-- TABLE TRIGGER TESTTRG_TESTTAB IS FIRED TOO


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值