You are on page 1of 21

PostgreSQL Triggers

(TAE 2)

Subject: DBMS
Guided by: Prof. Neha Purohit
-Submitted by: Varun Dalal (CSE -4-C-25)
What is a ‘TRIGGER’?
• A trigger is a function that is triggered automatically when a database
event like INSERT, UPDATE or DELETE occurs.

• We say that ‘ A trigger has been fired/executed/invoked’.

• Triggers are used to maintain the referential integrity of data by


changing the data in a systematic fashion.

• Triggers cannot be manually called. They are always invoked by the


SQL statements containing the specified events.
Difference between Functions and
Triggers
• A Function is called explicitly by the user using a
function call along with the arguments.

• On the other hand, Trigger is always implicitly fired


whenever the triggering event occurs.
Syntax
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF}
{INSERT | UPDATE | DELETE}
ON table_name
{FOR EACH ROW| FOR EACH STATEMENT}
EXECUTE PROCEDURE trig_func();
Lets examine the syntax line by line!!!
CREATE [OR REPLACE] TRIGGER trigger_name

• This statement is clearly used to create a trigger named


‘trigger_name’.
• REPLACE is used to overwrite any previous trigger with the
same name.
{BEFORE | AFTER | INSTEAD OF}
The BEFORE, AFTER and INSTEAD OF are the keywords that determine
when the trigger will be fired.

BEFORE trigger will be fired before the triggering SQL statement is


executed.

AFTER trigger will be fired after the triggering SQL statement is executed.

INSTEAD OF trigger will be fired when the user inserts data into a table
via a view.
{INSERT | UPDATE | DELETE}
• These are the event names. The trigger will be triggered when
the SQL statement contains any of these events.

ON table_name
• This statement specifies the table name on which the trigger is
being applied
{FOR EACH ROW| FOR EACH STATEMENT}

• FOR EACH ROW defines that the trigger will be fired


individually for all the rows affected by the event.

• FOR EACH STATEMENT defines that the trigger will be fired


only once for the event.
EXECUTE PROCEDURE trig_func();
• Here we call the trigger function so as the required task is
performed.
Procedure for creating a trigger in
PostgreSQL
1. Create a Trigger function using CREATE FUNCTION
statement and state the return type as ‘trigger’.
2. Bind the trigger function to a trigger using CREATE TRIGGER
statement.
•Lets see some examples!!!
1. BEFORE INSERT
1 4

Notice the spaces in First and Last Name…


Also the DEPT is in lowercase
2
Output

3
2. AFTER INSERT
1 4

2
5

6
3
3.AFTER UPDATE
4
1

3 6

Output
4.BEFORE UPDATE
1 3

2
5.BEFORE DELETE
1 3

4
2

5
6.AFTER DELETE
1 4

2
5

3
DISABLING TRIGGER
ALTER TABLE table_name
DISABLE TRIGGER trigger_name | ALL

For ex:
ALTER TABLE CR
DISABLE TRIGGER CR_TRIGG;
//This will disable the CR_TRIGG trigger until it is enable back.

ALTER TABLE CR
DISABLE TRIGGER ALL;
//This will disable all triggers associated with CR table until we enable it back.
//Please Note that disabling a trigger does not delete it!!!
ENABLE TRIGGER BACK
ALTER TRIGGER trigger_name ENABLE;

For ex:
ALTER TRIGGER CR_TRIGG ENABLE;
//This will enable the CR_TRIGG back
DROP TRIGGER
Syntax:
DROP trigger_name ON TABLE table_name;

For example:
DROP CR_TRIGG ON TABLE CR;
Thank you!!!

You might also like