Try   HackMD

資料庫作業四

  • 姓名:張議隆
  • 學號:F74082125
TOC

6.16

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.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

用 Latex 打這東西真痛苦,每個文字都要用 \text{} 包住,符號也得額外查詢。

a. Retrieve the names of all employees in department 5 who work more than 10 hours per week on the ProductX project.

ProjectXσPname="ProductX"(PROJECT)WorksOn_XProjectXPnumber=PnoWORKS_ONX_10_moreσHours>10(WorksOn_X)Employee_10_XX_10_moreEssn=SsnEMPLOYEEAnswerπFname, Lname(Employee_10_X)


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

Full_DependentEMPLOYEESsn=EssnDEPENDENTSame_DependentσFname=Dependent_name(Full_Dependent)AnswerπFname, Lname(Same_Dependent)


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

Full_Employee_and_SupervisorEMPLOYEESsn=Super_ssnEMPLOYEEF_W_SsnπSsn(σFname="Franklin", Lname="Wong"(EMPLOYEE))AnswerπFname, Lname(σSuper_ssn=F_W_Ssn(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,只是我真的打不出那個字了。

Project_and_WorkPROJECTPnumber=PnoWORKS_ONHoursPnameFSUM Hours(Project_and_Work)AnswerπPname, Sum_hours(Hours)


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

All_Project_NumberρPno(πPnumber(PROJECT))Ssn_With_All_ProjectWORKS_ON÷All_Project_NumberSelected_EmployeeσSsn=Ssn_With_All_Project(EMPLOYEE)AnswerπFname, Lname(Selected_Employee)


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

All_SsnπSsn(EMPLOYEE)Ssn_With_ProjectρSsn(πEssn(WORKS_ON))Ssn_Without_ProjectAll_SsnSsn_With_ProjectAnswerπFname, Lname(σSsn=Ssn_Without_Project(EMPLOYEE))


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

Salary_And_DnoπSalary,Dno(EMPLOYEE)Average_Salary_per_DnoDnoFAVERAGE Salary(Salary_And_Dno)Department_And_AVG_SalaryDEPARTMENTDnumber=DnoAverage_Salary_per_DnoAnswerπDname, Average_salary(Department_And_AVG_Salary)


h. Retrieve the average salary of all female employees.

Female_EmployeeσSex="F"(EMPLOYEE)AnswerSexFAVERAGE Salary(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.

Project_In_HoustonσPlocation="Houston"(PROJECT)Department_Not_In_HoustonσDlocation!="Houston"(DEPARTMENT)JoinProject_In_HoustonPlocation=DlocationDepartment_Not_In_HoustonSelected_PnoπPnumber(Join)Selected_SsnσPno=Pnumber(Selected_Pno)AnswerπFname,Lname,Address(σSsn=Selected_Ssn(EMPLOYEE))


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

Manager_SsnρSsn(πMgr_ssn(DEPARTMENT))Ssn_With_DependentρSsn(πEssn(DEPENTDENT))Manager_Ssn_Without_DependentManager_SsnSsn_With_DependentAnswerπLname(σSsn=Manager_Ssn_Without_Dependent(EMPLOYEE))

6.18

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:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

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

That_Book_IDσTitle="The Lost Tribe"(BOOK)That_Branch_IDσBranch_name="Sharpstown"(LIBRARY_BRANCH)AnswerπNo_of_copies(σBook_id=That_Book_ID, Branch_id=That_Branch_ID(BOOK_COPIES))


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

That_Book_IDσTitle="The Lost Tribe"(BOOK)AnswerπNo_of_copies(σBook_id=That_Book_ID(BOOK_COPIES))


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

All_BorrowersπCard_no(BORROWER)All_Borrowers_Has_BooksπCard_no(BOOK_LOANS)Borrowers_No_BookAll_BorrowersAll_Borrowers_Has_BooksAnswerπName(σ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" 代指應該要選的那天。

That_Branch_IDσBranch_name="Sharpstown"(LIBRARY_BRANCH)Due_TodayσBranch_id=That_Branch_ID, Due_date="today"(BOOK_LOANS)Due_Book_idπBook_id(Due_Today)Due_Book_TitleπTitle(σBook_id=Due_Book_id(BOOK))Borrowers_noπCard_no(Due_Today)Borrower_InfoπName, Address(σCard_no=Boorrowers_no(BORROWER))AnswerDue_Book_TitleBorrower_Info


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

Loan_Count_by_Branch_idBranch_idFCOUNT (Branch_id)(BOOK_LOANS)AnswerπBranch_name, Count_branch_id(LIBRARY_BRANCH  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.

Group_by_Loan_CountCard_noFCOUNT (Card_no)(BOOK_LOANS)Borrower_SatisfyingσCount_card_no > 5(Group_by_Loan_Count)AnswerπName, Address, Count_card_no(BORROWER  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.

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

Those_Book_IDσAuthor_name="Stephen King"(BOOK_AUTHORS)Those_BooksσBook_id=Those_Book_ID(BOOK)That_Branch_IDσBranch_name="Central"(LIBRARY_BRANCH)Books_SatisfyingσBranch_id=That_Branch_ID(Those_Books)Book_id_SatisfyingπBookid(Books_Satisfying)Copies_SatisfyingσBook_id=Book_id_Satisfying(BOOK_COPIES)AnswerπTitle, No_of_copies(Books_Satisfying  Copies_Satisfying)

7.16

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:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

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 為:(我不知道怎麼在圖形內加上底線,因此文字敘述)

  • STUDENTStudent_id, Ssn
  • DEPARTMENTDepartment_code
  • SECTIONSection_number

另外 CURRENT_ADDRESSPERMANENT_ADDRESSGRADE_REPORT 都屬於 weak entity ,沒畫上去是因為沒有 double diamond 以及 double box 這類型的 shape
其他沒特別提到的限制等等我在圖形內應該都表示了,我盡可能地根據題目要求作圖。







ER_diagram

Entity Relation Diagram


STUDENT

STUDENT



First_name

First_name



STUDENT--First_name




Last_name

Last_name



STUDENT--Last_name




Student_id

Student_id



STUDENT--Student_id




Ssn

Ssn



STUDENT--Ssn




Birth_date

Birth_date



STUDENT--Birth_date




Sex

Sex



STUDENT--Sex




Class

Class



STUDENT--Class




Major

Major



STUDENT--Major




Minor

Minor



STUDENT--Minor




Degree

Degree



STUDENT--Degree




LIVES_IN

LIVES_IN



STUDENT--LIVES_IN

1



TAKES

TAKES



STUDENT--TAKES

1



HOLDS

HOLDS



STUDENT--HOLDS

1



Current_address

Current_address



Current_phone

Current_phone



Permanent_address

Permanent_address



Permanent_phone

Permanent_phone



CURRENT_ADDRESS

CURRENT_ADDRESS



CURRENT_ADDRESS--Current_address




CURRENT_ADDRESS--Current_phone




PERMANENT_ADDRESS

PERMANENT_ADDRESS



PERMANENT_ADDRESS--Permanent_address




PERMANENT_ADDRESS--Permanent_phone




State

State



PERMANENT_ADDRESS--State




Zip

Zip



PERMANENT_ADDRESS--Zip




DEPARTMENT

DEPARTMENT



Department_name

Department_name



DEPARTMENT--Department_name




Department_code

Department_code



DEPARTMENT--Department_code




Office_number

Office_number



DEPARTMENT--Office_number




Office_phone

Office_phone



DEPARTMENT--Office_phone




College

College



DEPARTMENT--College




COURSE

COURSE



Course_name

Course_name



COURSE--Course_name




Description

Description



COURSE--Description




Course_number

Course_number



COURSE--Course_number




Hours

Hours



COURSE--Hours




Level

Level



COURSE--Level




Offering_department_code

Offering_department_code



COURSE--Offering_department_code




HAS

HAS



COURSE--HAS

N



SECTION

SECTION



Instructor

Instructor



SECTION--Instructor




Semester

Semester



SECTION--Semester




Year

Year



SECTION--Year




Course

Course



SECTION--Course




Section_number

Section_number



SECTION--Section_number




BELONGS

BELONGS



SECTION--BELONGS

N



GRADE_REPORT

GRADE_REPORT



Grade_student_id

Grade_student_id



GRADE_REPORT--Grade_student_id




Grade_section_id

Grade_section_id



GRADE_REPORT--Grade_section_id




Grade_in_letter

Grade_in_letter



GRADE_REPORT--Grade_in_letter




Grade_in_numeric

Grade_in_numeric



GRADE_REPORT--Grade_in_numeric




LIVES_IN--CURRENT_ADDRESS

1



LIVES_IN--PERMANENT_ADDRESS

1



TAKES--SECTION

N



BELONGS--COURSE

1



HOLDS--GRADE_REPORT

N



HAS--DEPARTMENT

1



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

7.23

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.

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?

在具有 BANKCodeBANK_BRANCHBranch_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)。
    • 一個銀行名稱對應到許多分部
  • ACCTSBANK_BRANCH 的 (min, max) 為 (1, 1);ACCOUNT 的 (min, max) 為 (0, n)。
    • 一個分部底下可能沒有帳戶或是有許多帳戶
  • LOANSBANK_BRANCH 的 (min, max) 為 (1, 1);LOAN 的 (min, max) 為 (0, n)。
    • 一個分部底下可能沒有借貸或是有許多借貸
  • A_CACCOUNT 的 (min, max) 為 (1, n);CUSTOMER 的 (min, max) 為 (1, n)。
    • 一個人在一間銀行底下應該只有一個帳戶,但是有 n 個人就應該有 n 個帳戶
  • L_CLOAN 的 (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