What is the usage of WHEN clause in trigger?

A WHEN clause specifies the condition that must be true for the trigger to be triggered.

In PL/SQL triggers, the WHEN clause is used to specify a condition under which the trigger should be fired. The WHEN clause allows you to define a condition that, when true, activates the trigger and executes its associated trigger body.

Here’s a basic syntax example:

CREATE OR REPLACE TRIGGER trigger_name
BEFORE INSERT ON your_table
FOR EACH ROW
WHEN (your_condition)
BEGIN
-- Trigger body
-- This part will be executed when the WHEN condition is true
END;

In this example, the trigger is set to fire before an INSERT operation on your_table for each row. The WHEN clause specifies the condition that must be satisfied for the trigger to be activated.

It’s important to note that the WHEN clause is optional, and if it is not specified, the trigger will fire for every applicable event. When you do use the WHEN clause, the trigger will only execute its body if the specified condition is true.

For example, you might use the WHEN clause to create a trigger that only fires for specific values or ranges of values in a column, ensuring that the trigger’s actions are selective based on a given condition.