已知银行企业的数据库由以下表组成: ①分行表 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) 注:带下划线的属性为主码,假设客户的名字不相同 完成如下操作: (1)用SQL语句创建表。 (2)使用关系代数和SQL语句找出在Brighton银行中有存款的所有客户的姓名、存款号、和存款额。 (3)使用关系代数和SQL语句找出账户平均余额小于5000元的支行,显示支行名称及账户平均余额。 (4)使用关系代数和SQL语句找出所有在银行中有贷款但无账户的客户。 (5)使用关系代数和SQL语句对所有存款余额大于平均存款额的账户付3%的利息。
时间: 2024-02-16 07:02:32 浏览: 73
用SQL写的简单的银行数据库
5星 · 资源好评率100%
很抱歉,我是一个自然语言处理AI,无法直接执行SQL语句或关系代数操作。但我可以给你提供一些参考:
(1)用SQL语句创建表。
```sql
CREATE TABLE branch (
branch_name VARCHAR(255) PRIMARY KEY,
branch_city VARCHAR(255),
assets DECIMAL(10,2)
);
CREATE TABLE customer (
customer_name VARCHAR(255) PRIMARY KEY,
customer_street VARCHAR(255),
customer_city VARCHAR(255)
);
CREATE TABLE loan (
loan_number INT PRIMARY KEY,
branch_name VARCHAR(255),
amount DECIMAL(10,2)
);
CREATE TABLE borrower (
customer_name VARCHAR(255),
loan_number INT,
PRIMARY KEY (customer_name, loan_number),
FOREIGN KEY (customer_name) REFERENCES customer(customer_name),
FOREIGN KEY (loan_number) REFERENCES loan(loan_number)
);
CREATE TABLE account (
account_number INT PRIMARY KEY,
branch_name VARCHAR(255),
balance DECIMAL(10,2)
);
CREATE TABLE depositor (
customer_name VARCHAR(255),
account_number INT,
PRIMARY KEY (customer_name, account_number),
FOREIGN KEY (customer_name) REFERENCES customer(customer_name),
FOREIGN KEY (account_number) REFERENCES account(account_number)
);
```
(2)使用关系代数和SQL语句找出在Brighton银行中有存款的所有客户的姓名、存款号、和存款额。
关系代数:
$$\pi_{customer\_name, account\_number, balance}(\sigma_{branch\_name='Brighton'}(depositor \bowtie account))$$
SQL语句:
```sql
SELECT customer_name, account_number, balance
FROM depositor
JOIN account ON depositor.account_number = account.account_number
WHERE account.branch_name = 'Brighton';
```
(3)使用关系代数和SQL语句找出账户平均余额小于5000元的支行,显示支行名称及账户平均余额。
关系代数:
$$\pi_{branch\_name,avg(balance)}(\rho_{account2}(account) \bowtie_{account1.branch\_name = account2.branch\_name} \gamma_{account1.branch\_name,avg(balance)}(account as account1))$$
$$\sigma_{avg(balance) < 5000}(account2)$$
SQL语句:
```sql
SELECT account1.branch_name, AVG(account2.balance) AS avg_balance
FROM account AS account1
JOIN account AS account2 ON account1.branch_name = account2.branch_name
GROUP BY account1.branch_name
HAVING AVG(account2.balance) < 5000;
```
(4)使用关系代数和SQL语句找出所有在银行中有贷款但无账户的客户。
关系代数:
$$\pi_{customer\_name}(\sigma_{borrower.loan\_number = loan.loan\_number \land account.branch\_name \text{ is null}}(borrower \bowtie loan \bowtie account))$$
SQL语句:
```sql
SELECT borrower.customer_name
FROM borrower
JOIN loan ON borrower.loan_number = loan.loan_number
LEFT JOIN account ON loan.branch_name = account.branch_name
WHERE account.account_number IS NULL;
```
(5)使用关系代数和SQL语句对所有存款余额大于平均存款额的账户付3%的利息。
关系代数:
$$account' \leftarrow \sigma_{balance > avg(balance)}(account)$$
$$account' \leftarrow \rho_{account2}(account')$$
$$account' \leftarrow account' \cup \gamma_{avg(balance)}(account')$$
$$account' \leftarrow \rho_{account3}(account')$$
$$account' \leftarrow account' \bowtie_{account2.branch\_name = account3.branch\_name}(account2 \times account3)$$
$$account' \leftarrow \sigma_{interest\_rate = 0.03}(account')$$
$$account \leftarrow account \cup account'$$
SQL语句:
```sql
UPDATE account
SET balance = balance * 1.03
WHERE balance > (SELECT AVG(balance) FROM account);
```
阅读全文