0% found this document useful (0 votes)
41 views13 pages

Bankingdatabase

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)
41 views13 pages

Bankingdatabase

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/ 13

consider the following relational schema:

CUSTOMER(CNO PRIMARY KEY,CNAME)


BRANCH(BCODE PRIMARY KEY,BNAME)
ACCOUNT(ANO PRIMARY KEY,ATYPE ,BALANCE, CID,BCODE)

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

TRANSACTION(TID PRIMARY KEY,ANO,TTYPE,TDATE,TAMOUNT)

TTYPE can be 'D' or 'W'

a)develope DDL to implement the above schema specifying appropriate data tpe for
each attribute enforcing primary key(specified),foregin key, check constraint

b)populate the databse with rich dataset

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

a) DDL to implement the relational schema:

CREATE TABLE CUSTOMER (


CNO INT PRIMARY KEY,
CNAME VARCHAR(255)
);

CREATE TABLE BRANCH (


BCODE INT PRIMARY KEY,
BNAME VARCHAR(255)
);

CREATE TABLE ACCOUNT (


ANO INT PRIMARY KEY,
ATYPE CHAR(1) CHECK (ATYPE IN ('S', 'C')),
BALANCE DECIMAL(10, 2),
CID INT,
BCODE INT,
FOREIGN KEY (CID) REFERENCES CUSTOMER(CNO),
FOREIGN KEY (BCODE) REFERENCES BRANCH(BCODE)
);

CREATE TABLE TRANSACTION (


TID INT PRIMARY KEY,
ANO INT,
TTYPE CHAR(1) CHECK (TTYPE IN ('D', 'W')),
TDATE DATE,
TAMOUNT DECIMAL(10, 2),
FOREIGN KEY (ANO) REFERENCES ACCOUNT(ANO)
);

b) Population of the database with a rich dataset:

-- Insert data into CUSTOMER table


INSERT INTO CUSTOMER VALUES (1, 'Customer1');
INSERT INTO CUSTOMER VALUES (2, 'Customer2');

-- Insert data into BRANCH table


INSERT INTO BRANCH VALUES (101, 'Branch1');
INSERT INTO BRANCH VALUES (102, 'Branch2');

-- Insert data into ACCOUNT table


INSERT INTO ACCOUNT VALUES (1001, 'S', 5000.00, 1, 101);
INSERT INTO ACCOUNT VALUES (1002, 'C', 10000.00, 1, 101);
INSERT INTO ACCOUNT VALUES (1003, 'S', 3000.00, 2, 102);

-- Insert data into TRANSACTION table


INSERT INTO TRANSACTION VALUES (2002, 1002, 'W', '2024-01-14', 500.00);
INSERT INTO TRANSACTION VALUES (2003, 1003, 'D', '2024-01-13', 200.00);
INSERT INTO TRANSACTION VALUES (2004, 1003, 'W', '2024-01-14', 100.00);

c) List the details of customers who have a savings account and a current account:

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';

d) List the details of branches and the number of accounts in each branch:

SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT


FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;

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
)

SELECT B.BCODE, B.BNAME, BAC.ACCOUNT_COUNT


FROM BRANCH B
JOIN BranchAccountCount BAC ON B.BCODE = BAC.BCODE
WHERE BAC.ACCOUNT_COUNT < (SELECT AVG(ACCOUNT_COUNT) FROM BranchAccountCount);

f) List the details of customers who have performed three transactions on a day:

SELECT C.CNO, C.CNAME


FROM CUSTOMER C
JOIN ACCOUNT A ON C.CNO = A.CID
JOIN TRANSACTION T ON A.ANO = T.ANO
WHERE TDATE = '2024-01-13'
GROUP BY C.CNO, C.CNAME
HAVING COUNT(T.TID) = 3;

g) Create a view that will keep track of branch details and the number of accounts
in each branch:

CREATE VIEW BranchAccountView AS


SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;

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;

CREATE TRIGGER update_balance_trigger


AFTER INSERT ON TRANSACTION
FOR EACH ROW EXECUTE FUNCTION update_balance();

i)SELECT C.CNO, C.CNAME, A.BALANCE


FROM CUSTOMER C
JOIN ACCOUNT A ON C.CNO = A.CID
WHERE A.BALANCE < (SELECT AVG(BALANCE) FROM ACCOUNT);

J)SELECT C.CNO, C.CNAME, SUM(A.BALANCE) AS TOTAL_BALANCE


FROM CUSTOMER C
JOIN ACCOUNT A ON C.CNO = A.CID
GROUP BY C.CNO, C.CNAME;

K)CREATE VIEW CustomerAccountView AS


SELECT C.CNO, C.CNAME, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM CUSTOMER C
LEFT JOIN ACCOUNT A ON C.CNO = A.CID
GROUP BY C.CNO, C.CNAME;

L)SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT


FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;

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
)

SELECT B.BCODE, B.BNAME, BAC.ACCOUNT_COUNT


FROM BRANCH B
JOIN BranchAccountCount BAC ON B.BCODE = BAC.BCODE
WHERE BAC.ACCOUNT_COUNT < (SELECT AVG(ACCOUNT_COUNT) FROM BranchAccountCount);

N)CREATE VIEW BranchAccountView AS


SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;

mongodb

// Use or create a database


use your_database_name;

// Create or use collections (similar to tables)


db.createCollection("CUSTOMER");
db.createCollection("BRANCH");
db.createCollection("ACCOUNT");
db.createCollection("TRANSACTION");

// Insert data into CUSTOMER collection


db.CUSTOMER.insertMany([
{ CNO: 1, CNAME: "Customer1" },
{ CNO: 2, CNAME: "Customer2" }
]);

// Insert data into BRANCH collection


db.BRANCH.insertMany([
{ BCODE: 101, BNAME: "Branch1" },
{ BCODE: 102, BNAME: "Branch2" }
]);

// Insert data into ACCOUNT collection


db.ACCOUNT.insertMany([
{ ANO: 1001, ATYPE: "S", BALANCE: 5000.00, CID: 1, BCODE: 101 },
{ ANO: 1002, ATYPE: "C", BALANCE: 10000.00, CID: 1, BCODE: 101 },
{ ANO: 1003, ATYPE: "S", BALANCE: 3000.00, CID: 2, BCODE: 102 }
]);

// Insert data into TRANSACTION collection


db.TRANSACTION.insertMany([
{ TID: 2001, ANO: 1001, TTYPE: "D", TDATE: ISODate("2024-01-13"), TAMOUNT:
1000.00 },
{ TID: 2002, ANO: 1002, TTYPE: "W", TDATE: ISODate("2024-01-14"), TAMOUNT: 500.00
},
{ TID: 2003, ANO: 1003, TTYPE: "D", TDATE: ISODate("2024-01-13"), TAMOUNT: 200.00
},
{ TID: 2004, ANO: 1003, TTYPE: "W", TDATE: ISODate("2024-01-14"), TAMOUNT: 100.00
}
]);
// Additional MongoDB setup may be needed based on your specific use case and
requirements.

// 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';

D)SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT


FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;

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
)

SELECT B.BCODE, B.BNAME, BAC.ACCOUNT_COUNT


FROM BRANCH B
JOIN BranchAccountCount BAC ON B.BCODE = BAC.BCODE
WHERE BAC.ACCOUNT_COUNT < (SELECT AVG(ACCOUNT_COUNT) FROM BranchAccountCount);

F)SELECT C.CNO, C.CNAME


FROM CUSTOMER C
JOIN ACCOUNT A ON C.CNO = A.CID
JOIN TRANSACTION T ON A.ANO = T.ANO
WHERE TDATE = '2024-01-13'
GROUP BY C.CNO, C.CNAME
HAVING COUNT(T.TID) = 3;

