423193_LABTASK10_4_April_2025
423193_LABTASK10_4_April_2025
1. bank database stores customer transactions. If any transaction fails, the system should
rollback to a safe point instead of undoing everything.
BankAccounts (AccountID, Name, Balance)
i) Create a table BankAccounts
ii) Perform multiple transactions with SAVEPOINTS:
iii) What happens if COMMIT is executed before ROLLBACK?
iv) Can you create nested SAVEPOINTS?
v) What would happen if we removed SAVEPOINT sp1 before rollback?
-- BEGIN;
-- SAVEPOINT sp1;
-- -- operation A
-- SAVEPOINT sp2;
-- -- operation B
-- ROLLBACK TO sp2; -- Undo only B
-- ROLLBACK TO sp1; -- Undo A and B
-- COMMIT;
--1.
--i)
create table BankAccounts(
AccountID serial primary key,
Name varchar(100) not null,
Balance decimal(10,2) check (Balance >= 0) not null
);
--ii)
begin;
insert into BankAccounts(Name, Balance) values ('Neo', 50000.00);
savepoint p1;
update BankAccounts
set Balance = Balance + 100
where Name = 'Neo';
--iv)
savepoint p2; -- inside savepoint p1 (nested savepoint)
update BankAccounts
set Balance = Balance - 200000
where Name = 'Neo';
update BankAccounts
set Balance = Balance - 1000
where Name = 'Neo';
--iii)
rollback to savepoint p1;
--not reached as commit is done
--rollback; --not reached as commit is done
--v)
insert into BankAccounts(Name, Balance)
values ('Tianne', 35000.50);
begin;
savepoint s4;
insert into Transactions(AccountID, Amount, TransactionType)
values (2, 1000, 'Deposit');
commit;
2. A company database has Admin, Manager, and Employee roles. Grant access based on job roles.
Users (UserID , Name, Role). Apply Role-Based Access Control (RBAC)
i) Can a revoked user still access old data?
ii) What happens if a user with GRANT OPTION assigns a privilege?
iii) How can we prevent users from granting privileges to themselves?
--2.
--i)
revoke employee from charlie;
-- Now charlie cannot SELECT from Reports
--ii)
create user dave with password 'pass4';
-- Grant manager with grant option to bob
grant select on Reports to ben with grant option;
--iii)
--just don't grant them with the grant option (as we granted for ben in ii)
giving him grant permission don't do that) to prevent users from granting
privileges to themselves