Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

1)

create view customerDetails as


select top 3 c.name_ as 'Customer name', b.name_ as 'Branch Name',
b.address_ as 'Branch Address', a.balance as 'Account balance'
from Belongs_to bt, Customer c, Account a, branch b
where bt.cno = c.number and bt.accno = a.accno and a.branchNo = b.branchNo
and a.code = b.code
order by a.balance desc

2)
create view depositDetails as
select count(h.accno) 'No of accounts'
from Has h, Account a
where h.accno = a.accno and h.type_ = 'credit'
group by a.code

3)
create function fn_totalAccBalance(@branchNo int,@bankCode int ) returns real
as
begin
declare @tot real

select @tot = sum(balance)


from Account
where branchNo = @branchNo and code = @bankCode

return @tot
end
4)
create function fn_totalWithdrawals(@year date, @accNo int, @method varchar(10))
returns real
as
begin
declare @tId int
declare @tot real
select @tId = tid from Has where accno = @accNo

select sum(amount)
from Transaction_
where date_ = @year and exec_by = @method and tid = @tid
return @tot
end

5)
create procedure updateAccount(@accNo int, @amount real, @operation varchar(10))
as
begin
if (@operation = 'credit')
begin
update Account
set balance = balance + @amount
where accno = @accNo
end
else
begin
update Account
set balance = balance - @amount
where accno = @accNo
end
end
6)
create procedure transferMoney(@transferAmount real,@transferFrom int,
@transferTo int)
as
begin
update Account
set balance = balance - @transferAmount
where accno = @transferFrom

update Account
set balance = balance + @transferAmount
where accno = @transferTo
end

7)
create trigger checkRemainBalance
on Account
for insert, update
as
begin
declare @bal real

select @bal = balance from inserted

if (@bal < 500)


begin
print 'Cannot withdraw money'
rollback transaction
end
end
8)
create trigger ATMwithdrawal
on Transaction
for insert
as
begin
declare @withdrawAmount real
declare @type varchar(20)

select @withdrawAmount = amount, @type = exec_by from inserted

if(@withdrawAmount > 80000 and @type = 'ATM')


begin
print 'Cannot withdraw more than 80000'
rollback transaction
end

9)
create trigger upadateBalance
on Has
for insert
as
begin
declare @type varchar(20)
declare @accNo int
declare @amount real
declare @tId int
select @type = type_, @accNo = accountNO, @tId = tid from
inserted

select @amount = amount from Transaction_ where tid = @tId

if (@type = 'credit')
begin
update Account
set balance = balance + @amount
where accno = @accNo
end
else
begin
update Account
set balance = balance - @amount
where accno = @accNo
end
end

You might also like