1. 程式人生 > >第3章 SQL 習題 - 3.8

第3章 SQL 習題 - 3.8

3.8考慮下圖中的銀行資料庫,其中加下劃線的是主碼。為這個關係資料庫構造出如下SQL查詢:

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

)

為了解題方便,先建立這些關係,並填充一些樣例資料。

drop table if exists branch cascade;
drop table if exists customer cascade;
drop table if exists loan cascade;
drop table if exists borrower cascade;
drop table if exists account cascade;
drop table if exists depositor cascade;
create table branch(
    branch_name varchar not null, -- '支行名稱', 
    branch_city varchar , -- '所在城市', 
    assets numeric(18,2) DEFAULT '0.00' , -- '賬戶餘額',
    PRIMARY KEY(branch_name)
);
create table customer(
    customer_name varchar not null , -- '客戶名稱', 
    customer_street varchar , -- '居住街道', 
    customer_city varchar , -- '居住城市',
    primary key(customer_name)
);
create table loan(
    loan_number varchar , -- '貸款號', 
    branch_name varchar , -- '支行名稱', 
    amount numeric(18, 2) , -- '貸款金額',
    primary key (loan_number),
    foreign key (branch_name) references branch on delete no action
);
create table borrower(
    customer_name varchar , -- '客戶名稱', 
    loan_number varchar , -- '貸款號',
    primary key (customer_name, loan_number),
    foreign key (customer_name) references customer on delete no action,
    foreign key (loan_number) references loan on delete no action
);
create table account(
    account_number varchar , -- '賬戶號', 
    branch_name varchar , -- '支行名稱', 
    balance numeric(18,2) , -- '賬戶金額',
    primary key (account_number),
    foreign key (branch_name) references branch on delete no action
);
create table depositor(
    customer_name varchar , -- '客戶名稱', 
    account_number varchar , -- '賬戶號',
    primary key (customer_name, account_number),
    foreign key (customer_name) references customer on delete no action,
    foreign key (account_number) references account on delete no action
);

insert into branch values ('銀行支行_1', '北京', 66666);
insert into branch values ('銀行支行_2', '北京', 77777);
insert into branch values ('銀行支行_3', '北京', 88888);
insert into branch values ('銀行支行_4', '北京', 99999);

insert into customer values ('客戶_1', '中關村北大街', '北京');
insert into customer values ('客戶_2', '西二旗軟體園', '北京');
insert into customer values ('客戶_3', '回龍觀', '北京');
insert into customer values ('客戶_4', '西二旗軟體園', '北京');
insert into customer values ('客戶_5', '回龍觀', '北京');
insert into customer values ('客戶_6', '中關村北大街', '北京');
insert into customer values ('客戶_7', '上地環島', '北京');
insert into customer values ('客戶_8', '上地環島', '北京');

insert into loan values ('貸款號_1', '銀行支行_1', 120);
insert into loan values ('貸款號_2', '銀行支行_1', 230);
insert into loan values ('貸款號_3', '銀行支行_3', 345);
insert into loan values ('貸款號_4', '銀行支行_4', 139);
insert into loan values ('貸款號_5', '銀行支行_1', 233);
insert into loan values ('貸款號_6', '銀行支行_2', 45);
insert into loan values ('貸款號_7', '銀行支行_3', 65);
insert into loan values ('貸款號_8', '銀行支行_4', 12345);

insert into borrower values ('客戶_1', '貸款號_1');
insert into borrower values ('客戶_6', '貸款號_2');
insert into borrower values ('客戶_3', '貸款號_3');
insert into borrower values ('客戶_8', '貸款號_4');
insert into borrower values ('客戶_1', '貸款號_5');
insert into borrower values ('客戶_6', '貸款號_6');
insert into borrower values ('客戶_2', '貸款號_7');
insert into borrower values ('客戶_7', '貸款號_8');

insert into account values ('賬戶_1', '銀行支行_1');
insert into account values ('賬戶_2', '銀行支行_3');
insert into account values ('賬戶_3', '銀行支行_3');
insert into account values ('賬戶_4', '銀行支行_4');
insert into account values ('賬戶_5', '銀行支行_1');
insert into account values ('賬戶_6', '銀行支行_1');
insert into account values ('賬戶_7', '銀行支行_4');
insert into account values ('賬戶_8', '銀行支行_4');
insert into account values ('賬戶_9', '銀行支行_2');

insert into depositor values ('客戶_1', '賬戶_1');
insert into depositor values ('客戶_1', '賬戶_9');
insert into depositor values ('客戶_2', '賬戶_2');
insert into depositor values ('客戶_3', '賬戶_3');
insert into depositor values ('客戶_4', '賬戶_4');
insert into depositor values ('客戶_5', '賬戶_5');
insert into depositor values ('客戶_6', '賬戶_6');
insert into depositor values ('客戶_7', '賬戶_7');
insert into depositor values ('客戶_8', '賬戶_8');

a.找出銀行中所有有賬戶但無貸款的客戶;

(select customer_name from depositor)
except
(select customer_name from borrower);
 customer_name 
---------------
 客戶_5
 客戶_4
(2 rows)

b.找出與"Smith"居住在同一個城市、同一個街道的所有客戶的名字。

咱們的樣本資料裡沒有叫smith的客戶,假設查詢叫“客戶_4”的客戶吧。

with target_customer as (
	select customer_street, customer_city from customer
	where customer_name = '客戶_4'
)
select * from customer natural join target_customer;
 customer_street | customer_city | customer_name 
-----------------+---------------+---------------
 西二旗軟體園    | 北京          | 客戶_2
 西二旗軟體園    | 北京          | 客戶_4
(2 rows)

c.找出所有支行的名稱,在這些支行中都有居住在"Harrison"的客戶所開設的賬戶。

假設查詢居住在“回龍觀”的客戶吧。

with target_account as (
	select account_number 
	from customer natural join depositor
	where customer_street = '回龍觀'
)
select distinct branch_name from account natural join target_account;
 branch_name 
-------------
 銀行支行_1
 銀行支行_3
(2 rows)