Bankingdatabase
Bankingdatabase
an account can be a savings account or a current account check ATYPE is 'S' or 'C'
.a customer can have both types of accounts
a)develope DDL to implement the above schema specifying appropriate data tpe for
each attribute enforcing primary key(specified),foregin key, check constraint
c)develope a SQL query to list the details of customers who have a savings account
and a current account
d)develope a SQQL query to list the details of branches and the number of accounts
in each branch
e)develope a SQL query to list the details of branches where the number of accounts
is less than the averge number of accounts in all branches
f)delvelope a SQL query to list the details of customers who have performed three
transactions on a day
g)create a view that will keep track of branch details and the number of account in
each branch
h) develope a database trigger that will update the value of balance in account
table when a record is inserted in the transaction table consider the following
cases
i)if TTYPE ='D' the value of BALANCE in the ACCOUNT table must be increment
by the velue of TAMOUNT
ii) if TTYPE='W' the velue of BALANCE in the ACCOUNT table must be
decremented by the value of TAMOUNT if a minimum balance of Rs.2000 will be
maintained for a savongs account and a minimum balance of Rs.5000 will be
maintained for a current account else appropriate messages may be displayed
EXTRA QUERIES
I) develope a SQL query to list the details of customers who have balance less than
the average balance of all customers
j)develope a sql query to list the details of customers with the sum of balance in
their account(s).
k) create a view that will keep track of customer details and the number of account
each customer has.
l)develope a sql query to list the details of branches and number of accounts in
each branch
m)develope a sql query to list the details of branches where the number of accounts
is less than average number of accounts in all branches
n)create a view that will keep track of branch details and the number of account in
each branch
POSTGRES
c) List the details of customers who have a savings account and a current account:
d) List the details of branches and the number of accounts in each branch:
e) List the details of branches where the number of accounts is less than the
average number of accounts in all branches:
WITH BranchAccountCount AS (
SELECT B.BCODE, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE
)
f) List the details of customers who have performed three transactions on a day:
g) Create a view that will keep track of branch details and the number of accounts
in each branch:
h) Create a database trigger that will update the value of the balance in the
account table:
CREATE OR REPLACE FUNCTION update_balance()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.TTYPE = 'D' THEN
UPDATE ACCOUNT SET BALANCE = BALANCE + NEW.TAMOUNT WHERE ANO = NEW.ANO;
ELSIF NEW.TTYPE = 'W' THEN
UPDATE ACCOUNT
SET BALANCE = CASE WHEN ATYPE = 'S' AND BALANCE - NEW.TAMOUNT >= 2000 THEN
BALANCE - NEW.TAMOUNT
WHEN ATYPE = 'C' AND BALANCE - NEW.TAMOUNT >= 5000 THEN
BALANCE - NEW.TAMOUNT
ELSE RAISE EXCEPTION 'Insufficient balance';
END
WHERE ANO = NEW.ANO;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
M)WITH BranchAccountCount AS (
SELECT B.BCODE, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE
)
mongodb
// a) List the details of customers who have balance less than the average balance
of all customers
db.ACCOUNT.aggregate([
{
$lookup: {
from: "CUSTOMER",
localField: "CID",
foreignField: "CNO",
as: "customer"
}
},
{
$unwind: "$customer"
},
{
$match: {
BALANCE: { $lt: db.ACCOUNT.aggregate([{ $group: { _id: null, avg_balance:
{ $avg: "$BALANCE" } }]).toArray()[0].avg_balance }
}
},
{
$project: {
"customer.CNO": 1,
"customer.CNAME": 1,
BALANCE: 1
}
}
]);
// b) List the details of customers with the sum of the balance in their account(s)
db.ACCOUNT.aggregate([
{
$lookup: {
from: "CUSTOMER",
localField: "CID",
foreignField: "CNO",
as: "customer"
}
},
{
$unwind: "$customer"
},
{
$group: {
_id: "$customer.CNO",
CNAME: { $first: "$customer.CNAME" },
TOTAL_BALANCE: { $sum: "$BALANCE" }
}
},
{
$project: {
CNO: "$_id",
CNAME: 1,
TOTAL_BALANCE: 1,
_id: 0
}
}
]);
// c) Create a view that will keep track of customer details and the number of
accounts each customer has
// MongoDB doesn't support views, you can create a collection instead
db.CUSTOMER_ACCOUNT_VIEW.insertMany(
db.CUSTOMER.aggregate([
{
$lookup: {
from: "ACCOUNT",
localField: "CNO",
foreignField: "CID",
as: "accounts"
}
},
{
$project: {
CNO: 1,
CNAME: 1,
ACCOUNT_COUNT: { $size: "$accounts" },
_id: 0
}
}
]).toArray()
);
// d) List the details of branches and the number of accounts in each branch
db.BRANCH.aggregate([
{
$lookup: {
from: "ACCOUNT",
localField: "BCODE",
foreignField: "BCODE",
as: "accounts"
}
},
{
$project: {
BCODE: 1,
BNAME: 1,
ACCOUNT_COUNT: { $size: "$accounts" },
_id: 0
}
}
]);
// e) List the details of branches where the number of accounts is less than the
average number of accounts in all branches
const averageAccountCount = db.BRANCH.aggregate([
{
$lookup: {
from: "ACCOUNT",
localField: "BCODE",
foreignField: "BCODE",
as: "accounts"
}
},
{
$project: {
ACCOUNT_COUNT: { $size: "$accounts" }
}
},
{
$group: {
_id: null,
avgAccountCount: { $avg: "$ACCOUNT_COUNT" }
}
}
]).toArray()[0].avgAccountCount;
db.BRANCH.aggregate([
{
$lookup: {
from: "ACCOUNT",
localField: "BCODE",
foreignField: "BCODE",
as: "accounts"
}
},
{
$project: {
BCODE: 1,
BNAME: 1,
ACCOUNT_COUNT: { $size: "$accounts" }
}
},
{
$match: {
ACCOUNT_COUNT: { $lt: averageAccountCount }
}
}
]);
MYSQL
C)SELECT C.CNO, C.CNAME
FROM CUSTOMER C
JOIN ACCOUNT A1 ON C.CNO = A1.CID AND A1.ATYPE = 'S'
JOIN ACCOUNT A2 ON C.CNO = A2.CID AND A2.ATYPE = 'C';
E)WITH BranchAccountCount AS (
SELECT B.BCODE, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE
)
H)DELIMITER //
CREATE TRIGGER update_balance_trigger
AFTER INSERT ON TRANSACTION
FOR EACH ROW
BEGIN
IF NEW.TTYPE = 'D' THEN
UPDATE ACCOUNT SET BALANCE = BALANCE + NEW.TAMOUNT WHERE ANO = NEW.ANO;
ELSEIF NEW.TTYPE = 'W' THEN
UPDATE ACCOUNT
SET BALANCE = CASE WHEN ATYPE = 'S' AND BALANCE - NEW.TAMOUNT >= 2000 THEN
BALANCE - NEW.TAMOUNT
WHEN ATYPE = 'C' AND BALANCE - NEW.TAMOUNT >= 5000 THEN
BALANCE - NEW.TAMOUNT
ELSE SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT =
'Insufficient balance';
END
WHERE ANO = NEW.ANO;
END IF;
END;
//
DELIMITER ;
M)WITH BranchAccountCount AS (
SELECT B.BCODE, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE
)
e)WITH BranchAccountCount AS (
SELECT B.BCODE, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE
)
M)WITH BranchAccountCount AS (
SELECT B.BCODE, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE
)