0% found this document useful (0 votes)
246 views8 pages

Bank Tables

The document contains SQL code to create tables for a banking database including tables for branches, customers, accounts, loans, depositors, and borrowers. It then populates the tables with sample data and writes various SQL queries to retrieve information from the tables, including finding averages, counts, intersections, and updating records.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
246 views8 pages

Bank Tables

The document contains SQL code to create tables for a banking database including tables for branches, customers, accounts, loans, depositors, and borrowers. It then populates the tables with sample data and writes various SQL queries to retrieve information from the tables, including finding averages, counts, intersections, and updating records.
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 8

drop drop drop drop drop drop

table table table table table table

depositor; borrower; customer; account; loan; branch;

-- Table branch CREATE TABLE branch( branch_name char(20) PRIMARY KEY, branch_city char(20), assets int, check (assets >=0) ); -- Table customer CREATE TABLE customer( customer_name char(20) PRIMARY KEY, customer_street char(20), customer_city char(20) ); --Table loan CREATE TABLE loan( loan_number char(20), branch_name char(20) REFERENCES branch(branch_name), amount int, PRIMARY KEY(loan_number) ); --Table account CREATE TABLE account( account_number char(20), branch_name char(20) REFERENCES branch(branch_name), balance int, PRIMARY KEY(account_number) ); -- Table depositor CREATE TABLE depositor( customer_name char(20) REFERENCES customer(customer_name), account_number char(20) REFERENCES account(account_number), PRIMARY KEY (customer_name,account_number) ); -- Table borrower CREATE TABLE borrower( customer_name char(20) REFERENCES customer(customer_name), loan_number char(20) REFERENCES loan(loan_number), PRIMARY KEY (customer_name,loan_number)

); -- Look at the tables that we just created SELECT table_name FROM user_tables; --- Populate the tables --- Populate INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO -- Populate INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO -- Populate INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO -- Populate INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO branch branch branch branch branch branch branch branch branch account account account account account account account account customer customer customer customer customer customer customer customer customer customer customer customer customer customer depositor depositor depositor depositor depositor depositor depositor

VALUES('Brighton','Brooklyn',720000); VALUES('Downtown','Brooklyn',9000000); VALUES('Mianus','Horseneck',400000); VALUES('North Town','Rye',3700000); VALUES('Perryridge','Horseneck',1700000); VALUES('Pownal','Bennington',300000); VALUES('Redwood','Palo Alto',220000); VALUES('Round Hill','Horseneck',8000000);

VALUES('A-101','Downtown',500); VALUES('A-102','Perryridge',400); VALUES('A-201','Brighton',900); VALUES('A-215','Mianus',700); VALUES('A-217','Brighton',750); VALUES('A-222','Redwood',700); VALUES('A-305','Round Hill',350);

VALUES('Adams','Spring','Pittsfield'); VALUES('Brooks','Senator','Brooklyn'); VALUES('Curry','North','Rye'); VALUES('Glenn','Sand Hill','Woodside'); VALUES('Green','Walnut','Stamford'); VALUES('Hayes','Main','Harrison'); VALUES('Johnson','Alma','Palo Alto'); VALUES('Jackson','Alma','Palo Alto'); VALUES('Jones','Main','Harrison'); VALUES('Lindsay','Park','Pittsfield'); VALUES('Smith','North','Rye'); VALUES('Turner','Putnam','Stamford'); VALUES('Williams','Nassau','Princeton');

VALUES('Hayes','A-102'); VALUES('Johnson','A-101'); VALUES('Johnson','A-201'); VALUES('Jones','A-217'); VALUES('Lindsay','A-222'); VALUES('Smith','A-215');

INSERT INTO depositor VALUES('Turner','A-305'); -- Populate INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO -- Populate INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO loan loan loan loan loan loan loan loan

VALUES('L-11','Round Hill',900); VALUES('L-14','Downtown',1500); VALUES('L-15','Perryridge',1500); VALUES('L-16','Perryridge',1300); VALUES('L-17','Downtown',200); VALUES('L-23','Redwood',2000); VALUES('L-93','Mianus',500);

borrower borrower borrower borrower borrower borrower borrower borrower borrower

VALUES('Adams','L-16'); VALUES('Curry','L-93'); VALUES('Hayes','L-15'); VALUES('Jackson','L-14'); VALUES('Jones','L-17'); VALUES('Smith','L-11'); VALUES('Smith','L-23'); VALUES('Williams','L-17');

-- find the table names of all existing tables created by users select table_name from user_tables;

-- find the schema of the customer table desc customer

-- direct the output of a query to a file named queryResult.lst spool queryResult; select * from employees; spool off;

-- Save the contents of the SQL buffer (previous SQL statement) into a file with name mySQLFile.sql save mySQLFile

