已知银行企业的数据库由以下表组成: 0分行表 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-17 14:02:29 浏览: 90
(1) SQL 语句创建表:
```sql
CREATE TABLE branch (
branch_name VARCHAR(50),
branch_city VARCHAR(50),
assets DECIMAL(10, 2),
PRIMARY KEY (branch_name)
);
CREATE TABLE customer (
customer_name VARCHAR(50),
customer_street VARCHAR(50),
customer_city VARCHAR(50),
PRIMARY KEY (customer_name)
);
CREATE TABLE loan (
loan_number INT,
branch_name VARCHAR(50),
amount DECIMAL(10, 2),
PRIMARY KEY (loan_number),
FOREIGN KEY (branch_name) REFERENCES branch(branch_name)
);
CREATE TABLE borrower (
customer_name VARCHAR(50),
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,
branch_name VARCHAR(50),
balance DECIMAL(10, 2),
PRIMARY KEY (account_number),
FOREIGN KEY (branch_name) REFERENCES branch(branch_name)
);
CREATE TABLE depositor (
customer_name VARCHAR(50),
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 d.customer_name, d.account_number, a.balance
FROM depositor d
INNER JOIN account a
ON d.account_number = a.account_number
WHERE a.branch_name = 'Brighton';
```
(3) 使用关系代数和 SQL 语句找出账户平均余额小于 5000 元的支行,显示支行名称及账户平均余额。
关系代数:
$$
\pi_{branch\_name, avg\_balance}(\gamma_{branch\_name}(account) \div \rho_{avg\_balance}(account))
$$
$$
\pi_{branch\_name, avg\_balance}(\sigma_{avg\_balance<5000}(\pi_{branch\_name, avg\_balance}(\gamma_{branch\_name}(account) \div \rho_{avg\_balance}(account))))
$$
SQL 语句:
```sql
SELECT a.branch_name, AVG(a.balance) AS avg_balance
FROM account a
GROUP BY a.branch_name
HAVING AVG(a.balance) < 5000;
```
(4) 使用关系代数和 SQL 语句找出所有在银行中有货款但无账户的客户。
关系代数:
$$
\Pi_{customer\_name}((borrower - \sigma_{account.account\_number IS NOT NULL}(depositor)) \bowtie loan)
$$
SQL 语句:
```sql
SELECT b.customer_name
FROM borrower b
INNER JOIN loan l
ON b.loan_number = l.loan_number
LEFT JOIN depositor d
ON b.customer_name = d.customer_name
WHERE d.account_number IS NULL;
```
(5) 使用关系代数和 SQL 语句对所有存款余额大于平均存款额的账户付3%的利息。
关系代数:
$$
account' \leftarrow \gamma_{AVG(balance)}(account)
$$
$$
account'' \leftarrow account \bowtie \rho_{interest\_rate}(3\%)
$$
$$
account''' \leftarrow account'' \bowtie \gamma_{account\_number}(\sigma_{balance>AVG(balance)}(account'))
$$
$$
account''' \leftarrow \sigma_{balance'=balance*1.03}(account''')
$$
SQL 语句:
```sql
UPDATE account
SET balance = balance * 1.03
WHERE balance > (
SELECT AVG(balance)
FROM account
);
```
阅读全文