SlideShare a Scribd company logo
TRIGGER
Trigger
Def :-
PostgreSQL Trigger is a function, which
invoked automatically when a specified
database event (insert, delete, and update)
occurs.
The triggers are used to supplement referential
integrity or check constraint or to enforce
complex business rules.
Steps to create a new Trigger:
Step 1: Create a trigger function using CREATE
FUNCTION statement.
Step 2: Then bind the trigger function to a
table by using CREATE
TRIGGER statement.
Creating a Function :
CREATE FUNCTION Trigger_Function()
RETURNS trigger AS
‘DECLARE
Variable Declarations;
[...] <function body>
[...]
RETURN
BEGIN
{variable name | value}
END;’
language ‘plpgsql’;
A trigger is created with CREATE FUNCTION command,
with no arguments and a return type as trigger.
A trigger that is marked FOR EACH ROW is called once
for every row that the operation modifies.
A trigger that is marked FOR EACH STATEMENT only
executes once for any given operation, regardless of
how many rows it modifies.
Creating a Trigger
CREATE TRIGGER trigger_name
{BEFORE/after}{insert/update/delete}ON[table_name]
FOR EACH {ROW/statement}
EXECUTE PROCEDURE function_name ();
• trigger_name: User defined name of the trigger.
• before/after: Determines whether the function is called before or after an
event.
• event_name: Database events can be Insert, Update and Delete.
• table_name: The name of the table the trigger is for.
• for each row /for each statement: specifies whether the trigger procedure
should be fired once for every row affected by the trigger event or just once
per SQL statement. If neither is specified, for each statement is the default.
• function_name: Name of the function to execute.
Reference Variables
Old/New:
Within the trigger both old and new values of the record
are used in the transactions.
Old:
It refers to the data existed within a table prior to the transaction.
Example: Delete operation
New: New values are the data values that the transaction creates.
Example: insert & update operation
Drop Trigger
DROP TRIGGER trigger_name on tablename;
Drop trigger t1 on employee;
Trigger Example
Consider the following database
Customer (cno integer, cname varchar(20), city varchar(20))
Account (a_no int, a_type varchar(10), opening_date date, balance money)
Customer and Account are related with one to many relationship
Write a trigger which is executed whenever insertion is made to
the account table. If the entered balance is less than 1000, print
an error message that balance cannot be less than 1000.
create or replace function ibal()
returns trigger as
'declare
begin
if new.bal<1000 then
raise exception ''Balance cannot b less than 1000'';
else if new.bal>=1000 then
raise notice ''Successful insertion'';
end if;
end if;
return new;
end;'
language 'plpgsql';
create trigger q0
before insert on account
for each row
execute procedure ibal();
Output:
insert into account values(9,'Current','1/1/2001',999,227);
ERROR: Balance cannot b less than 1000
insert into account values(9,'Current','1/1/2001',1000,227);
NOTICE: Successful Insertion
INSERT 0 1
Write a trigger which is executed whenever Updation in balance of
the account table is made. If the balance becomes less than 1000,
print an error message that balance cannot be less than 1000.
create or replace function ubal()
returns trigger as
'declare
begin
if new.bal<1000 then
raise exception ''Balance cannot b less than 1000'';
else if new.bal>=1000 then
raise notice ''Successful updation'';
end if;
end if;
return new;
end;'
language 'plpgsql';
create trigger q
before update on account
for each row
execute procedure ubal();
Output:
update account set bal=999 where ano=1;
ERROR: Balance cannot b less than 1000
update account set bal=40000 where ano=4;
NOTICE: Successful updation
UPDATE 1
Write a trigger which does not allow deletion of accounts
of Savings type.
create or replace function savr()
returns trigger as
'declare
begin
if old.atype=''Savings'' then
raise exception ''Savings AC cannot b deleted'';
end if;
return old;
end;'
language 'plpgsql';
create trigger d
before delete on account
for each row
execute procedure savr();
Output:
delete from account where atype='Savings';
ERROR: Savings AC cannot b deleted
Thank You

More Related Content

PPTX
Web Developer make the most out of your Database !
PPTX
Oracle trigger
PPTX
DBMS UNIT 9.pptx..................................
PPTX
PDF
Function Procedure Trigger Partition.pdf
PDF
PPTX
Procedures and triggers in SQL
PPTX
Triggers
Web Developer make the most out of your Database !
Oracle trigger
DBMS UNIT 9.pptx..................................
Function Procedure Trigger Partition.pdf
Procedures and triggers in SQL
Triggers

Similar to Trigger in Relational Database Management System (20)

PPTX
DOODB_LAB.pptx
PPTX
Unit 4
PDF
Below is my code- I have an error that I still have difficulty figurin.pdf
PPS
Sql xp 10
DOCX
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
PPT
Intro to trigger and constraint
PPT
Intro to tsql unit 15
PDF
Programming in Oracle with PL/SQL
PDF
The evolution of redux action creators
PPTX
11 - Trigger mysql advance materi for student.pptx
PDF
Triggers in PL introduction yo database s
PPT
Module06
PPTX
Procedure and Functions in pl/sql
PPTX
DBMS: Week 12 - Triggers in MySQL Database Server
DOCX
OOP program questions with answers
PPTX
Redux training
PDF
Lecture Notes Unit5 chapter16 Trigger Creation
DOCX
PROCEDURE FOR MONTH END DAY 1 INCLUDING SCREEN SHOT & SCRIPTS (Final rev 090211)
PDF
An Introduction To PostgreSQL Triggers
PDF
Assignment#08
DOODB_LAB.pptx
Unit 4
Below is my code- I have an error that I still have difficulty figurin.pdf
Sql xp 10
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
Intro to trigger and constraint
Intro to tsql unit 15
Programming in Oracle with PL/SQL
The evolution of redux action creators
11 - Trigger mysql advance materi for student.pptx
Triggers in PL introduction yo database s
Module06
Procedure and Functions in pl/sql
DBMS: Week 12 - Triggers in MySQL Database Server
OOP program questions with answers
Redux training
Lecture Notes Unit5 chapter16 Trigger Creation
PROCEDURE FOR MONTH END DAY 1 INCLUDING SCREEN SHOT & SCRIPTS (Final rev 090211)
An Introduction To PostgreSQL Triggers
Assignment#08
Ad

