PostgreSQL - SPLessons

PostgreSQL Triggers

Chapter 25

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PostgreSQL Triggers

PostgreSQL Triggers

shape Description

A PostgreSQL Triggers is program, which can be executed automatically by the Oracle server when an event is raised. A PostgreSQL Triggers is executed before or after an insert, update or delete statement.

A PostgreSQL Triggers is fired, when a appropriate event occurs for the table. PostgreSQL Triggers are used to perform verification checks on data values for insertion and perform calculations.

The types of PostgreSQL Triggers are

Before Insert trigger

shape Description

PostgreSQL Before insert trigger will fire the trigger before performing insert records values inside the table.

shape Syntax


create trigger <trigger_name>
before insert
on <table_name> for each row

BEGIN

— variable declarations

— trigger code

END;

Trigger_name =>The name of the trigger to be create.

Table_name =>The accurate table in the database.

shape Conceptual
figure

shape Examples

By viewing the below example, the concept of before insert trigger can be easily understand.

SQLDB=# select * from employee01;

 emp_id | emp_name | city      | designation       | salary | perks 
--------+----------+-----------+-------------------+--------+-------
     1  | david    | delhi     | manager           |  12000 |  855 
     2  | shaha    | mumbai    | assistant manager |  13000 |  853 
     3  | sha      | puna      | scales manager    |  11000 |  850 
     4  | jack     | bangalore | designer          |  14000 |  854 
     5  | james    | mangalore | web designer      |  15000 | 1124 

(5 rows)

SQLDB=# delimiter $$
SQLDB=# CREATE TRIGGER inst_trigger BEFORE INSERT ON EmpLOYEE01
     -> FOR EACH ROW
     -> BEGIN
     -> UPDATE Employee01 SET Salary=Salary-400 where perks>500;
     -> end;
     -> $$
Query returned successfully with no result in 234 ms.

SQLDB=# delimiter ;
SQLDB=# insert into employee01 values(6,'mike','Chennai','developer',15500,840);
Query returned successfully with no result in 234 ms.

SQLDB=# select * from employee01;

 emp_id | emp_name | city      | designation       | salary | perks 
--------+----------+-----------+-------------------+--------+-------
    1   |  david   | delhi     | manager           |  11600 |  855 
    2   |  shaha   | mumbai    | assistant manager |  12600 |  853 
    3   |  shah    | puna      | scales manager    |  10600 |  850 
    4   |  jack    | bangalore | designer          |  13600 |  854 
    5   |  james   | mangalore | web designer      |  14600 | 1124 
    6   |  mike    | Chennai   | developer         |  15100 |  840 

(6 rows)

In the above example,before insert, trigger will fire the condition before inserting a record inside the table or on the table, and update command is performed on the column salary(Set Salary=Salary-400 where perks>500). Then the salary of all the employee changes as all the employee perks are greater then the applied condition.

Before update trigger

shape Description

PostgreSQL Before update trigger will fired the trigger before performing update records values inside the table.

shape Syntax

create trigger <trigger_name>
before update on <table_name> for each row.

BEGIN

— variable declarations

— trigger code

END;

Trigger_name =>The name of the trigger to be create.

Table_name =>The accurate table in the database.

shape Examples

By viewing the below example, the concept of before update trigger can be easily understand.

SQLDB=# select * from employee01;

 emp_id | emp_name | city      | designation       | salary | perks 
--------+----------+-----------+-------------------+--------+-------
    1   | david    | delhi     | manager           |  12000 |  855 
    2   | shaha    | mumbai    | assistant manager |  13000 |  853 
    3   | sha      | puna      | scales manager    |  11000 |  850 
    4   | jack     | bangalore | designer          |  14000 |  854 
    5   | james    | mangalore | web designer      |  15000 | 1124 
    6   | mike     | chennai   | develpoer         |  15500 |  840 
(6 rows)

SQLDB=# delimiter //
SQLDB=# CREATE TRIGGER updtrigger BEFORE UPDATE ON Employee01
    -> FOR EACH ROW
    -> BEGIN
    -> IF NEW.Salary=500 THEN -&gt; SET NEW.Salary=10000;
    -> ELSEIF NEW.Salary>500 THEN
    -> SET NEW.Salary=15000;
    -> END IF;
    -> END
    -> //
Query returned successfully with no result in 234 ms.
SQLDB=# delimiter ;
SQLDB=# UPDATE Employee01
    -> SET Salary=500;
Query returned successfully with no result in 234 ms.

SQLDB=# select * from employee01;

 emp_id | emp_name | city      | designation       | salary | perks 
--------+----------+-----------+-------------------+--------+-------
    1   | david    | delhi     | manager           |  10000 |   855 
    2   | shaha    | mumbai    | assistant manager |  10000 |   853 
    3   | sha      | puna      | scales manager    |  10000 |   850 
    4   | jack     | bangalore | designer          |  10000 |   854 
    5   | james    | mangalore | web designer      |  10000 |  1124 
    6   | mike     | Chennai   | developer         |  10500 |   840 

