- Introduction.
- What are triggers?
- How does a trigger work?
- What is the type of trigger?
- DB2 triggers vs store procedures.
- How do you create triggers?
- Trigger syntax details.
- Triggers Example.
- Conclusion.
Introduction.
What are the Triggers in DB2?
How does a trigger work?
What are the types of triggers?
- INSERT Trigger: An insert trigger is defined on the DB2 table. It is triggered automatically each time an insert operation is performed on the table via the INSERT SQL statement.
- UPDATE Trigger: An update trigger is defined on the DB2 table. It is triggered automatically each time an update operation is performed on the table via the UPDATE SQL statement.
- DELETE Trigger: An delete trigger is defined on the DB2 table. It is triggered automatically each time a delete operation is performed on the table via the DELETE SQL statement.
- INSTEAD OF Trigger: The instead of trigger is different from above mention trigger types because it's defined on the table views. It provides logic to make non-updatable views to be updatable.
DB2 Trigger vs Stored Procedures.
- A stored procedure, on the other hand, is not event-driven and does not belong to an individual table.
- Stored procedures are explicitly executed using a CALL. However, Triggers are initiated automatically by the database.
- Additionally, stored procedures can access many tables without being specifically attached to any of them. Triggers can modify other tables, but their execution is tied to an individual table.
How do you create triggers?
- SQL CREATE A TRIGGER: SQL statement to define a trigger in DB2.
- SQL ALTER TRIGGER: SQL statement to modify or alter existing defined trigger in DB2.
CREATE Trigger syntax details.
- The create statement starts with CREATE TRIGGER keyword followed by the trigger name. The trigger name should be unique and it should be as per project standards.
- The trigger activation time determines when the trigger event will occur. You can put the condition BEFORE, AFTER, or INSTEAD OF.
- Activation of a trigger is caused by a trigger event. The operation can be INSERT, UPDATE, or DELETE. If more than one event is specified, the keyword OR can be used, for example, INSERT OR DELETE. The subject table is the table or view for which the trigger is defined.
- For any columns or rows that are involved in the trigger event, transition variables or transition tables provide before and after values for those columns and rows.
- For each row or for each statement, the trigger granularity determines whether the action is executed for each row or for each statement changed. An UPDATE operation, for example, may update seven rows, and the trigger is triggered once or seven times, depending on this attribute.
- When the trigger is set to MODE DB2ROW, it is activated after each row operation. In MODE DB2SQL, triggers are activated after every row operation.
- As soon as the trigger is triggered, the triggered action is executed. The action can be simple or complex. It can begin with a WHEN clause, which can include any true/false condition.
DB2 Trigger Example.
CREATE TRIGGER NEW_EMPLOYEEAFTER INSERT ON EMPLOYEEFOR EACH ROW MODE DB2SQLUPDATE EMPSTATSET EMPCNT = EMPCNT + 1;
2. Now, let's check the value of EMPCNT in the EMPSTAT table.
SELECT EMPCNT FROM EMPSTAT;
3. Let's insert data into EMPLOYEE and then check the value of EMPCNT.
INSERT INTO Employee (empno, firstnme, midinit,
4. Let's check the value of EMPCNT from EMPSTAT table.
SELECT EMPCNT FROM EMPSTAT;
Youtube Tutorial: Triggers in DB2 | DB2 Triggers | Create Triggers Syntax.
Conclusion.
Finally, this marks an end to the DB2 Triggers or simply triggers in DB2. In this tutorial, you will learn the basic fundamental of triggers. DB2 Triggers allow developers to implement code that runs periodically in response to predefined events. The triggers concept is the same for all relational databases. However, the syntax would be different for different databases. Do check out COBOL LEVEL 88 Condition.
No comments:
Post a Comment