G)CREATE VIEW BranchAccountView AS


SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;

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 ;

I)SELECT C.CNO, C.CNAME, A.BALANCE


FROM CUSTOMER C
JOIN ACCOUNT A ON C.CNO = A.CID
WHERE A.BALANCE < (SELECT AVG(BALANCE) FROM ACCOUNT);

J)SELECT C.CNO, C.CNAME, SUM(A.BALANCE) AS TOTAL_BALANCE


FROM CUSTOMER C
JOIN ACCOUNT A ON C.CNO = A.CID
GROUP BY C.CNO, C.CNAME;

K)CREATE VIEW CustomerAccountView AS


SELECT C.CNO, C.CNAME, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM CUSTOMER C
LEFT JOIN ACCOUNT A ON C.CNO = A.CID
GROUP BY C.CNO, C.CNAME;

L)SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT


FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;

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
)

SELECT B.BCODE, B.BNAME, BAC.ACCOUNT_COUNT


FROM BRANCH B
JOIN BranchAccountCount BAC ON B.BCODE = BAC.BCODE
WHERE BAC.ACCOUNT_COUNT < (SELECT AVG(ACCOUNT_COUNT) FROM BranchAccountCount);

N)CREATE VIEW BranchAccountView AS


SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;
ORACLE

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';

D)SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT


FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;

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
)

SELECT B.BCODE, B.BNAME, BAC.ACCOUNT_COUNT


FROM BRANCH B
JOIN BranchAccountCount BAC ON B.BCODE = BAC.BCODE
WHERE BAC.ACCOUNT_COUNT < (SELECT AVG(ACCOUNT_COUNT) FROM BranchAccountCount);

f)SELECT C.CNO, C.CNAME


FROM CUSTOMER C
JOIN ACCOUNT A ON C.CNO = A.CID
JOIN TRANSACTION T ON A.ANO = T.ANO
WHERE T.TDATE = TO_DATE('2024-01-13', 'YYYY-MM-DD')
GROUP BY C.CNO, C.CNAME
HAVING COUNT(T.TID) = 3;

g)CREATE VIEW BranchAccountView AS


SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;

h)CREATE OR REPLACE 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;
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_APPLICATION_ERROR(-20001, 'Insufficient
balance');
END
WHERE ANO = :NEW.ANO;
END IF;
END;
/

I)SELECT C.CNO, C.CNAME, A.BALANCE


FROM CUSTOMER C
JOIN ACCOUNT A ON C.CNO = A.CID
WHERE A.BALANCE < (SELECT AVG(BALANCE) FROM ACCOUNT);

J)SELECT C.CNO, C.CNAME, SUM(A.BALANCE) AS TOTAL_BALANCE


FROM CUSTOMER C
JOIN ACCOUNT A ON C.CNO = A.CID
GROUP BY C.CNO, C.CNAME;

K)CREATE VIEW CustomerAccountView AS


SELECT C.CNO, C.CNAME, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM CUSTOMER C
LEFT JOIN ACCOUNT A ON C.CNO = A.CID
GROUP BY C.CNO, C.CNAME;

L)SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT


FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;

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
)

SELECT B.BCODE, B.BNAME, BAC.ACCOUNT_COUNT


FROM BRANCH B
JOIN BranchAccountCount BAC ON B.BCODE = BAC.BCODE
WHERE BAC.ACCOUNT_COUNT < (SELECT AVG(ACCOUNT_COUNT) FROM BranchAccountCount);

N)CREATE VIEW BranchAccountView AS


SELECT B.BCODE, B.BNAME, COUNT(A.ANO) AS ACCOUNT_COUNT
FROM BRANCH B
LEFT JOIN ACCOUNT A ON B.BCODE = A.BCODE
GROUP BY B.BCODE, B.BNAME;

You might also like