23
34/34 Questions Answered Saved on Jun 4 at 4:49 PM TIME REMAINING : Midterm 2 Q1 Preliminaries 1 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… Ë

Midterm 2 - University of California, Irvine · 2020. 11. 17. · 34/34 Questions Answered Saved on Jun 4 at 4:49 PM TIME REMAINING: Midterm 2 Q1 Preliminaries 1 Point Instructions

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

  • 34/34 Questions AnsweredSaved on Jun 4 at 4:49 PM

    TIME REMAINING

    �:��

    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);

    Correct

    Save Answer Last saved on Jun 04 at 8:42 AM

    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:

  • Correct

    Save Answer Last saved on Jun 04 at 8:43 AM

    Q2.22 Points

    Join is a core (necessary) operation in the relational algebra.

    Correct

    Save Answer Last saved on Jun 04 at 8:43 AM

    Q2.32 Points

    Union is a core (necessary) operation in the relational algebra.

    Correct

    Save Answer Last saved on Jun 04 at 8:43 AM

    SQL

    the relational algebra

    the relational calculus

    Python

    True

    False

    True

    False

  • Q2.42 Points

    Division is a core (necessary) operation in the relational algebra.

    Correct

    Save Answer Last saved on Jun 04 at 8:43 AM

    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

    Correct

    Save Answer Last saved on Jun 04 at 8:43 AM

    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' }

    True

    False

    0

    3

    5

    15

  • Correct

    Save Answer Last saved on Jun 04 at 8:43 AM

    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?

    Correct

    Save Answer Last saved on Jun 04 at 8:44 AM

    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?

    0

    1

    5

    11

    FOREIGN KEY CONSTRAINT

    CHECK CONSTRAINT

    UNIQUE CONSTRAINT

    TRIGGER

    VIEW

  • Correct

    Save Answer Last saved on Jun 04 at 8:44 AM

    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?

    Correct

    Save Answer Last saved on Jun 04 at 8:44 AM

    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?

    FOREIGN KEY CONSTRAINT

    CHECK CONSTRAINT

    UNIQUE CONSTRAINT

    TRIGGER

    VIEW

    FOREIGN KEY CONSTRAINT

    CHECK CONSTRAINT

    UNIQUE CONSTRAINT

    TRIGGER

    VIEW

  • Correct

    Save Answer Last saved on Jun 04 at 8:44 AM

    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?

    Correct

    Save Answer Last saved on Jun 04 at 8:44 AM

    Q2.122 Points

    What would the following MySQL query return given the current HR reference

    tables?

    FOREIGN KEY CONSTRAINT

    CHECK CONSTRAINT

    UNIQUE CONSTRAINT

    TRIGGER

    VIEW

    FOREIGN KEY CONSTRAINT

    CHECK CONSTRAINT

    UNIQUE CONSTRAINT

    TRIGGER

    VIEW

  • SELECT (e1.age + e2.age) / 2FROM Emp e1, Emp e2 WHERE e1.eid = 7 AND e2.eid = 9;

    Correct

    Save Answer Last saved on Jun 04 at 8:45 AM

    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;

    Correct

    Save Answer Last saved on Jun 04 at 8:45 AM

    Q2.142 Points

    0

    11

    22

    NULL

    0

    11

    22

    NULL

  • 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?

    Correct

    Save Answer Last saved on Jun 04 at 8:45 AM

    Q2.152 Points

    Consider the following MySQL UPDATE query:

    UPDATE Emp e SET e.eid = e.eid + 1WHERE e.age > 21;

    Look at the Emp table's CREATE TABLE statement again. Will this update query

    succeed?

    Correct

    Save Answer Last saved on Jun 04 at 8:45 AM

    Q3 Taking an Equivalence Class

    Yes

    No

    Yes

    No

  • 30 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

    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):

    Correct

    Save Answer Last saved on Jun 04 at 8:45 AM

    Q3.26 Points

    Here is the given query:

    (π eid, did (Works)) ÷ (π did ( σ budget = 50000 (Dept)))

    Select the equivalent query or queries (if any):

    Correct

    Save Answer Last saved on Jun 04 at 8:46 AM

    π 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) }

    { 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)))))}

  • 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):

    Correct

    Save Answer Last saved on Jun 04 at 8:46 AM

    Q3.46 Points

    Here is the given query:

    π eid, ename (σ pcttime < 100 (Works ⨝ Emp))

    Select the equivalent query or queries (if any):

    { 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);

  • Correct

    Save Answer Last saved on Jun 04 at 8:46 AM

    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):

    Correct

    Save Answer Last saved on Jun 04 at 8:46 AM

    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;

    eid,pct,did,dname,budget,mgrid,mgrname

    1,80,101,Accounting,50000,1,Dustin

    7,33,101,Accounting,50000,1,Dustin

    1,20,102,Sales,50000,3,Em

    3,100,102,Sales,50000,3,Em

    5,50,102,Sales,50000,3,Em

    7,33,102,Sales,50000,3,Em

    7,33,103,Legal,100000,7,Win

    9,100,NULL,NULL,NULL,NULL,NULL

    Save Answer Last saved on Jun 04 at 4:46 PM

    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)?

    It's currently possible for an employee to be assigned to work in a non-

    existent department.

    Save Answer Last saved on Jun 04 at 8:48 AM

    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;

  • eid,ename,mgrname,did,dname,budget

    1,Dustin,Dustin,101,Accounting,50000

    3,Emily,Em,102,Sales,50000

    5,John,NULL,NULL,NULL,NULL

    7,Win,Win,103,Legal,100000

    9,"Arthur Jr",NULL,NULL,NULL,NULL

    Save Answer Last saved on Jun 04 at 4:49 PM

    Q4.42 Points

    And what problem or issue does this query reveal with the current schema or base table contents?

    It's currently possible for an employee's name to be different than what's

    recorded in the department table.

    Save Answer Last saved on Jun 04 at 8:48 AM

    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):

    SELECT w.eid, w.did, w.budget

    FROM WorksInDept w

    WHERE NOT EXISTS

    (SELECT * FROM ManagesDept m

    WHERE m.eid = w.eid AND m.did = w.did);

  • Save Answer Last saved on Jun 04 at 8:48 AM

    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

    SELECT age AS agegroup, COUNT(*) AS numemps, MIN(salary) AS lowsal,

    AVG (salary) AS avgsal, MAX(salary) AS highsal

    FROM Emp

    GROUP BY age;

    Save Answer Last saved on Jun 04 at 8:58 AM

    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);

    Dept (did, dname, budget, eid, ename)

    (101, 'Accounting', 50000, 1, 'Dustin'),

    (102, 'Sales', 50000, 3, 'Emily'),

  • 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;

    8

    Save Answer Last saved on Jun 04 at 8:50 AM

    Q5.42 Points

    Here's the next statement in the sequence:

    INSERT INTO Works (eid, did, pcttime) VALUES (7, 104, 1);

    Warning: Bad assignment!

    Save Answer Last saved on Jun 04 at 8:51 AM

    Q5.51 Point

    And here's the last statement in the sequence:

    SELECT COUNT(*) AS after FROM Works;

    8

  • Save Answer Last saved on Jun 04 at 8:50 AM

    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.

    Yes.

    Add a FOREIGN KEY constraint FROM Works(did) to Dept(did)!

    Save Answer Last saved on Jun 04 at 8:51 AM

    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.)

    Correct

    Primary keys

    Views

    Foreign keys

    Stored procedures

    Check constraints

    Unique constraints

  • Save Answer Last saved on Jun 04 at 8:51 AM

    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 .)

    Emp.eid

    Correct

    Save Answer Last saved on Jun 04 at 8:52 AM

    Save All Answers Submit & View Submission