New to Nutbox?

Automation Catalyst - Triggers - Steem Dev Community.

2 comments

bountyking5
79
18 days agoSteemit3 min read

Hello Everyone,


1_20240416_171005_0000.png

Good evening to all, how are you. I hope you all friends will be well and good. By the grace of God, i am also good. Today i am here with new post in which we will discuss the triggers in PLSQL .

It will not wrong if we will say that triggers are automation catalyst. They handle complex tasks, ensure data is correct, and automate jobs smoothly. Triggers are written in a PLSQL. In this blog, we will explore what PLSQL triggers are and how it work and see some examples of how they are used in real life.

PLSQL triggers are stored instructions and containing blocks of code. They run automatically when specific events happen on database tables or views. These events could be things like inserting, updating, deleting data or making structural changes to the database. Triggers act like watchful protectors and responding to these events. Now lets discuss and break down how they are written and how they work.

Syntax


CREATE [OR REPLACE] TRIGGER trigger_name
BEFORE | AFTER | INSTEAD OF event
ON table_name
[FOR EACH ROW | FOR EACH STATEMENT]
DECLARE
    -- Variable declarations
BEGIN
    -- Trigger logic
EXCEPTION
    -- Exception handling
END;


CREATE TRIGGER: It will used to make new trigger or replaces an existing one with the given name.
BEFORE/AFTER/INSTEAD OF: Determines when the trigger will activate before, after, or instead of the event that triggers it.
ON table_name: Specifies the table or view where the trigger will work.
DECLARE: Local variables will define here.
BEGIN-END: Contains the instructions or tasks to be done when the trigger is activated.
EXCEPTION: Handles any errors that might occur while the trigger is running.

CREATE OR REPLACE TRIGGER emp_trigger
BEFORE UPDATE ON employees
FOR EACH ROW
DECLARE
    o_data employees%ROWTYPE;
BEGIN
    o_data := :O;

    -- Insertion of new data.
    INSERT INTO employee_data (employee_id, old_name, new_name, updated_date)
    VALUES (:O.employee_id, o_data.employee_name, :NEW.employee_name, SYSDATE);
END;


In the above code we create a trigger with name emp_trigger to fire before an updating operation on the employees table. We declare a variable o_data of type employees%ROWTYPE to store the old data before the update. Then insert new record into the employee_data table by recording the employee ID, old name, new name, and the updated date.

It would be my pleasure if you will share your opinion and reviews on this topic.

Please cast witness vote to @bangla.Witness or set proxy to @rme.

_
Vote @bangla.witness as witness
Set Proxy @rme as proxy

Special Thanks.

Cc: @rme
Cc: @hungry-griffin
Cc: @pennsif

Thanks for giving your precious time to my post.
Follow | Upvote | Share | Comments
Follow me on Instagram.
Instagram

πŸ†ƒπŸ…·πŸ…°οΈπŸ…½πŸ…Ί πŸ†ˆπŸ…ΎοΈπŸ†„

Comments

Sort byBest