Triggers
Group Members:
Awais Masood
Asad Azad
Sharoze
Triggers
• Named SQL blocks Which are Stored in Database
• Specialized Stored Proggrams which execute implicitly
• when a triggering event occurs
• Three parts:
– Event (activates the trigger)
– Condition (tests whether the triggers should run) [Optional]
– Action (what happens if the trigger runs)
• Semantics:
– When event occurs, and condition is satisfied,
– the action is performed.
Events
•A DML Statments
•A DDL Statments
•A System events
Types
• DML triggers
• DDL Triggers
• System/Database Event Triggers
• Logon Triggers
• Compound Triggers
Syntax
CREATE TRIGGER <triggerName>
BEFORE|AFTER INSERT|DELETE|UPDATE
[OF <columnList>] ON
<tableName>|<viewName>
[REFERENCING [OLD AS <oldName>] [NEW AS
<newName>]]
[FOR EACH ROW] (default is “FOR EACH STATEMENT”)
[WHEN (<condition>)]
<PSM body>;
Example Trigger
CREATE TRIGGER minSalary
BEFORE UPDATE ON Professor
REFERENCING OLD AS oldTuple NEW as newTuple
FOR EACH ROW
WHEN (newTuple.salary < oldTuple.salary)
BEGIN
RAISE_APPLICATION_ERROR (-20004, ‘Salary Decreasing !!’);
END;
.
run;
Uses
• Complex Auditing
• Automatic Generates Value
• Replica / Duplicate data

Triggers

  • 1.
  • 2.
    Triggers • Named SQLblocks Which are Stored in Database • Specialized Stored Proggrams which execute implicitly • when a triggering event occurs • Three parts: – Event (activates the trigger) – Condition (tests whether the triggers should run) [Optional] – Action (what happens if the trigger runs) • Semantics: – When event occurs, and condition is satisfied, – the action is performed.
  • 3.
    Events •A DML Statments •ADDL Statments •A System events
  • 4.
    Types • DML triggers •DDL Triggers • System/Database Event Triggers • Logon Triggers • Compound Triggers
  • 5.
    Syntax CREATE TRIGGER <triggerName> BEFORE|AFTERINSERT|DELETE|UPDATE [OF <columnList>] ON <tableName>|<viewName> [REFERENCING [OLD AS <oldName>] [NEW AS <newName>]] [FOR EACH ROW] (default is “FOR EACH STATEMENT”) [WHEN (<condition>)] <PSM body>;
  • 6.
    Example Trigger CREATE TRIGGERminSalary BEFORE UPDATE ON Professor REFERENCING OLD AS oldTuple NEW as newTuple FOR EACH ROW WHEN (newTuple.salary < oldTuple.salary) BEGIN RAISE_APPLICATION_ERROR (-20004, ‘Salary Decreasing !!’); END; . run;
  • 7.
    Uses • Complex Auditing •Automatic Generates Value • Replica / Duplicate data