-- Run the SQL*Plus statements in the file mySQLFile @mySQLFile -- Quit Oracle quit; --set output line width to 500 characters set line 500 -- set pagesize to 10 lines set pagesize 10 --_ alter table alter table branch alter table branch alter table branch alter table branch

add (county char(20)); add primary key (branch_name); add constraint countyUnique unique (county); modify (branch_city char(30) default 'Denton' constraint cityNotnull not null); alter table branch add constraint ACheck check (assets>0); alter table branch drop constraint countyUnique;

alter table branch drop constraint Acheck; alter table branch drop constraint citynotnull; alter table branch drop primary key cascade;

--___SQL does not eliminate duplications automatically -- List the branch names of branches what have a loan select branch_name from loan; select distinct branch_name from loan; -- List all loans select * from loan; -- Find loan number, branch name, and the loan amount multiplied by 100 of each loan select loan_number, branch_name, amount * 100 from loan; --Find all loan number for loans made at the Perryridge branch with loan amounts greater than $1200. select loan_number from loan where branch_name = 'Perryridge' and amount > 1200; --Find the loan number of those loans with loan amounts between $90,000 and $100,000 select loan_number from loan where amount between 90000 and 100000; -- Try the following statement and say the result as a Cartisian product select * from borrower, loan; -- Find the name, loan number and loan amount of all customers having a loan at the Perryridge branch. select customer_name, borrower.loan_number, amount from borrower, loan where borrower.loan_number = loan.loan_number and branch_name = 'Perryridge'; -- Rename an attribute in the result using 'as' select customer_name, borrower.loan_number as loan_id, amount from borrower, loan where borrower.loan_number = loan.loan_number;

-- Rename a relation -- Note: in oracle 'as' is omitted select customer_name, T.loan_number, S.amount from borrower T, loan S where T.loan_number = S.loan_number; --Find the names of all branches that have greater assets than some branch located in Brooklyn. select distinct T.branch_name from branch T, branch S where T.assets > S.assets and S.branch_city = 'Brooklyn'; _ Or select branch_name from branch where assets > some ( select assets from branch where branch_city = 'Brooklyn' ); --Find the names of all customers whose street includes the substring Main. select customer_name from customer where customer_street like '%Main%'; --List in alphabetic order the names of all customers having a loan in Perryridge branch select distinct customer_name from borrower, loan where borrower.loan_number = loan.loan_number and branch_name = 'Perryridge' order by customer_name desc; -- Find all customers who have a loan, an account, or both: (select customer_name from depositor) union (select customer_name from borrower); -- Find all customers who have both a loan and an account. (select customer_name from depositor) intersect (select customer_name from borrower); -- Find all customers who have an account but no loan. (select customer_name from depositor) minus (select customer_name from borrower); -- Find the average account balance at the Perryridge branch. select avg (balance) from account where branch_name = 'Perryridge';

--Find the number of tuples in the customer relation. select count (*) from customer; -- Find the number of depositors in the bank. select count (distinct customer_name) from depositor; -- Find the number of depositors for each branch. select branch_name, count (distinct customer_name) from depositor, account where depositor.account_number = account.account_number group by branch_name; -- Find the names of all branches where the average account balance is more than $500. select branch_name, avg (balance) from account group by branch_name having avg (balance) > 500; --Find all loan number which appear in the loan relation with null values for amount. select loan_number from loan where amount is null; -- Find all customers who have both an account and a loan at the bank. select distinct customer_name from borrower where customer_name in (select customer_name from depositor); -- Find all customers who have a loan at the bank but do not have account at the bank select distinct customer_name from borrower where customer_name not in (select customer_name from depositor); -- Find the names of all branches that have greater assets than all branches located in Brooklyn. select branch_name from branch where assets > all (select assets from branch where branch_city = 'Brooklyn'); -- Find all customers who have an account at all branches located in Brooklyn. select distinct S.customer_name from depositor S where not exists ( (select branch_name an

from branch where branch_city = 'Brooklyn') minus (select R.branch_name from depositor T, account R where T.account_number = R.account_number and S.customer_name = T.customer_name)); -- Find all customers who have at most one account at the Perryridge branch. select T.customer_name from depositor T where 1 = ( select count(R.customer_name) from account, depositor R where T.customer_name = R.customer_name and R.account_number = account.account_number and account.branch_name = 'Perryridge'); -- set the assets of Perryridge branch to 100,000 update branch set assets = 100000 where branch_name= 'Perryridge'; -- increase the balances of the accounts in Perryridge branch by 10%, in Brooklyn by 5%, and others by 1% update account set balance = case when branch_name = 'Perryridge' then balance *1.10 when branch_name = 'Brooklyn' then balance *1.05 else balance *1.01 end;

You might also like