Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
0/34 Questions Answered
Midterm 2
Q1 Preliminaries1 Point
Instructions
The allowed time for the exam is 60 minutes (50 minutes plus a 10 minute
technology buffer). Be sure to pay attention to time and budget your time
accordingly!
The exam is open pre-prepared cheat sheet, open book, open notes, open
web browser, and even open MySQL. You are just not allowed to communicate
with or otherwise interact with other students (or friends) during the course of
the exam, and this includes your HW brainstorming buddy. This exam is to be a
solo effort!
Read each question carefully, in its entirety, and then answer each part of the
question.
STUDENT NAME
Search students by name or email… Ë
If you don't understand something, make your best guess; if you find
ambiguities in a question, note the interpretation that you are taking.
Acknowledgement: I certify that I am taking this exam myself, on my own, with
honesty and integrity, without interaction with others during the exam, and
without having obtained any information about the exam's content from others
prior to taking it.
Reference Data
The exam questions that involve query-writing and data will all be based on the
following HR schema about employees, departments, work assignments, and
managers. (You will need to scroll back here periodically to answer some of the
questions.)
CREATE SCHEMA HR; USE HR; CREATE TABLE Emp ( eid integer PRIMARY KEY, ename varchar(50), age integer, salary integer, CHECK ((eid MOD 2) <> 0)); CREATE TABLE Dept ( did integer PRIMARY KEY, dname varchar(50), budget integer, eid integer, -- dept manager's id ename varchar(50), -- dept manager's name FOREIGN KEY (eid) REFERENCES Emp(eid)); CREATE TABLE Works ( eid integer, did integer, pcttime integer, PRIMARY KEY (eid, did), FOREIGN KEY (eid) REFERENCES Emp(eid)); INSERT INTO Emp (eid, ename, age, salary) VALUES (1, 'Dustin', 50, 60000), (3, 'Emily', 30, 30000),
Trueý
Falseý
(5, 'John', 20, 20000), (7, 'Win', NULL, 45000), (9, 'Arthur Jr', 22, 40000); INSERT INTO Dept (did, dname, budget, eid, ename) VALUES (101, 'Accounting', 50000, 1, 'Dustin'), (102, 'Sales', 50000, 3, 'Em'), (103, 'Legal', 100000, 7, 'Win'); INSERT INTO Works (eid, did, pcttime) VALUES (1, 101, 80), (1, 102, 20), (3, 102, 100), (5, 102, 50), (7, 101, 33), (7, 102, 33), (7, 103, 33), (9, 104, 100);
Save Answer
Q2 Short and Two the Point(s)30 Points
For each of the following questions, you should
SELECT best(answer) FROM question LIMIT 1.
Q2.12 Points
A query language is said to be relationally complete if it has at least the
expressive power of:
Save Answer
SQLý
the relational algebraý
the relational calculusý
Pythoný
Q2.22 Points
Join is a core (necessary) operation in the relational algebra.
Save Answer
Q2.32 Points
Union is a core (necessary) operation in the relational algebra.
Save Answer
Q2.42 Points
Division is a core (necessary) operation in the relational algebra.
Save Answer
Q2.52 Points
Based on the current HR reference tables, what is the cardinality of the result of
the relational algebra query: Emp x Dept
Trueý
Falseý
Trueý
Falseý
Trueý
Falseý
Save Answer
Q2.62 Points
Based on the HR reference data, what is the cardinality of the result of the
relational calculus query: { e | (e ∉ Emp) ∧ e.ename = 'Win' }
Save Answer
Q2.72 Points
What would be the most appropriate MySQL mechanism to employ to ensure
that the budget of a Dept is always at least 10000?
0ý
3ý
5ý
15ý
∞ý
0ý
1ý
5ý
11ý
∞ý
Save Answer
Q2.82 Points
What would be the most appropriate MySQL mechanism to employ to ensure
that the budget of a Dept is always sufficient to cover its manager's salary?
Save Answer
Q2.92 Points
What would be the most appropriate MySQL mechanism to employ to ensure
that no two employees who work in a given department have the same name?
FOREIGN KEY CONSTRAINTý
CHECK CONSTRAINTý
UNIQUE CONSTRAINTý
TRIGGERý
VIEWý
FOREIGN KEY CONSTRAINTý
CHECK CONSTRAINTý
UNIQUE CONSTRAINTý
TRIGGERý
VIEWý
Save Answer
Q2.102 Points
What would be the most appropriate MySQL mechanism to employ to enable
the secretarial staff to see the ids and names of employees, but not their ages
or salaries, along with the ids and names of the departments that they work in?
Save Answer
Q2.112 Points
What would be the most appropriate MySQL mechanism to employ to ensure
that no two employees in the company make exactly the same amount of
money?
FOREIGN KEY CONSTRAINTý
CHECK CONSTRAINTý
UNIQUE CONSTRAINTý
TRIGGERý
VIEWý
FOREIGN KEY CONSTRAINTý
CHECK CONSTRAINTý
UNIQUE CONSTRAINTý
TRIGGERý
VIEWý
Save Answer
Q2.122 Points
What would the following MySQL query return given the current HR reference
tables?
SELECT (e1.age + e2.age) / 2FROM Emp e1, Emp e2 WHERE e1.eid = 7 AND e2.eid = 9;
Save Answer
Q2.132 Points
What would the following MySQL query return given the current HR reference
tables?
SELECT AVG(e.age) FROM Emp e WHERE e.eid >= 7;
FOREIGN KEY CONSTRAINTý
CHECK CONSTRAINTý
UNIQUE CONSTRAINTý
TRIGGERý
VIEWý
0ý
11ý
22ý
NULLý
Save Answer
Q2.142 Points
Consider the following MySQL aggregate query:
SELECT e.age, e.name, COUNT(*) FROM Emp e GROUP BY e.age HAVING COUNT(*) > 1;
Is this a legal SQL query?
Save Answer
Q2.152 Points
Consider the following MySQL UPDATE query:
UPDATE Emp e SET e.eid = e.eid + 1WHERE e.age > 21;
0ý
11ý
22ý
NULLý
Yesý
Noý
Look at the Emp table's CREATE TABLE statement again. Will this update query
succeed?
Save Answer
Q3 Taking an Equivalence Class30 Points
Consider once again the tables of our HR reference database:
CREATE SCHEMA HR; USE HR; CREATE TABLE Emp ( eid integer PRIMARY KEY, ename varchar(50), age integer, salary integer, CHECK ((eid MOD 2) <> 0)); CREATE TABLE Dept ( did integer PRIMARY KEY, dname varchar(50), budget integer, eid integer, -- dept manager's id ename varchar(50), -- dept manager's name FOREIGN KEY (eid) REFERENCES Emp(eid)); CREATE TABLE Works ( eid integer, did integer, pcttime integer, PRIMARY KEY (eid, did), FOREIGN KEY (eid) REFERENCES Emp(eid));
For each of the following problems, you will be given a relational query along
with a set of additional queries. For each problem, indicate which query or
queries in the additional query set are equivalent to the given query. (Note that
equivalent means that it will produce the same result as the original query for
Yesý
Noý
all possible database states permitted by the HR schema's CREATE TABLE
statements.) Note: Be sure to consider each query carefully!
Q3.16 Points
Here is the given query:
SELECT DISTINCT d.dname, e.ename FROM Emp e JOIN Dept d ON e.eid = d.eid;
Select the equivalent query or queries (if any):
Save Answer
Q3.26 Points
Here is the given query:
(π eid, did (Works)) ÷ (π did ( σ budget = 50000 (Dept)))
Select the equivalent query or queries (if any):
π dname, ename (Emp ⨝ Dept)
SELECT DISTINCT d.dname, d.ename FROM Dept d;
{ t(dname, ename) | ∃e ∈ Emp (t.ename = e.ename ∧ ∃d ∈ Dept (e.eid =
d.eid ∧ t.dname = d.dname) }
Save Answer
Q3.36 Points
Here is the given query:
SELECT DISTINCT ename FROM Emp e WHERE e.salary = (SELECT MAX(salary) FROM Emp);
Select the equivalent query or queries (if any):
Save Answer
Q3.46 Points
{ t(eid) | ∃e ∈ Emp (t.eid = e.eid ∧ ∀d ∈ Dept (d.budget = 50000 (∃w ∈
Works (w.eid = e.sid ∧ d.bid = w.bid))))}
{ t(eid) | ∃e ∈ Emp (t.eid = e.eid ∧ ∀d ∈ Dept (d.budget = 50000 ∧ (∃w ∈
Works (w.eid = e.sid ∧ d.bid = w.bid))))}
{ t(eid) | ∃e ∈ Emp (t.eid = e.eid ∧ ¬(∀d ∈ Dept (d.budget ≠ 50000 ∨ (∃w ∈
Works (w.eid = e.sid ∧ d.bid = w.bid)))))}
{ t(ename) | ∃e ∈ Emp (t.ename = e.ename ∧ ∄m ∈ Emp (e.salary <
m.salary) }
π ename (Emp ⨝ salary ≥ salary2 (ρ eid2←eid, ename2←ename,
age2←age, salary2←salary (Emp)))
SELECT DISTINCT ename FROM Emp e WHERE 0 = (SELECT COUNT(*)
FROM Emp p WHERE p.salary > e.salary);
Here is the given query:
π eid, ename (σ pcttime < 100 (Works ⨝ Emp))
Select the equivalent query or queries (if any):
Save Answer
Q3.56 Points
Here is the given query:
{t(ename) | ∃e ∈ Emp (t.ename = e.ename ∧ e.salary < 25000 ∧ e.age < 25)}
Select the equivalent query or queries (if any):
Save Answer
SELECT e.eid, e.ename FROM Works w, Emp e WHERE w.pcttime < 100
AND w.eid = e.eid;
SELECT e.eid, e.ename FROM Emp e WHERE e.eid in (SELECT eid
FROM Works WHERE pcttime < 100);
SELECT DISTINCT e.eid, e.ename FROM Works w LEFT OUTER JOIN
Emp e ON w.eid = e.eid WHERE w.pcttime < 100;
SELECT DISTINCT ename FROM Emp WHERE salary < 25000 OR age <
25;
(π ename (σ age < 25 (Emp))) ∩ (π ename (σ salary < 25000 (Emp)))
SELECT DISTINCT ename FROM Emp e WHERE age < 25 AND eid IN
(SELECT eid FROM Emp WHERE salary < 25000);
Q4 Query Me This, Batman18 Points
It's time to check the box that says "I'm tired of checking boxes!" In this
problem you will actually write or "hand execute" a couple of queries of your
own! Exciting, right?
Note: When asked to show queries' answers below, feel free to show them in a
CSV-like format. For example, suppose the given query is:
SELECT * FROM Dept WHERE budget < 100000;
In this case you could denote the query's answer as follows (since Gradescope
won't let you hand-draw pictures of tables with rows and columns):
did, dname, budget, eid, ename 101, 'Accounting', 50000, 1, 'Dustin' 102, 'Sales', 50000, 3, 'Em'
In preparation for what follows, here's a repeat of the HR database's tables and
their current contents:
CREATE SCHEMA HR; USE HR; CREATE TABLE Emp ( eid integer PRIMARY KEY, ename varchar(50), age integer, salary integer, CHECK ((eid MOD 2) <> 0)); CREATE TABLE Dept ( did integer PRIMARY KEY, dname varchar(50), budget integer, eid integer, -- dept manager's id ename varchar(50), -- dept manager's name FOREIGN KEY (eid) REFERENCES Emp(eid)); CREATE TABLE Works ( eid integer, did integer, pcttime integer,
PRIMARY KEY (eid, did), FOREIGN KEY (eid) REFERENCES Emp(eid)); INSERT INTO Emp (eid, ename, age, salary) VALUES (1, 'Dustin', 50, 60000), (3, 'Emily', 30, 30000), (5, 'John', 20, 20000), (7, 'Win', NULL, 45000), (9, 'Arthur Jr', 22, 40000); INSERT INTO Dept (did, dname, budget, eid, ename) VALUES (101, 'Accounting', 50000, 1, 'Dustin'), (102, 'Sales', 50000, 3, 'Em'), (103, 'Legal', 100000, 7, 'Win'); INSERT INTO Works (eid, did, pcttime) VALUES (1, 101, 80), (1, 102, 20), (3, 102, 100), (5, 102, 50), (7, 101, 33), (7, 102, 33), (7, 103, 33), (9, 104, 100);
Q4.14 Points
Consider the following SQL query Q:
WITH -- define some useful building blocks for Q WorksInDept (eid, pct, did, dname, budget, mgrid, mgrname) AS (SELECT e.eid, w.pcttime, d.did, d.dname, d.budget, d.eid, d.ename FROM Emp e LEFT OUTER JOIN Works w ON e.eid = w.eid LEFT OUTER JOIN Dept d ON w.did = d.did), ManagesDept (eid, ename, mgrname, did, dname, budget) AS (SELECT e.eid, e.ename, d.ename, d.did, d.dname, d.budget FROM Emp e LEFT OUTER JOIN Dept d ON e.eid = d.eid) SELECT ... -- rest of query Q goes here
Show the answer that would result from executing this query if the rest of query
Q is:
SELECT * FROM WorksInDept;
Enter your answer here
Save Answer
Q4.22 Points
What problem or issue does this query reveal with the schema (the CREATE
TABLE statements) or the current base tables' contents (the data in Emp, Dept,
or Works)?
Enter your answer here
Save Answer
Q4.34 Points
Now show the answer that would result from executing the above query if the
rest of query Q is:
SELECT * FROM ManagesDept;
Enter your answer here
Save Answer
Q4.42 Points
And what problem or issue does this query reveal with the current schema or
base table contents?
Enter your answer here
Save Answer
Q4.56 Points
Show what query Q's SELECT ending should be if your task is, for each
employee, to print their eids along with the dids and budgets of the
departments that they work in but do not manage. I.e., starting with
SELECT ... , write the remainder of the query. The FROM clause(s) in your
answer should only reference the temporary tables introduced in the WITH
clause (and should not need or use any of the base tables):
Enter your answer here
Save Answer
Q5 Overcoming SELECTion Bias20 Points
It's time to move past SELECT statements and put some of SQL's more
advanced language features to use on the HR database.
Q5.16 Points
Complete the following CREATE VIEW statement to aid the HR analysts in
keeping an eye on age-related salary equity for employees. Anticipating a
future with more employees, they want to be prepared to keep track of how
many employees there are of each age and what the age groups' salary
statistics are.
CREATE VIEW EmpStats (agegroup, numemps, lowsal, avgsal, highsal) AS
Enter your answer here
Save Answer
Q5.25 Points
One of the senior IT staff in the IT department spotted one of the problems that
you identified in problem 4 and has proposed a PROCEDURE-based cure. From
now on she wants applications to use this procedure to assign managers to
departments:
DELIMITER // CREATE PROCEDURE NewManager (IN newdid integer, IN neweid integer) BEGIN UPDATE Dept SET eid = neweid, ename = (SELECT ename FROM Emp WHERE eid = neweid) WHERE did = newdid; END;
Show what the effect would be on the Dept table of running the following SQL
statement given the existing data:
CALL NewManager(102, 3);
Enter your answer here
Save Answer
Q5.31 Point
An overeager new hire in the HR IT department spotted one of the problems
that you identified in problem 4 and has proposed the following TRIGGER-
based approach to its prevention:
DELIMITER // CREATE Trigger HackItUp AFTER INSERT ON Works FOR EACH ROWBEGIN IF NEW.did NOT IN (SELECT did FROM Dept) THEN SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Warning: Bad assignment!'; END IF; END;
Show what each of the following three SQL statements would produce after
this trigger has been defined (starting with the existing data) if they are
executed one after another in sequence:
Here's the first statement in the sequence:
SELECT COUNT(*) AS prior FROM Works;
Enter your answer here
Save Answer
Q5.42 Points
Here's the next statement in the sequence:
INSERT INTO Works (eid, did, pcttime) VALUES (7, 104, 1);
Enter your answer here
Save Answer
Q5.51 Point
And here's the last statement in the sequence:
SELECT COUNT(*) AS after FROM Works;
Enter your answer here
Save Answer
Q5.63 Points
Look at the HR schema's CREATE TABLE statements. Is there a better way to
tackle the problem that the new hire's trigger is attempting to solve? If so,
explain briefly what you would recommend doing instead.
Enter your answer here
S A
Save Answer
Q5.72 Points
A SQL database can be made more secure by combining its access control
capabilities (i.e., GRANT and REVOKE) with which of the following other SQL
DDL features? (Check all that apply.)
Save Answer
Q6 Last and also Least1 Point
Take one last careful look at the tables and columns in the HR schema and
data. What strikes you as quite literally being the oddest column in the entire
HR database? :-) (Note: Your answer should be of the form
Tblname.colname .)
Enter your answer here
Save Answer
Primary keys
Views
Foreign keys
Stored procedures
Check constraints
Unique constraints
Save All Answers Submit & View Submission \