0% found this document useful (0 votes)
1 views1 page

Ex No 7

The document outlines the creation of a 'transaction' table and a trigger function in PostgreSQL to log account transactions. It includes the definition of the function that calculates the transaction amount and determines if the operation is a deposit or withdrawal. An example insert operation is also provided, showing a successful transaction record in the table.

Uploaded by

takash2412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views1 page

Ex No 7

The document outlines the creation of a 'transaction' table and a trigger function in PostgreSQL to log account transactions. It includes the definition of the function that calculates the transaction amount and determines if the operation is a deposit or withdrawal. An example insert operation is also provided, showing a successful transaction record in the table.

Uploaded by

takash2412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

EX.

NO:7
ai258113=# create table transaction(ano int,transaction_date timestamp,operation
varchar(10),amt int);
CREATE TABLE
ai258113=# create or replace function transactions()
ai258113-# returns trigger
ai258113-# language plpgsql as $$
ai258113$# declare
ai258113$# transaction_amt numeric(10,2);
ai258113$# operation varchar(1);
ai258113$# begin
ai258113$# raise info 'informationmessage %',new.balance ; raise info
ai258113$# 'informationmessage %',old.balance;
ai258113$# transaction_amt=new.balance-old.balance;
ai258113$# if(transaction_amt<0) then
ai258113$# operation='w';
ai258113$# else
ai258113$# operation='d';
ai258113$# end if;
ai258113$# insert into transaction values(old.ano,current_timestamp,operation,
transaction_amt);
ai258113$# return new;
ai258113$# end$$;
CREATE FUNCTION
ai258113=# create or replace trigger transact before update on account
ai258113-# for each row execute procedure transactions();
CREATE TRIGGER
ai258113=#insert into transaction values(5,'2023-12-12
06:54:29.261514+00','d',100);
INSERT 0 1
ai258113=# select * from transaction;
ano | transaction_date | operation | amt
-----+----------------------------+-----------+-----
5 | 2023-12-12 06:54:29.261514 | d | 100
(1 row)

You might also like