(6 rows)

In the above example, before update, trigger will fire the condition before updating a record inside the table or on the table, and update command is performed on the column salary(update if Salary=Salary>11000 and perks>500 then change to 10000). Then the salary of all the employee changes, as all the employee perks are greater then the applied condition.

After insert trigger

shape Description

PostgreSQL After Insert trigger will fire the trigger after performing insert records values inside the table.

shape Syntax


create trigger <trigger_name>
AFTER INSERT
ON <table_name> FOR EACH ROW

BEGIN

— variable declarations

— trigger code

END;
Trigger_name =>The name of the trigger to be create.

Table_name => The accurate table in the database.

shape Examples

By viewing the below example,the concept of after insert trigger can be easily understand.

SQLDB=# create table student_table(stu_id int,stu_name varchar(255),stu_class int
);
Query returned successfully with no result in 234 ms.

SQLDB=# create table student_log1(user_id varchar(255),description varchar(255));

Query returned successfully with no result in 234 ms.

SQLDB=# CREATE TRIGGER student_insert AFTER insert ON student_table FOR EACH ROW
BEGIN INSERT into student_log1(user_id,description) VALUES (user(),CONCAT('INSERT student records',new.stu_id,' ',new.stu_name,' ',new.stu_class)); 
END$$
Query returned successfully with no result in 234 ms.

SQLDB=# delimiter ;

SQLDB=# insert into student_table values(1,'mike',10);
Query returned successfully with no result in 234 ms.

SQLDB=# insert into student_table values(2,'mad',20);
Query returned successfully with no result in 234 ms.

SQLDB=# insert into student_table values(3,'mack',30);
Query returned successfully with no result in 234 ms.

SQLDB=# select * from student_log1;

  user_id       | description                     
----------------+---------------------------------
root@localhost  | INSERT student records1 mike 10 
root@localhost  | INSERT student records2 mad 20  
root@localhost  | INSERT student records3 mack 30 
(3 rows) 

In the above example, after insert, trigger will fire the condition after inserting a record inside the table or on the table i.e, after creating student_insert table, it will fire on the student_log1 table and all the values will display on student_log1 table which was inserted in student_table.

After update trigger

shape Description

PostgreSQL After update trigger will fire the trigger after performing insert records values inside the table.

shape Syntax

create trigger <trigger_name>
AFTER UPDATE
ON <table_name> FOR EACH ROW

BEGIN

— variable declarations

— trigger code

END;
Trigger_name =>The name of the trigger to be create.

Table_name => The accurate table in the database.

shape Examples

By viewing the below example,the concept of after update trigger can be easily understand.

SQLDB=# Create Table Student_Table02( Stu_Id int, Stu_Name varchar(255),Stu_Class int);

create table stu_log02( user_id VARCHAR(255), description VARCHAR(255));
delimiter $$
SQLDB=# CREATE TRIGGER stu_update
->AFTER UPDATE ON stu_table FOR EACH ROW
->BEGIN
	->INSERT into stu_log(user_id, description)
       -> VALUES (user(), CONCAT('Update Student Record 
        ->(',old.stu_id,' ',old.stu_name,' ',old.stu_class,
	') to (',new.stu_id,' ',new.stu_name,' ',new.stu_class,')'));
->END$$
SQLDB=# delimiter ;
SQLDB=# select * from student_table02;

 stu_id | stu_name | stu_class 
--------+----------+-----------
    1   | david    |    9 
    2   | shah     |    9 
    3   | mike     |    9 
    4   | james    |    9 

(4 rows)

SQLDB=# update student_table02 set stu_class=stu_class+1;
Query returned successfully with no result in 234 ms.

SQLDB=# select * from student_table02;

 stu_id | stu_name | stu_class 
--------+----------+-----------
    1   | david    |    10 
    2   | shah     |    10 
    3   | mike     |    10 
    4   | james    |    10 

(4 rows)

SQLDB=# select * from stu_log02;

 user_id        | description                                            
----------------+-----------------------------------------------------
 root@localhost | Update Student Record (1 david 9) to (1 david 10)      
 root@localhost | Update Student Record (2 shah  9) to (2 shah 10)       
 root@localhost | Update Student Record (3 mike  9) to (3 mike 10)       
 root@localhost | Update Student Record (4 james 9) to (4 james 10)      

In the above example, after update, trigger will fire the condition after update a record inside the table or on the table. i.e, after creating student_update table it will fire on the student_log2 table and all the values will display on student_log2 table which was inserted in student_table02.

Summary

shape Key Points

  • PostgreSQL Triggers – A trigger automatically executes when an event is raised.
  • Before Insert Trigger – Fire before inserting the values.
  • Before Insert Trigger – Fire before updating the values.
  • After Insert Trigger – Fire after inserting the values.
  • After Update Trigger – Fired updating the values.