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

PLSQL Triggers

Trigger

Uploaded by

Mukesh Mohan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

PLSQL Triggers

Trigger

Uploaded by

Mukesh Mohan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PL/SQL - TRIGGERS

http://www.tuto rialspo int.co m/plsql/plsql_trig g e rs.htm


Co pyrig ht tuto rials po int.co m

T rig g ers are stored prog rams, which are automatically executed or fired when some events occur. T rig g ers are, in fact, written to be executed in response to any of the following events: A database manipulation (DML) statement (DELET E, INSERT , or UPDAT E). A database definition (DDL) statement (CREAT E, ALT ER, or DROP). A database operation (SERVERERROR, LOGON, LOGOFF, ST ART UP, or SHUT DOWN). T rig g ers could be defined on the table, view, schema, or database with which the event is associated.

Benefits of Trig g ers


T rig g ers can be written for the following purposes: Generating some derived column values automatically Enforcing referential integ rity Event log g ing and storing information on table access Auditing Synchronous replication of tables Imposing security authorizations Preventing invalid transactions

Creating Trig g ers


T he syntax for creating a trig g er is:
CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | AFTER | INSTEAD OF } {INSERT [OR] | UPDATE [OR] | DELETE} [OF col_name] ON table_name [REFERENCING OLD AS o NEW AS n] [FOR EACH ROW] WHEN (condition) DECLARE Declaration-statements BEGIN Executable-statements EXCEPTION Exception-handling-statements END;

Where, CREAT E [OR REPLACE] T RIGGER trig g er_name: Creates or replaces an existing trig g er with the trigger_name . {BEFORE | AFT ER | INST EAD OF} : T his specifies when the trig g er would be executed. T he INST EAD OF clause is used for creating trig g er on a view. {INSERT [OR] | UPDAT E [OR] | DELET E}: T his specifies the DML operation. [OF col_name]: T his specifies the column name that would be updated. [ON table_name]: T his specifies the name of the table associated with the trig g er.

[REFERENCING OLD AS o NEW AS n]: T his allows you to refer new and old values for various DML statements, like INSERT , UPDAT E, and DELET E. [FOR EACH ROW]: T his specifies a row level trig g er, i.e., the trig g er would be executed for each row being affected. Otherwise the trig g er will execute just once when the SQL statement is executed, which is called a table level trig g er. WHEN (condition): T his provides a condition for rows for which the trig g er would fire. T his clause is valid only for row level trig g ers.

Example:
T o start with, we will be using the CUST OMERS table we had created and used in the previous chapters:
Select * from customers; +----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | +----+----------+-----+-----------+----------+

T he following prog ram creates a row level trig g er for the customers table that would fire for INSERT or UPDAT E or DELET E operations performed on the CUST OMERS table. T his trig g er 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 (NEW.ID > 0) DECLARE sal_diff number; BEGIN sal_diff := :NEW.salary - :OLD.salary; dbms_output.put_line('Old salary: ' || :OLD.salary); dbms_output.put_line('New 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.

Here following two points are important and should be noted carefully: OLD and NEW references are not available for table level trig g ers, rather you can use them for record level trig g ers. If you want to query the table in the same trig g er, then you should use the AFT ER keyword, because trig g ers can query the table or chang e it ag ain only after the initial chang es are applied and the table is back in a consistent state. Above trig g er has been written in such a way that it will fire before any DELET E or INSERT or UPDAT E operation on the table, but you can write your trig g er on a sing le or multiple operations, for example BEFORE DELET E, which will fire whenever a record will be deleted using DELET E operation on the table.

Trig g ering a Trig g er

Let us perform some DML operations on the CUST OMERS table. Here is one INSERT statement, which will create a new record in the table:
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (7, 'Kriti', 22, 'HP', 7500.00 );

When a record is created in CUST OMERS table, above create trig g er display_salary_c hang es 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 CUST OMERS table. Here is one UPDAT E statement, which will update an existing record in the table:
UPDATE customers SET salary = salary + 500 WHERE id = 2;

When a record is updated in CUST OMERS table, above create trig g er display_salary_c hang es will be fired and it will display the following result:
Old salary: 1500 New salary: 2000 Salary difference: 500

You might also like