Recently uploaded (20)

PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
System and Network Administration Chapter 2
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
history of c programming in notes for students .pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
top salesforce developer skills in 2025.pdf
PPTX
ai tools demonstartion for schools and inter college
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Essential Infomation Tech presentation.pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Transform Your Business with a Software ERP System
PPTX
Introduction to Artificial Intelligence
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Odoo POS Development Services by CandidRoot Solutions
System and Network Administration Chapter 2
Upgrade and Innovation Strategies for SAP ERP Customers
How Creative Agencies Leverage Project Management Software.pdf
history of c programming in notes for students .pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Odoo Companies in India – Driving Business Transformation.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
top salesforce developer skills in 2025.pdf
ai tools demonstartion for schools and inter college
Reimagine Home Health with the Power of Agentic AI​
Essential Infomation Tech presentation.pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
Transform Your Business with a Software ERP System
Introduction to Artificial Intelligence
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Wondershare Filmora 15 Crack With Activation Key [2025
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Ad

Trigger in Relational Database Management System

  • 2. Trigger Def :- PostgreSQL Trigger is a function, which invoked automatically when a specified database event (insert, delete, and update) occurs. The triggers are used to supplement referential integrity or check constraint or to enforce complex business rules.
  • 3. Steps to create a new Trigger: Step 1: Create a trigger function using CREATE FUNCTION statement. Step 2: Then bind the trigger function to a table by using CREATE TRIGGER statement.
  • 4. Creating a Function : CREATE FUNCTION Trigger_Function() RETURNS trigger AS ‘DECLARE Variable Declarations; [...] <function body> [...] RETURN BEGIN {variable name | value} END;’ language ‘plpgsql’;
  • 5. A trigger is created with CREATE FUNCTION command, with no arguments and a return type as trigger. A trigger that is marked FOR EACH ROW is called once for every row that the operation modifies. A trigger that is marked FOR EACH STATEMENT only executes once for any given operation, regardless of how many rows it modifies.
  • 6. Creating a Trigger CREATE TRIGGER trigger_name {BEFORE/after}{insert/update/delete}ON[table_name] FOR EACH {ROW/statement} EXECUTE PROCEDURE function_name ();
  • 7. • trigger_name: User defined name of the trigger. • before/after: Determines whether the function is called before or after an event. • event_name: Database events can be Insert, Update and Delete. • table_name: The name of the table the trigger is for. • for each row /for each statement: specifies whether the trigger procedure should be fired once for every row affected by the trigger event or just once per SQL statement. If neither is specified, for each statement is the default. • function_name: Name of the function to execute.
  • 8. Reference Variables Old/New: Within the trigger both old and new values of the record are used in the transactions. Old: It refers to the data existed within a table prior to the transaction. Example: Delete operation New: New values are the data values that the transaction creates. Example: insert & update operation
  • 9. Drop Trigger DROP TRIGGER trigger_name on tablename; Drop trigger t1 on employee;
  • 10. Trigger Example Consider the following database Customer (cno integer, cname varchar(20), city varchar(20)) Account (a_no int, a_type varchar(10), opening_date date, balance money) Customer and Account are related with one to many relationship
  • 11. Write a trigger which is executed whenever insertion is made to the account table. If the entered balance is less than 1000, print an error message that balance cannot be less than 1000. create or replace function ibal() returns trigger as 'declare begin if new.bal<1000 then raise exception ''Balance cannot b less than 1000''; else if new.bal>=1000 then raise notice ''Successful insertion''; end if; end if; return new; end;' language 'plpgsql'; create trigger q0 before insert on account for each row execute procedure ibal(); Output: insert into account values(9,'Current','1/1/2001',999,227); ERROR: Balance cannot b less than 1000 insert into account values(9,'Current','1/1/2001',1000,227); NOTICE: Successful Insertion INSERT 0 1
  • 12. Write a trigger which is executed whenever Updation in balance of the account table is made. If the balance becomes less than 1000, print an error message that balance cannot be less than 1000. create or replace function ubal() returns trigger as 'declare begin if new.bal<1000 then raise exception ''Balance cannot b less than 1000''; else if new.bal>=1000 then raise notice ''Successful updation''; end if; end if; return new; end;' language 'plpgsql'; create trigger q before update on account for each row execute procedure ubal(); Output: update account set bal=999 where ano=1; ERROR: Balance cannot b less than 1000 update account set bal=40000 where ano=4; NOTICE: Successful updation UPDATE 1
  • 13. Write a trigger which does not allow deletion of accounts of Savings type. create or replace function savr() returns trigger as 'declare begin if old.atype=''Savings'' then raise exception ''Savings AC cannot b deleted''; end if; return old; end;' language 'plpgsql'; create trigger d before delete on account for each row execute procedure savr(); Output: delete from account where atype='Savings'; ERROR: Savings AC cannot b deleted