How are triggers defined in SQL? Explain with examples

How are triggers defined in SQL? Explain with examples

Answer:-

A trigger is a procedure that automatically runs when a specific event occurs in the DBMS. It’s useful for specifying actions to be taken when certain events happen and conditions are met. The CREATE TRIGGER statement is used to define these actions in SQL.

General form:

CREATE TRIGGER <name>
BEFORE | AFTER | <events>
FOR EACH ROW | FOR EACH STATEMENT
WHEN (<condition>)
<action>

A trigger has three main components:

  1. Event: The trigger is activated when this event occurs. Events can be Insert, Update, or Delete and it has 2 triggering times: Before the event, After the event
  2. Condition (optional): If the condition is true, the trigger executes; otherwise, it’s skipped.
  3. Action: The actions performed by the trigger when the event occurs and the condition is true.

Leave a Reply

Your email address will not be published. Required fields are marked *