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
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