SDSC5003 - Question of Assignment 2
#assignment #sdsc5003
原文
NOTE: The university policy on academic dishonesty and plagiarism (cheating) will be taken very seriously in this course. Everything submitted should be your own writing or coding. You must not let other students copy your work. Discussions of the assignment are okay, e.g. understanding the concepts involved. This assignment is an individual one. Upload your work as a single archive file with name A2-XXXX-YYYY.zip where XXXX is your name and YYYY is your student ID. Remember to include all files in the archive file.
Part I. Relational Algebra (35 points)
Consider the following relational schema:
-
Emp(eid: integer, ename: string, age: integer, salary: real) -
Works(eid: integer, did: integer, pct_time: integer) -
Dept(did: integer, dname: string, budget: real, managerid: integer)
Write the following queries in relational algebra:
a. Print the names and ages of each employee who works in both the Hardware department and the Software department. (7 points)
b. Print the name of each employee whose salary exceeds the budget of all the departments that he or she works in. To illustrate, if the employee works in 3 departments, whose budgets are 1000, 2000 and 3000 respectively, then the maximum budget of all the departments that he/she works in is 3000. (7 points)
c. Find the ename, age of managers who manage only departments with budgets greater than $1 million. (7 points)
d. Find the enames of managers who manage the departments with the greatest budgets. To illustrate, suppose there are three departments like this: department 1 with a budget of $1 million, department 2 with a budget of $2 million, department 3 with a budget of $2 million. Then departments 2 and 3 have the greatest budgets. (14 points)
Part II. SQL Queries (40 points)
Copy all SQL queries into one SQL file called part2.sql. For each query, please provide the comment as -- part2a for the first query.
All screenshots should be submitted together as a single PDF file called part2.pdf.
Upload your submissions as a single archive file called part2.zip.
Consider the relational schema for company in Part I, write SQL queries for each request in Part I and one extra request in the following. Your queries should be executable in SQLite.
In this part, you also need to use the “A2.db” (please download “A2.db” from course files) database in DB Browser for SQLite to give a screenshot for each query’s result.
a. Corresponding to Part I-a (8 points)
b. Corresponding to Part I-b (6 points)
c. Corresponding to Part I-c (7 points)
d. Corresponding to Part I-d (9 points)
e. For each department with more than 20 full-time-equivalent employees (i.e., the total pct_time of the part-time and full-time employees add up to at least 2000 hrs, assuming that the pct_time of a full-time employee is 100 hrs), print the did together with the number of employees that work in that department. (10 points)
Part III. Design Theory (25 points)
Suppose we have a relation schema with Functional Dependencies (FDs):
-
-
-
-
-
List all candidate keys of . (6 points)
-
The main requirement for BCNF means that the lefthand-side of an FD is a superkey. Do any FDs listed above violate the main requirement? Please explain for each FD. (6 points)
-
The secondary requirement for 3NF means that for an FD , is a part of a candidate key. Do any FD’s listed above violate the secondary requirement? Please explain for each FD. (4 points)
-
Give a BCNF decomposition of that is lossless and has as few tables as possible. (9 points)
注意: 本课程将严肃对待大学关于学术不端行为和抄袭(作弊)的政策。所有提交的内容必须是您自己的写作或编码。您不得让其他学生抄袭您的作业。可以讨论作业内容,例如理解相关概念。本作业为个人作业。请将作业打包为单个压缩文件上传,文件名为 A2-XXXX-YYYY.zip,其中 XXXX 是您的姓名,YYYY 是您的学号。请确保压缩文件中包含所有必需文件。
第一部分:关系代数(35分)
考虑以下关系模式:
-
Emp(eid: integer, ename: string, age: integer, salary: real) -
Works(eid: integer, did: integer, pct_time: integer) -
Dept(did: integer, dname: string, budget: real, managerid: integer)
用关系代数编写以下查询:
a. 列出同时在硬件部门(Hardware)和软件部门(Software)工作的员工的姓名和年龄。(7分)
b. 列出薪资超过其所在所有部门预算的员工的姓名。例如,若某员工在3个部门工作,部门预算分别为1000、2000和3000,则其薪资需超过3000。(7分)
c. 查找仅管理预算超过100万美元的部门的经理的姓名和年龄。(7分)
d. 查找管理预算最高部门的经理的姓名。例如,假设有3个部门:部门1预算100万美元,部门2和部门3预算均为200万美元,则部门2和3的预算最高。(14分)
第二部分:SQL查询(40分)
将所有SQL查询保存在名为 part2.sql 的SQL文件中。每个查询前需添加注释,如第一题注释为 -- part2a。
所有查询结果的截图需合并为一个PDF文件 part2.pdf。
将提交内容打包为单个压缩文件 part2.zip。
基于第一部分的关系模式,用SQL编写以下查询(查询需在SQLite中可执行),并使用提供的 A2.db 数据库验证结果:
a. 对应第一部分的a题(8分)
b. 对应第一部分的b题(6分)
c. 对应第一部分的c题(7分)
d. 对应第一部分的d题(9分)
e. 对于每个全职等效员工数超过20人的部门(即员工总工作时间≥2000小时,假设全职员工工作时间为100小时),输出部门编号(did)及该部门员工数。(10分)
第三部分:设计理论(25分)
设关系模式 满足以下函数依赖(FDs):
-
-
-
-
-
列出的所有候选键。(6分)
-
BCNF的主要要求是FD的左部必须是超键。上述FD是否违反该要求?请逐一说明。(6分)
-
3NF的次要要求是对于FD ,必须是候选键的一部分。上述FD是否违反该要求?请逐一说明。(4分)
-
给出的一个无损且表数最少的BCNF分解。(9分)
第一部分:关系代数(35分)
a. 列出同时在硬件部门(Hardware)和软件部门(Software)工作的员工的姓名和年龄。(7分)
b. 列出薪资超过其所在所有部门预算的员工的姓名。(7分)
c. 查找仅管理预算超过100万美元的部门的经理的姓名和年龄(7分)
d. 查找管理预算最高部门的经理的姓名(14分)
第二部分:SQL查询(40分)
1 | -- part2a |

