91-9990449935 0120-4256464 |
PL/SQL TriggerTrigger is invoked by Oracle engine automatically whenever a specified event occurs.Trigger is stored into database and invoked repeatedly, when specific condition match. Triggers are stored programs, which are automatically executed or fired when some event occurs. Triggers are written to be executed in response to any of the following events.
Triggers could be defined on the table, view, schema, or database with which the event is associated. Advantages of TriggersThese are the following advantages of Triggers:
Creating a trigger:Syntax for creating trigger: Here,
PL/SQL Trigger ExampleLet's take a simple example to demonstrate the trigger. In this example, we are using the following CUSTOMERS table:
Create table and have records:
Create trigger: Let's take a program to create a row level trigger for the CUSTOMERS table that would fire for INSERT or UPDATE or DELETE operations performed on the CUSTOMERS table. This trigger will display the salary difference between the old values and new values: After the execution of the above code at SQL Prompt, it produces the following result. Trigger created. Check the salary difference by procedure: Use the following code to get the old salary, new salary and salary difference after the trigger created. Output: Old salary: 20000 New salary: 25000 Salary difference: 5000 Old salary: 22000 New salary: 27000 Salary difference: 5000 Old salary: 24000 New salary: 29000 Salary difference: 5000 Old salary: 26000 New salary: 31000 Salary difference: 5000 Old salary: 28000 New salary: 33000 Salary difference: 5000 Old salary: 30000 New salary: 35000 Salary difference: 5000 6 customers updated Note: As many times you executed this code, the old and new both salary is incremented by 5000 and hence the salary difference is always 5000. After the execution of above code again, you will get the following result. Old salary: 25000 New salary: 30000 Salary difference: 5000 Old salary: 27000 New salary: 32000 Salary difference: 5000 Old salary: 29000 New salary: 34000 Salary difference: 5000 Old salary: 31000 New salary: 36000 Salary difference: 5000 Old salary: 33000 New salary: 38000 Salary difference: 5000 Old salary: 35000 New salary: 40000 Salary difference: 5000 6 customers updated Important PointsFollowing are the two very important point and should be noted carefully.
Next TopicPL/SQL Interview Questions
|