0% found this document useful (0 votes)
11 views

PL_SQL - Triggers

Uploaded by

Ayush Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

PL_SQL - Triggers

Uploaded by

Ayush Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PL/SQL - Triggers

● Triggers are stored programs, which are automatically executed or fired


when some events occur. Triggers are, in fact, written to be executed in
response to any of the following events:
○ A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).
○ A database definition (DDL) statement (CREATE, ALTER, or DROP).
○ A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or
SHUTDOWN).
● Triggers could be defined on the table, view, schema, or database with which
the event is associated.
Benefits of Triggers
● Triggers can be written for the following purposes:
● Generating some derived column values automatically
● Enforcing referential integrity
● Event logging and storing information on table access
● Auditing
● Synchronous replication of tables
● Imposing security authorizations
● Preventing invalid transactions
Creating Triggers
● The syntax for creating a trigger is:

CREATE [OR REPLACE] TRIGGER trigger_name WHEN (condition)


{BEFORE | AFTER | INSTEAD OF} DECLARE
{INSERT (OR) UPDATE (OR) DELETE} Declaration-statements
[OF col_name] BEGIN
ON table name Executable-statements
[REFERENCING OLD AS NEW AS n EXCEPTION
[FOR EACH ROW] Exception-handling statements
END;
Trigger Example:
Simple Trigger to Update last_updated
Example Table: Field:
CREATE TABLE employees ( This trigger will activate after an update is
made to the employees table and will set the
employee_id INT PRIMARY KEY, last_updated field to the current timestamp
CREATE TRIGGER update_last_updated
name VARCHAR(100),
AFTER UPDATE ON employees
position VARCHAR(100),
FOR EACH ROW
salary DECIMAL(10, 2), BEGIN
UPDATE employees
last_updated TIMESTAMP
SET last_updated = CURRENT_TIMESTAMP
); WHERE employee_id = NEW.employee_id;
Trigger to Prevent Deletion if Stock > 100:
Trigger Example:
This trigger will prevent the deletion of products with stock
greater than 100 units.
Example Table:
CREATE TRIGGER prevent_high_stock_deletion
CREATE TABLE products ( BEFORE DELETE ON products
product_id INT PRIMARY FOR EACH ROW
KEY, BEGIN
IF OLD.stock > 100 THEN
product_name VARCHAR(100),
SIGNAL SQLSTATE '45000'
stock INT
SET MESSAGE_TEXT = 'Cannot delete
); product with more than 100 units in stock.';
END IF;
Trigger Example:
● The following program creates 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:

CREATE OR REPLACE TRIGGER display_salary_changes


BEFORE DELETE OR INSERT OR UPDATE ON customers.
FOR EACH ROW
WHEN (NEWID>0)
DECLARE
sal diff number;
BEGIN
sal diff :=: NEW.salary -:OLD.salary:
dbms_output.put line('Old salary:’||:OLD-salary);
dbms_output.put line("Now salary:’ || NEW.salary);
dbms_output.put line("Salary difference:’ || sal_diff);
END:
When the above code is executed at SQL prompt, it produces the following result: Trigger created.
Triggering a Trigger
● Let us perform some DML operations on the CUSTOMERS table. Here is one INSERT statement, which will create a
new record in the table: INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARYJVALUES (7, 'Kriti',
22, 'HP', 7500.00);
When a record is created in CUSTOMERS table, above create trigger display_salary_changes will be fired and it will
display the following result:
Old salary
New salary: 7500
Salary difference

● Because this is a new record so old salary is not available and above result is coming as null.
Now, let us perform one more DML operation on the CUSTOMERS table. Here is one UPDATE statement, which will
update an existing record in the table:
UPDATE customersSET salary salary + 500WHERE id=2;

● When a record is updated in CUSTOMERS table, above create trigger display salary_changes will be fired and it will
display the following result:
Old salary: 1500
New salary: 2000

You might also like