第三部分:设计理论(25分)
关系模式 满足函数依赖(FDs):
1. 列出 的所有候选键。(6分)
计算属性闭包:
-
(由FD3和FD4)
-
(由FD2),然后 (由FD3和FD4)
-
(由FD1),然后 (由FD3和FD4)
-
(由FD1和FD2)
2. BCNF的主要要求是FD的左部必须是超键。上述FD是否违反该要求?请逐一说明。(6分)
BCNF要求:对每个非平凡FD , 必须是超键。
-
FD1 : 不是超键(,不包含所有属性)。违反BCNF。
-
FD2 : 不是超键(,不包含所有属性)。违反BCNF。
-
FD3 : 是候选键(超键)。不违反BCNF。
-
FD4 : 是候选键(超键)。不违反BCNF。
3. 3NF的次要要求是对于FD , 必须是候选键的一部分。上述FD是否违反该要求?请逐一说明。(4分)
-
FD1 : 不是超键,但 是主属性(包含于候选键 和 中)。不违反3NF。
-
FD2 : 不是超键,但 是主属性(包含于候选键 和 中)。不违反3NF。
-
FD3 : 是超键。不违反3NF。
-
FD4 : 是超键。不违反3NF。
所有FD均满足3NF要求,无违反。
4. 给出 的一个无损且表数最少的BCNF分解。(9分)
由于FD1和FD2违反BCNF,需分解。最小表数为3(因为属性间依赖关系无法用2个表满足BCNF)。以下是一个无损连接分解:
-
,FD ,候选键 (BCNF)。
-
,FD ,候选键 (BCNF)。
-
,无非平凡FD(除键依赖),候选键 (BCNF)。
