0% found this document useful (0 votes)
29 views5 pages

Banking Database

The document outlines the creation of a banking database with tables for branches, accounts, customers, loans, depositors, and borrowers, including primary and foreign key specifications. It provides SQL commands to insert data into these tables and execute queries to find customers with multiple accounts at a specific branch and those with accounts at all branches in a city. Additionally, it demonstrates how to delete account tuples for branches located in a specific city.

Uploaded by

Putta Swamy
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)
29 views5 pages

Banking Database

The document outlines the creation of a banking database with tables for branches, accounts, customers, loans, depositors, and borrowers, including primary and foreign key specifications. It provides SQL commands to insert data into these tables and execute queries to find customers with multiple accounts at a specific branch and those with accounts at all branches in a city. Additionally, it demonstrates how to delete account tuples for branches located in a specific city.

Uploaded by

Putta Swamy
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/ 5

Create a Banking database and execute the queries for the given

requirements.
BRANCH (BRANCH_NAME: STRING, BRANCH-CITY: STRING, ASSETS: REAL)
ACCOUNT (ACCNO: INT, BRANCH-NAME: STRING, BALANCE: REAL)
DEPOSITOR (CUSTOMER-NAME: STRING, ACCNO: INT)
CUSTOMER (CUSTOMER-NAME: STRING, CUSTOMER-STREET: STRING, CITY:
STRING)
LOAN (LOAN-NO: INT, BRANCH-NAME: STRING, AMOUNT REAL)
BORROWER (CUSTOMER-NAME: STRING, LOAN-NO: INT)
(i) Create the above tables by properly specifying the primary keys and the foreign keys
(ii) Enter at least five tuples for each relation
(iii) Find all the customers who have at least two accounts at the Main branch.
(iv) Find all the customers who have an account at all the branches located in a specific city.
(v) Demonstrate how you delete all account tuples at every branch located in a specific city.
CREATE TABLE branch
(branch_name VARCHAR (15),
branch_city VARCHAR (15),
assets FLOAT(10,2),
PRIMARY KEY (branch_name) );

CREATE TABLE account


(accno INTEGER (8),
branch_name VARCHAR (15),
balance FLOAT (10,2),
PRIMARY KEY (accno),
FOREIGN KEY (branch_name) REFERENCES branch(branch_name) ON DELETE
CASCADE
);
CREATE TABLE customer
(customer_name VARCHAR (15),
customer_street VARCHAR (15),
customer_city VARCHAR (15),
PRIMARY KEY (customer_name)
);

CREATE TABLE loan


( loan number INTEGER(8),
branc_hname VARCHAR (15),
amount FLOAT (10,2),
PRIMARY KEY (loan_number),
FOREIGN KEY (branch_name) REFERENCES branch(branch_name) on delete cascade );
CREATE TABLE depositor
(customer_name VARCHAR (15),
accno INT(10),
FOREIGN KEY (customer_name) REFERENCES customer(customer_name),
FOREIGN KEY (accno) REFERENCES account(accno)
);

CREATE TABLE borrower


(customer_name VARCHAR (15),
loan_number INT(8),
FOREIGN KEY (customer_name) REFERENCES customer(customer_name),
FOREIGN KEY (loan_number) REFERENCES loan(loan_number)
);
insert into branch (branch_name, branch_city, assets) values ("b1","c1",10000),
("b2","c2",20000), ("b3","c3",30000), ("b4","c4",40000), ("b5","c5",50000);
INSERT INTO account (accno,branch_name,balance) VALUES (12,"b1",3000),
-> (22,"b2",4000), (32,"b3",5000), (42,"b4",6000), (52,"b5",7000);
INSERT INTO customer (customer_name,customer_street,customer_city) VALUES
("cust1","cstreet1","ccity1"), ("cust2","cstreet2","ccity2"),
("cust3","cstreet3","ccity3"), ("cust4","cstreet4","ccity4"),
("cust5","cstreet5","ccity5");
INSERT INTO depositor (customer_name,accno) VALUES ("cust1",12), ("cust2",22),
("cust3",32), ("cust4",42), ("cust5",52);
INSERT INTO loan (loan_number,branch_name,amount) VALUES (10,"b1",10000),
(20,"b2",20000), (30,"b3",30000), (40,"b4",40000), (50,"b5",50000);
INSERT INTO borrower (customer_name,loan_number) VALUES ("cust1",10), ("cust2",20),
("cust3",30), ("cust4",40), ("cust5",50);
QUERIES:
iii. Find all the customers who have at least two accounts at the Main branch.
mysql> SELECT customer_name
FROM depositor d, account a
WHERE d.accno=a.accno
AND a.branch_name='B1'
GROUP BY d.customer_name
HAVING COUNT(d.customer_name)>=1;

Description: The query is selecting the customer's name such that the account number associated
with name is in both the account table and depositor table and also the name of the branch in the
account table is B1 and then the tuples are being grouped by customer name in the depositor
table and also the customer name having count atleast equal to 2 are being selected.
iv. Find all the customers who have an account at all the branches located in a specific city.
mysql> SELECT d.customer_name
FROM account a,branch b,depositor d
WHERE b.branch_name=a.branch_name AND
a.accno=d.accno AND
b.branch_city='c3'
GROUP BY d.customer_name
HAVING COUNT(distinct b.branch_name)=(
SELECT COUNT(branch_name)
FROM branch
WHERE branch_city='c3');

Description: The query selects the customers from the depositor table such that branch name is in
both the branch table and also account table and the account number in the selected tuples is in
both account table and in depositor table and also the name of the branch city is 'c3'. The selected
tuples are grouped by the customer name of the depositor table whose count should be equal to
the count of the branch name in the branch table with branch city 'c3'.
v. Demonstrate how you delete all account tuples at every branch located in a specific city.
mysql> DELETE FROM account WHERE branch_name IN(SELECT branch_name FROM
branch WHERE branch_city='c5');
Query OK, 1 row affected (0.04 sec)
mysql>SELECT * FROM account;
Description: The inner query "SELECT branch_name FROM branch WHERE branch_city='c5' "
selects the branch names from the branch table where the branch city is c5. The selected tuples
are given as input to the outer query which deletes the tuples with the selected branch names.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy