请考图-1的银行数据库。假设支行名称和客户姓名能够唯一标识 和账户可以与多位客户相关联 a.适当的主码是什么? b.请给出你选择的主码,并确定适当的外码 pranch(branch name, branch city, assets) customer (ID, customer name, customer street, cust omer ciy) oan (loan number, branch name, amount) borrower(ID,loan_number) account(account number, pranch name,balance)
时间: 2023-03-24 13:03:20 浏览: 605
a. 适当的主码是指能够唯一标识每个实体的属性或属性组合。根据给出的银行数据库,支行名称和客户姓名能够唯一标识客户实体,而账户可以与多位客户相关联,因此,主码应该是由支行名称、客户姓名和账户号码组成的属性组合。
b. 我选择的主码是由支行名称、客户姓名和账户号码组成的属性组合。适当的外码应该是从账户实体到客户实体的外码,即账户号码作为外码指向客户实体的ID属性。
相关问题
请考下列银行数据库: pranch(branch name, branch city, assets) customer (ID, customer name, customer street, cust omer ciy) oan (loan number, branch name, amount) borrower(ID,loan_number) account(account number, pranch name,balance) 假设branch name和customer name能够唯一标识出支行和客户,但是贷款和账户可以与多位客户相关联。请问: 1.适当的主码是什么? 2.请给出你选择的主码,并确定适当的外码
1. 适当的主码应该是能够唯一标识每个实体的属性或属性组合,以确保数据的完整性和一致性。在这个数据库中,可能的主码包括:branch name、customer name、loan number、account number、ID。
2. 我选择的主码是:branch name、customer name、loan number、account number、ID。对于外码,可以将borrower表中的ID和loan_number作为外码,与customer表和loan表进行关联;将account表中的branch name作为外码,与branch表进行关联。
已知银行企业的数据库由以下表组成: ①分行表 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%的利息。
很抱歉,我是一个自然语言处理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);
```
阅读全文