0% found this document useful (0 votes)
5 views3 pages

DBMSlabtask 28-03-2025

The document outlines the implementation of a banking system using a database management system (DBMS) that includes tables for accounts and transactions. It features a trigger to prevent overdrafts and a cursor to process daily transactions. The SQL code provided creates the necessary tables, functions, and triggers to manage deposits and withdrawals while ensuring sufficient balance in accounts.

Uploaded by

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

DBMSlabtask 28-03-2025

The document outlines the implementation of a banking system using a database management system (DBMS) that includes tables for accounts and transactions. It features a trigger to prevent overdrafts and a cursor to process daily transactions. The SQL code provided creates the necessary tables, functions, and triggers to manage deposits and withdrawals while ensuring sufficient balance in accounts.

Uploaded by

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

DBMS (Lab Task)

Roll No: 423193


Implement a banking system where transactions (deposits/withdrawals) need to be
recorded. A Trigger will prevent overdrafts. A Cursor will process daily
transactions.
Accounts ( AccountID , HolderName, Balance)
Transactions (TransactionID , AccountID , Amount, Type)

create table Accounts(


AccountID serial primary key,
HolderName varchar(50),
Balance numeric(15,2) not null
);

create table Transactions(


TransactionID serial primary key,
AccountID int references Accounts(AccountID),
Amount numeric(15,2) not null,
Type varchar(10) check (Type in ('Withdraw','Deposit')),
Tdate timestamp default current_timestamp
);

create or replace function noOverdraft()


returns trigger
language plpgsql
as $$
begin
if new.Type='Withdraw' then
if (select Balance from Accounts where
AccountID=new.AccountID) < new.Amount then
raise exception 'Insufficient balance!';
end if;
update Accounts
set Balance=Balance-new.Amount
where AccountID=new.AccountID;
raise notice 'Amount withdrawn Rs. %',new.Amount;
else
update Accounts
set Balance=Balance+new.Amount
where AccountID=new.AccountID;
raise notice 'Amount deposited Rs. %',new.Amount;
end if;
return new;
end;
$$;

create trigger trigPreOverdraft


before insert
on Transactions
for each row
execute procedure noOverdraft();

do $$
declare rec_row record;
begin
for rec_row in
select * from Transactions
where Tdate::DATE=current_date
loop
raise notice 'Transaction being processed: TransactionID:
%, AccountID: %, Amount: %, Type: %,', rec_row.TransactionID,
rec_row.AccountID, rec_row.Amount, rec_row.Type;
end loop;
end $$;

insert into Accounts(HolderName, Balance)


values ('Tim',100000.00),
('Deigo',50000.50);

insert into Transactions(AccountID, Amount, Type) values


(1,1000.00,'Withdraw'),(2,1500.00,'Deposit');

Result:

You might also like