---
# System prepended metadata

title: 資料庫作業四
tags: [1112_courses, database]

---

# 資料庫作業四
* 姓名：張議隆
* 學號：F74082125

:::spoiler TOC
[TOC]
:::

## 6.16
:::info
Specify the following queries on the COMPANY relational database schema shown in Figure 3.5, using the relational operators discussed in this chapter. Also show the result of each query as it would apply to the database state in Figure 3.6. 
:::
![](https://hackmd.io/_uploads/rk0RhYE82.png)
![](https://hackmd.io/_uploads/SyY1atNUn.png)

:::warning
用 Latex 打這東西真痛苦，每個文字都要用 `\text{}` 包住，符號也得額外查詢。
:::
a. Retrieve the names of all employees in department 5 who work more than 10 hours per week on the ProductX project.

$\text{ProjectX} \leftarrow \sigma_{\text{Pname="ProductX"}}(\text{PROJECT}) \\
\text{WorksOn_X} \leftarrow \text{ProjectX} \bowtie_{\text{Pnumber=Pno}} \text{WORKS_ON} \\
\text{X_10_more} \leftarrow \sigma_{\text{Hours>10}}(\text{WorksOn_X}) \\
\text{Employee_10_X} \leftarrow \text{X_10_more} \bowtie_{\text{Essn=Ssn}} \text{EMPLOYEE} \\
\text{Answer} \leftarrow \pi_{\text{Fname, Lname}}(\text{Employee_10_X})$

---
b. List the names of all employees who have a dependent with the same first name as themselves.

$\text{Full_Dependent} \leftarrow \text{EMPLOYEE} \bowtie_{\text{Ssn=Essn}} \text{DEPENDENT} \\
\text{Same_Dependent} \leftarrow \sigma_{\text{Fname=Dependent_name}}(\text{Full_Dependent}) \\
\text{Answer} \leftarrow \pi_{\text{Fname, Lname}}(\text{Same_Dependent})$

---

c. Find the names of all employees who are directly supervised by ‘Franklin Wong’.

$\text{Full_Employee_and_Supervisor} \leftarrow \text{EMPLOYEE} \bowtie_{\text{Ssn=Super_ssn}} \text{EMPLOYEE} \\
\text{F_W_Ssn} \leftarrow \pi_{\text{Ssn}}(\sigma{\text{Fname="Franklin", Lname="Wong"}}(EMPLOYEE))\\
\text{Answer} \leftarrow \pi_{\text{Fname, Lname}}(\sigma_{\text{Super_ssn=F_W_Ssn}}(\text{Full_Employee_and_Supervisor}))$

---

d. For each project, list the project name and the total hours per week (by all employees) spent on that project.

這題第一行的 JOIN 是 Left Outer Join，只是我真的打不出那個字了。
$\text{Project_and_Work} \leftarrow \text{PROJECT} \bowtie_{Pnumber=Pno} \text{WORKS_ON} \\
\text{Hours} \leftarrow _{Pname}\mathcal{F}_{SUM\ Hours}(\text{Project_and_Work}) \\
\text{Answer} \leftarrow \pi_{\text{Pname, Sum_hours}}(\text{Hours})$

---

e. Retrieve the names of all employees who work on every project.

$\text{All_Project_Number} \leftarrow \rho_{\text{Pno}}(\pi_{Pnumber}(PROJECT)) \\
\text{Ssn_With_All_Project} \leftarrow \text{WORKS_ON} \div \text{All_Project_Number} \\
\text{Selected_Employee} \leftarrow \sigma_{\text{Ssn=Ssn_With_All_Project}}(EMPLOYEE) \\
\text{Answer} \leftarrow \pi_{\text{Fname, Lname}}(\text{Selected_Employee})$

---
f. Retrieve the names of all employees who do not work on any project.

$\text{All_Ssn} \leftarrow \pi_{Ssn}(EMPLOYEE) \\
\text{Ssn_With_Project} \leftarrow \rho_{Ssn}(\pi_{Essn}(\text{WORKS_ON})) \\
\text{Ssn_Without_Project} \leftarrow \text{All_Ssn} - \text{Ssn_With_Project} \\
\text{Answer} \leftarrow \pi_{\text{Fname, Lname}}(\sigma_{\text{Ssn=Ssn_Without_Project}}(EMPLOYEE))$

---

g. For each department, retrieve the department name and the average salary of all employees working in that department.

$\text{Salary_And_Dno} \leftarrow \pi_{Salary, Dno}(EMPLOYEE) \\
\text{Average_Salary_per_Dno} \leftarrow _{Dno}\mathcal{F}_{AVERAGE\ Salary}(\text{Salary_And_Dno}) \\
\text{Department_And_AVG_Salary} \leftarrow \text{DEPARTMENT} \bowtie_{Dnumber=Dno} \text{Average_Salary_per_Dno} \\
\text{Answer} \leftarrow \pi_{\text{Dname, Average_salary}}(\text{Department_And_AVG_Salary})$

---
h. Retrieve the average salary of all female employees.

$\text{Female_Employee} \leftarrow \sigma_{\text{Sex="F"}}(EMPLOYEE) \\
\text{Answer} \leftarrow _{Sex}\mathcal{F}_{AVERAGE\ Salary}(\text{Female_Employee})$

---
i. Find the names and addresses of all employees who work on at least one project located in Houston but whose department has no location in Houston.

$\text{Project_In_Houston} \leftarrow \sigma_{\text{Plocation="Houston"}}(\text{PROJECT}) \\
\text{Department_Not_In_Houston} \leftarrow \sigma_{\text{Dlocation!="Houston"}}(\text{DEPARTMENT}) \\
\text{Join} \leftarrow \text{Project_In_Houston} \bowtie_{Plocation=Dlocation} \text{Department_Not_In_Houston} \\
\text{Selected_Pno} \leftarrow \pi_{\text{Pnumber}}(\text{Join})\\
\text{Selected_Ssn} \leftarrow \sigma_{\text{Pno=Pnumber}}(\text{Selected_Pno}) \\
\text{Answer} \leftarrow \pi_{Fname, Lname, Address}(\sigma_{\text{Ssn=Selected_Ssn}}(\text{EMPLOYEE}))$

---
j. List the last names of all department managers who have no dependents. 

$\text{Manager_Ssn} \leftarrow \rho_{Ssn}(\pi_{\text{Mgr_ssn}}(\text{DEPARTMENT})) \\
\text{Ssn_With_Dependent} \leftarrow \rho_{Ssn}(\pi_{Essn}(\text{DEPENTDENT})) \\
\text{Manager_Ssn_Without_Dependent} \leftarrow \text{Manager_Ssn} - \text{Ssn_With_Dependent} \\
\text{Answer} \leftarrow \pi_{\text{Lname}}(\sigma_{\text{Ssn=Manager_Ssn_Without_Dependent}}(EMPLOYEE))$

## 6.18
:::info
Consider the LIBRARY relational database schema shown in Figure 6.14, which is used to keep track of books, borrowers, and book loans. Referential integrity constraints are shown as directed arcs in Figure 6.14. Write down relational expressions for the following queries: 
:::

![](https://hackmd.io/_uploads/HJ2xaFVLn.png)

a. How many copies of the book titles The Lost Tribe are owned by the library branch whose name is ‘Sharpstown’?

$\text{That_Book_ID} \leftarrow \sigma_{\text{Title="The Lost Tribe"}}(\text{BOOK}) \\
\text{That_Branch_ID} \leftarrow \sigma_{\text{Branch_name="Sharpstown"}}(\text{LIBRARY_BRANCH}) \\
\text{Answer} \leftarrow \pi_{\text{No_of_copies}}(\sigma_{\text{Book_id=That_Book_ID, Branch_id=That_Branch_ID}}(\text{BOOK_COPIES}))$

---

b. How many copies of the book titles The Lost Tribe are owned by each library branch?

$\text{That_Book_ID} \leftarrow \sigma_{\text{Title="The Lost Tribe"}}(\text{BOOK}) \\
\text{Answer} \leftarrow \pi_{\text{No_of_copies}}(\sigma_{\text{Book_id=That_Book_ID}}(\text{BOOK_COPIES}))$

---

c. Retrieve the names of all borrowers who do not have any books checked out.

$\text{All_Borrowers} \leftarrow \pi_{\text{Card_no}}(\text{BORROWER}) \\
\text{All_Borrowers_Has_Books} \leftarrow \pi_{\text{Card_no}}(\text{BOOK_LOANS}) \\
\text{Borrowers_No_Book} \leftarrow \text{All_Borrowers} - \text{All_Borrowers_Has_Books} \\
\text{Answer} \leftarrow \pi_{\text{Name}}(\sigma_{\text{Card_no=Borrowers_No_Book}}(BORROWER))$

---

d. For each book that is loaned out from the Sharpstown branch and whose Due_date is today, retrieve the book title, the borrower’s name, and the borrower’s address.

我不知道要怎麼定義 `today` 是哪一天，因此請允許我直接用 `"today"` 代指應該要選的那天。
$\text{That_Branch_ID} \leftarrow \sigma_{\text{Branch_name="Sharpstown"}}(\text{LIBRARY_BRANCH}) \\
\text{Due_Today} \leftarrow \sigma_{\text{Branch_id=That_Branch_ID, Due_date="today"}}(\text{BOOK_LOANS}) \\
\text{Due_Book_id} \leftarrow \pi_{\text{Book_id}}(\text{Due_Today}) \\
\text{Due_Book_Title} \leftarrow \pi_{\text{Title}}(\sigma_{\text{Book_id=Due_Book_id}}(BOOK)) \\
\text{Borrowers_no} \leftarrow \pi_{\text{Card_no}}(\text{Due_Today}) \\
\text{Borrower_Info} \leftarrow \pi_{\text{Name, Address}}(\sigma_{\text{Card_no=Boorrowers_no}}(BORROWER)) \\
\text{Answer} \leftarrow \text{Due_Book_Title} \cup \text{Borrower_Info}$

---

e. For each library branch, retrieve the branch name and the total number of books loaned out from that branch.

$\text{Loan_Count_by_Branch_id} \leftarrow _{\text{Branch_id}}\mathcal{F}_{COUNT\ (\text{Branch_id})}(\text{BOOK_LOANS}) \\
\text{Answer} \leftarrow \pi_{\text{Branch_name, Count_branch_id}}(\text{LIBRARY_BRANCH}\ \cup\ \text{Loan_Count_by_Branch_id})$


---

f. Retrieve the names, addresses, and number of books checked out for all borrowers who have more than five books checked out.

$\text{Group_by_Loan_Count} \leftarrow _{\text{Card_no}}\mathcal{F}_{COUNT\ (\text{Card_no})}(\text{BOOK_LOANS}) \\
\text{Borrower_Satisfying} \leftarrow \sigma_{\text{Count_card_no > 5}}(\text{Group_by_Loan_Count})\\
\text{Answer} \leftarrow \pi_{\text{Name, Address, Count_card_no}}(\text{BORROWER}\ \cup\ \text{Borrower_Satisfying})$

---

g. For each book authored (or coauthored) by Stephen King, retrieve the title and the number of copies owned by the library branch whose name is Central. 

我不知道在這個資料庫內怎麼尋找共同作者，因此我就只尋找作者名。

$\text{Those_Book_ID} \leftarrow \sigma_{\text{Author_name="Stephen King"}}(\text{BOOK_AUTHORS}) \\
\text{Those_Books} \leftarrow \sigma_{\text{Book_id=Those_Book_ID}}(\text{BOOK}) \\
\text{That_Branch_ID} \leftarrow \sigma_{\text{Branch_name="Central"}}(\text{LIBRARY_BRANCH}) \\
\text{Books_Satisfying} \leftarrow \sigma_{\text{Branch_id=That_Branch_ID}}(\text{Those_Books}) \\
\text{Book_id_Satisfying} \leftarrow \pi_{Book_id}(\text{Books_Satisfying}) \\
\text{Copies_Satisfying} \leftarrow \sigma_{\text{Book_id=Book_id_Satisfying}}(\text{BOOK_COPIES}) \\
\text{Answer} \leftarrow \pi_{\text{Title, No_of_copies}}(\text{Books_Satisfying}\ \cup\ \text{Copies_Satisfying})$

## 7.16
:::info
Consider the following set of requirements for a UNIVERSITY database that is used to keep track of students’ transcripts. This is similar but not identical to the database shown in Figure 1.2:
:::

![](https://hackmd.io/_uploads/rJ2b6YNUh.png)


a. The university keeps track of each student’s name, student number, Social Security number, current address and phone number, permanent address and phone number, birth date, sex, class (freshman, sophomore, …, graduate), major department, minor department (if any), and degree program (B.A., B.S., …, Ph.D.). Some user applications need to refer to the city, state, and ZIP Code of the student’s permanent address and to the student’s last name. Both Social Security number and student number have unique values for each student.

---

b. Each department is described by a name, department code, office number, office phone number, and college. Both name and code have unique values for each department.

---

c. Each course has a course name, description, course number, number of semester hours, level, and offering department. The value of the course number is unique for each course.

---

d. Each section has an instructor, semester, year, course, and section number. The section number distinguishes sections of the same course that are taught during the same semester/year; its values are 1, 2, 3, …, up to the number of sections taught during each semester.

---

e. A grade report has a student, section, letter grade, and numeric grade (0, 1, 2, 3, or 4).

---

Design an ER schema for this application, and draw an ER diagram for the schema. Specify key attributes of each entity type, and structural constraints on each relationship type. Note any unspecified requirements, and make appropriate assumptions to make the specification complete.

每個 Table 的 Key attribute 為：(我不知道怎麼在圖形內加上底線，因此文字敘述)
* `STUDENT`：++`Student_id`++, ++`Ssn`++
* `DEPARTMENT`： ++`Department_code`++
* `SECTION`： ++`Section_number`++

另外 `CURRENT_ADDRESS` 、 `PERMANENT_ADDRESS` 與 `GRADE_REPORT` 都屬於 weak entity ，沒畫上去是因為沒有 `double diamond` 以及 `double box` 這類型的 [shape](https://graphviz.org/doc/info/shapes.html)
其他沒特別提到的限制等等我在圖形內應該都表示了，我盡可能地根據題目要求作圖。
```graphviz
graph ER_diagram{
    overlap = scale;
	node [fontname="Helvetica,Arial,sans-serif"]
	edge [fontname="Helvetica,Arial,sans-serif"]
	layout=fdp;
    
	node [shape=box]; STUDENT; 
	node [shape=ellipse];  First_name; Last_name; Student_id; Ssn; Current_address; Current_phone; Permanent_address; Permanent_phone; Birth_date; Sex; Class; Major; Minor; Degree;
    STUDENT -- First_name;
    STUDENT -- Last_name;
    STUDENT -- Student_id;
    STUDENT -- Ssn;
    STUDENT -- Birth_date;
    STUDENT -- Sex;
    STUDENT -- Class;
    STUDENT -- Major;
    STUDENT -- Minor;
    STUDENT -- Degree;
    
    subgraph address{
        node [shape=box]; CURRENT_ADDRESS;
        node [shape=ellipse]; Current_address; Current_phone; 
        CURRENT_ADDRESS -- Current_address;
        CURRENT_ADDRESS -- Current_phone;
    
        node [shape=box]; PERMANENT_ADDRESS; 
        node [shape=ellipse]; Permanent_address; Permanent_phone; State; Zip;
        PERMANENT_ADDRESS -- Permanent_address;
        PERMANENT_ADDRESS -- Permanent_phone;
        PERMANENT_ADDRESS -- State;
        PERMANENT_ADDRESS -- Zip;
    }

    subgraph department{
        node [shape=box]; DEPARTMENT;
        node [shape=ellipse]; Department_name; Department_code; Office_number; Office_phone; College;
        DEPARTMENT -- Department_name;
        DEPARTMENT -- Department_code;
        DEPARTMENT -- Office_number;
        DEPARTMENT -- Office_phone;
        DEPARTMENT -- College;
    }
    
    subgraph course{
        node [shape=box]; COURSE;
        node [shape=ellipse] Course_name; Description; Course_number; Hours; Level; Offering_department_code; 
        COURSE -- Course_name;
        COURSE -- Description;
        COURSE -- Course_number;
        COURSE -- Hours;
        COURSE -- Level;
        COURSE -- Offering_department_code;
    }
    
    subgraph section{
        node [shape=box]; SECTION;
        node [shape=ellipse] Instructor; Semester; Year; Course; Section_number;
        SECTION -- Instructor;
        SECTION -- Semester;
        SECTION -- Year;
        SECTION -- Course;
        SECTION -- Section_number;
    }
    
    subgraph grades{
        node [shape=box]; GRADE_REPORT;
        node [shape=ellipse]; Grade_student_id; Grade_section_id; Grade_in_letter; Grade_in_numeric;
        GRADE_REPORT -- Grade_student_id;
        GRADE_REPORT -- Grade_section_id;
        GRADE_REPORT -- Grade_in_letter;
        GRADE_REPORT -- Grade_in_numeric;
    }
    
    
    node [shape=diamond] LIVES_IN;
    STUDENT -- LIVES_IN [label=1];
    LIVES_IN -- CURRENT_ADDRESS [label=1 lhead=address];
    LIVES_IN -- PERMANENT_ADDRESS [label=1 lhead=address];
    
    node [shape=diamond] TAKES;
    STUDENT -- TAKES [label=1];
    TAKES -- SECTION [label=N];
    
    node [shape=diamond] BELONGS;
    SECTION -- BELONGS [label=N];
    BELONGS -- COURSE [label=1];
    
    node [shape=diamond] HOLDS;
    STUDENT -- HOLDS [label=1];
    HOLDS -- GRADE_REPORT [label=N];
    
    node [shape=diamond] HAS;
    COURSE -- HAS [label=N];
    HAS -- DEPARTMENT [label=1];
    
	label = "\n\nEntity Relation Diagram";
	fontsize=20;
}
```

:::info
我知道圖很小(物件太多了)，但我已經盡全力將圖形畫好了。
有些 edge 有 overlapping 的問題，但我不知道怎麼移動特定 edge 讓他不用穿圖。
使用的套件是 Graphviz，在Hackmd上作圖用這個還算不錯。
:::
## 7.23

:::info
Consider the ER diagram shown in Figure 7.21 for part of a BANK database. Each bank can have multiple branches, and each branch can have multiple accounts and loans. 
:::

![](https://hackmd.io/_uploads/rJuNpYEU3.png)

a. List the strong (nonweak) entity types in the ER diagram.

`BANK`, `ACCOUNT`, `LOAN`, `CUSTOMER`

---

b. Is there a weak entity type? If so, give its name, partial key, and identifying relationship.

* `BANK_BRANCH`
    * partial key: `Branch_no`
    * identifying relationship: `BRANCHES`

---

c. What constraints do the partial key and the identifying relationship of the weak entity type specify in this diagram?

在具有 `BANK` 的 `Code` 與 `BANK_BRANCH` 的 `Branch_no` 情況下才能 uniquely indentify 每個銀行的分部。

---

d. List the names of all relationship types, and specify the (min, max) constraint on each participation of an entity type in a relationship type. Justify your choices.

* `BRANCHES`: `BANK` 的 (min, max) 為 (1, 1)；`BANK_BRANCH` 的 (min, max) 為 (1, n)。
    * 一個銀行名稱對應到許多分部
* `ACCTS`：`BANK_BRANCH` 的 (min, max) 為 (1, 1)；`ACCOUNT` 的 (min, max) 為 (0, n)。
    * 一個分部底下可能沒有帳戶或是有許多帳戶
* `LOANS`：`BANK_BRANCH` 的 (min, max) 為 (1, 1)；`LOAN` 的 (min, max) 為 (0, n)。
    * 一個分部底下可能沒有借貸或是有許多借貸
* `A_C`：`ACCOUNT` 的 (min, max) 為 (1, n)；`CUSTOMER` 的 (min, max) 為 (1, n)。
    * 一個人在一間銀行底下應該只有一個帳戶，但是有 n 個人就應該有 n 個帳戶
* `L_C`：`LOAN` 的 (min, max) 為 (0, n)；`CUSTOMER` 的 (min, max) 為 (1, n)。
    * 一個人在一間銀行底下可以有多次借貸，更別提許多人了

---

e. List concisely the user requirements that led to this ER schema design.

這個設計是從銀行的角度看過去，一個銀行在各地有著許多分部，每個分部底下都有各自的帳戶資料及借貸資料，分別對應到不同人。

---

f. Suppose that every customer must have at least one account but is restricted to at most two loans at a time, and that a bank branch cannot have more than 1,000 loans. How does this show up on the (min, max) constraints? 

* 如果每個帳戶都沒有借貸的話，可以得到 `CUSTOMER` 的 (min, max) 為 (0, Infinity)，而 `LOAN` 的 (min, max) 為 (0, 0)。
* 如果每個帳戶都借貸一次的話，可以得到 `CUSTOMER` 的 (min, max) 為 (0, 1000)，而 `LOAN` 的 (min, max) 為 (0, 1000)。
* 如果每個帳戶都借貸兩次的話，可以得到 `CUSTOMER` 的 (min, max) 為 (0, 500)，而 `LOAN` 的 (min, max) 為 (0, 1000)。

從以上的討論可以得出：`LOAN` 的 (min, max) 為 (0, 1000)，但是對於 `CUSTOMER` 則是不明，會根據實際的借貸筆數狀況而影響實際 (min, max)。

###### tags: `1112_courses` `database`