Bank database

branch(branch_name, branch_city, assets)
customer (ID, customer_name, customer_street, customer_city)
loan (loan_number, branch_name, amount)
borrower (ID, loan_number)
account (account_number, branch_name, balance)
depositor (ID, account_number)


image.png HW01 CISC3000 Assignment 01 - 图2

Question 01

Consider the above bank database. Give an expression in the relational algebra for each of the following queries.
a. Find the name of each branch located in “Chicago”.

  1. -- a. Find the name of each branch located in Chicago”.
  2. select branch_name branch_name]
  3. from branch [(branch)]
  4. where branch_city = "Chicago" branch_city = "Chicago"]
  5. a. π branch_name branch_city = "Chicago"(branch))

b. Find the ID of each borrower who have a loan in branch “Downtown”.

  1. -- b. Find the ID of each borrower who have a loan in branch Downtown”.
  2. select ID ID]
  3. from loan natural join borrower using (ID) [(borrower borrower.ID=loan.ID loan)]
  4. where branch_name = "Downtown" branch_name = "Downtown"]
  5. b. π ID ( σ branch_name = "Downtown" (borrower borrower.ID=loan.ID loan))

Question 02

Consider the above bank database.
Assume that branch names and IDs uniquely identify branches and customers, but loans and accounts can be associated with more than one customer.

Depositor

ID Account_number
ID1 A01
ID2 A01

Borrower

ID Loan_number
ID1 L01
ID2 L01

The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).

a. What are the appropriate primary keys?

  1. a. What are the appropriate primary keys?
  2. Ans:
  3. 1) branch_name in the 'branch' table is the primary key in 'branch' table
  4. 2) ID in the 'customer' table is the primary key in 'customer' table
  5. 3) loan_number - 'loan'
  6. 4) account_number - 'account'
  7. 5) ID - 'depositor'
  8. 6) ID - 'borrower'

Correct Answer:
a. The primary keys of the various schemas are underlined.
We allow customers to have more than one account, and more than one loan.
branch(branch_name, branch_city, assets)
customer (ID, customer_name, customer_street, customer_city)
loan (loan_number, branch_name, amount)
borrower (ID, loan_number)
account (account_number, branch_name, balance)
depositor (ID, account_number)

b. Given your choice of primary keys, identify appropriate foreign keys.

Foreign key constraint: Value in one relation must appear in another

  1. b. Given your choice of primary keys, identify appropriate foreign keys.
  2. Ans:
  3. 1) ID in customer - 'depositor'
  4. 2) ID in customer - 'borrower'
  5. 3) brancher_name in branch - 'loan'
  6. 4) brancher_name in branch - 'account'

Correct Answer
b. The foreign keys are as follows:
i. For loan: branch name referencing branch.
ii. For borrower: Attribute ID referencing customer and loan number referencing loan
iii. For account: branch name referencing branch.
iv. For depositor: Attribute ID referencing customer and account number referencing account

Question 03

Construct a schema diagram for the above bank database.
Use the primary and foreign keys of your previous exercise.
🟢 as primary key
🔶 as foreign keys

image.png

Question 04

Consider the above bank database.
Construct the following SQL queries for this relational database.
a. Find the ID of each customer of the bank who has an account but not a loan.

  1. -- 4.a. Find the ID of each customer of the bank who has an account but not a loan.
  2. select C.ID
  3. from customer as C,depositor as D
  4. where C.ID = D.ID
  5. and C.ID not in (
  6. select ID
  7. from borrower)

Correct Answer
a. Find the ID of each customer of the bank who has an account but not a loan.

  1. (select ID from depositor)
  2. except
  3. (select ID from borrower)

b. Find the ID of each customer who lives on the same street and in the same city as customer “12345”.

  1. -- 4.b. Find the ID of each customer who lives on the same street and in the same city as customer 12345”.
  2. select ID
  3. from customer
  4. where customer_street, customer_city in (
  5. select customer_street, customer_city
  6. from customer
  7. where ID = '12345')

Correct b.
b. Find the ID of each customer who lives on the same street and in the same city as customer “12345”.

  1. select F.ID
  2. from customer as F, customer as S
  3. where F.customer_street = S.customer_street
  4. and F.customer_city = S.customer_city
  5. and S.customer_id = '12345'

c. Find the name of each branch that has at least one customer who has an account in the bank and who lives in “Harrison”.

  1. /*
  2. c. Find the name of each branch that has at least one customer
  3. who has an account in the bank and who lives in “Harrison”.
  4. */
  5. select branch_name
  6. from account natural join depositor
  7. where ID in (
  8. select ID
  9. from customer
  10. where customer_city = 'Harrison')

Correct c.
c. Find the name of each branch that has at least one customer who has an account in the bank and who lives in Harrison.

  1. select distinct branch name
  2. from account, depositor, customer
  3. where customer.id = depositor.id
  4. and depositor.account_number = account.account_number
  5. and customer_city = 'Harrison'

Question 05

image.png
Consider the above bank database.
Construct the following SQL queries for this relational database.
a. Find each customer who has an account at every branch located in “Brooklyn”.

  1. /*
  2. a. Find each customer who has an account
  3. at every branch located in “Brooklyn”.
  4. */
  5. select ID, account_number
  6. from depositor natural join account
  7. where branch_name in (
  8. select branch_name
  9. from branch
  10. where branch_city = "Brooklyn"
  11. )

Correct 5.a
a. Find each customer who has an account at every branch located in “Brooklyn”.

  1. select ID, customer_name
  2. from customer as c
  3. where (
  4. -- Count the number of branch in Brooklyn--
  5. select count(*)
  6. from branch
  7. where branch_city = 'Brooklyn')
  8. =
  9. -- Customer has an account in Brooklyn --
  10. (select count(distinct branch.branch name)
  11. from customer, depositor, account, branch
  12. where (
  13. depositor.ID = customer.ID
  14. and depositor.account number = account.account number
  15. and account.branch_name = branch.branch_name
  16. and branch city = 'Brooklyn'))

There are other ways of writing this query, for example by first finding customers who do not have an account at some branch in Brooklyn, and then removing these customers from the set of all customers by using an except clause.


b. Find the total sum of all loan amounts in the bank.

  1. /*
  2. b. Find the total sum of all loan amounts in the bank.
  3. */
  4. select sum(amount) as SUM_LOAN, branch_name
  5. from loan
  6. group by branch_name

Correct 5.b
b. Find the total sum of all loan amounts in the bank.

  1. select sum(amount)
  2. from loan

c. Find the names of all branches that have assets greater than
those of at least one branch located in “Brooklyn”.

  1. /*
  2. c. Find the names of all branches that have assets greater than
  3. those of at least one branch located in “Brooklyn”.
  4. */
  5. select branch_name
  6. from branch
  7. where assets > some(
  8. select assets
  9. from branch
  10. where branch_city = 'Brooklyn')

Correct 5.c
c. Find the names of all branches that have assets greater than
those of at least one branch located in “Brooklyn”.

  1. select branch name
  2. from branch
  3. where assets > some(
  4. select assets
  5. from branch
  6. where branch city = 'Brooklyn')

The keyword any could be used in place of some above.