Create a PLSQL Trigger to display the message - cover image

Create a PL/SQL Trigger to display the message "NEW EMPLOYEE DETAILS INSERTED", whenever a new record is inserted into Employee table

Table: EMPLOYEE

COLUMN DATATYPE CONS.
EMPID NUMBER(5) PK
EMP_NAME VARCHAR2(25) NOT NULL
SALARY NUMBER(10,2)
(Hint: Data is case sensitive. Use '/' to terminate the PLSQL block)

Step-by-Step Guide to Creating the Trigger

Introduction to Triggers:
First, understand that a trigger is a special kind of stored procedure that automatically executes when a specific event occurs in the database.

Choosing the Event:
In this case, we want the trigger to execute after a new employee record is inserted into the Employee table.

Writing the Trigger Code:
Let's break down the trigger code step by step.

Trigger Declaration:
We start by declaring the trigger with a name. Here, it's called display:

CREATE TRIGGER display
This line tells the database we're creating a new trigger called display.

Specifying the Timing and Event:
Next, we specify when this trigger should run and on which table. We want it to run after a new row is inserted into the Employee table:

AFTER INSERT ON Employee
This means the trigger will activate right after an insertion occurs.

Defining the Scope:
We need the trigger to execute for each row that's inserted, so we include:

FOR EACH ROW

Writing the Trigger Body:
Now, we define what the trigger should do. In this case, we want to display a message:

BEGIN
  dbms_output.put_line('NEW EMPLOYEE DETAILS INSERTED');
END;
/
BEGIN and END are used to enclose the actions the trigger will perform.
dbms_output.put_line is a procedure that outputs a line of text. Here, it's used to display 'NEW EMPLOYEE DETAILS INSERTED'.

Conclusion:
With this trigger in place, every time a new employee is added to the Employee table, a message will be automatically displayed, providing immediate feedback and helping with database monitoring and logging.

Leave a comment