Middeb] 2 T4 PM U

24
32ν32 Questions Answered Saved at 1237 AM Middeb] 2 4 PM Q1 2 Points Q11 Preliminaries 2 Points I^cdbecdi_^c MUST READ Idͻc g_bdh 2 `_i^dc The allowed time for the exam is 50 ]i^edec Be sure to pay attention to time and to budget your time accordingly The exam is open preΌprepared hardcopy 2Όsided cheat sheet but is cl_ced efebidhi^g elce You are 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 While taking the exam the _^li window open on your computer is to be a browser with one tab Ό ie this one Go ahead and close everything else now if you have not already done so Also put away any other devices Ό just keep your cheat sheet out STUDENT NAME Search students by name or email Ë

Transcript of Middeb] 2 T4 PM U

Page 1: Middeb] 2 T4 PM U

32/32 Questions AnsweredSaved at 12:37 AM

Midterm 2 (4 PM)

Q12 Points

Q1.1 Preliminaries2 Points

Instructions (MUST READ: It's worth 2 points!)

The allowed time for the exam is 50 minutes. Be sure to pay attention to time

and to budget your time accordingly!

The exam is open pre-prepared hardcopy 2-sided cheat sheet but is closed

everything else. You are 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! While taking the

exam, the only window open on your computer is to be a browser with one tab

- i.e., this one. (Go ahead and close everything else now if you have not

already done so! Also, put away any other devices - just keep your cheat sheet

out.)

STUDENT NAME

Search students by name or email… Ë

Page 2: Middeb] 2 T4 PM U

Read each question carefully, in its entirety, and then answer each part of the

question. If you don't understand something, make your best educated guess

and proceed accordingly.

Acknowledgement: I certify that I am taking this exam myself, on my own, with

honesty and integrity, without interaction with others during the exam, following

the rules outlined above and on Piazza, and without having obtained any

information about the exam's content from any source prior to taking it.

� Correct

Save Answer Last saved on Nov 24 at 12:29 AM

Q2 Short and Two the Point(s)26 Points

Reference Data

Questions that involve query-writing and data will all be based on the following

information about musicians, guitars, and which musicians own which guitars.

Be sure to read the DDL comments before proceeding to understand the

meanings of the fields. (This database is for use in a new guitar-trading website

that you will be working on throughout this exam. :-)) We will also periodically

provide a summary of the schema and/or data for reference throughout the

exam so you won't have to scroll very far to refer to it when needed.

Note: You will not need to copy/paste anything from this schema or data here,

because later, when you might want a subset of this data in CSV form, we will

give you what you'll need in a copy/paste-able form there. (And remember that

you are not permitted to use PostgreSQL or RelaX or any other tools during the

exam!)

Trueź

Falseý

Page 3: Middeb] 2 T4 PM U

For each of the following questions, you should

SELECT best(answer) FROM question LIMIT 1.

Q2.12 Points

It is possible to write a relational algebra query Q over a relational database D

that produces a result with more tuples than the sum of the cardinalities of D's

tables.

Page 4: Middeb] 2 T4 PM U

� Correct

Save Answer Last saved on Nov 24 at 12:29 AM

Q2.22 Points

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

� Correct

Save Answer Last saved on Nov 24 at 12:29 AM

Q2.32 Points

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

� Correct

Save Answer Last saved on Nov 24 at 12:30 AM

Q2.42 Points

The SQL database language is relationally complete.

Trueź

Falseý

Trueý

Falseź

Trueý

Falseź

Page 5: Middeb] 2 T4 PM U

� Correct

Save Answer Last saved on Nov 24 at 12:30 AM

Q2.52 Points

Based on the given tables, what is the cardinality of the result of the relational

algebra query: π name (Owns ⨝ Musician)

� Correct

Save Answer Last saved on Nov 24 at 12:30 AM

Trueź

Falseý

more than 6ý

Page 6: Middeb] 2 T4 PM U

Q2.62 Points

Based on the given tables, what is the cardinality of the result of the relational

calculus query: { m | (m ∉ Musician) ∧ (m.name = 'Dickey') }

� Correct

Save Answer Last saved on Nov 24 at 12:30 AM

Q2.72 Points

What would be the appropriate PostgreSQL feature to use to ensure that the

net worth value stored for each musician is always consistent with the

information in the Owns and Guitar tables? (Recall from the DDL comments that

a musician's net worth is the sum of the prices of their owned guitars.)

� Correct

Save Answer Last saved on Nov 24 at 12:30 AM

more than 6ź

FOREIGN KEY CONSTRAINTý

CHECK CONSTRAINTý

PRIMARY KEY CONSTRAINTý

TRIGGERź

VIEWý

ENUMERATIONý

Page 7: Middeb] 2 T4 PM U

Q2.82 Points

What would be the appropriate PostgreSQL feature to use to ensure that the

year of acquisition of a guitar is never before the year 1900 A.D.?

� Correct

Save Answer Last saved on Nov 24 at 12:30 AM

Q2.92 Points

For the given data model, what would be the appropriate PostgreSQL feature

to use to ensure that no guitar can be listed as being owned (acquired) by a

particular musician in multiple years?

� Correct

Save Answer Last saved on Nov 24 at 12:31 AM

FOREIGN KEY CONSTRAINTý

CHECK CONSTRAINTź

PRIMARY KEY CONSTRAINTý

TRIGGERý

VIEWý

ENUMERATIONý

FOREIGN KEY CONSTRAINTý

CHECK CONSTRAINTý

PRIMARY KEY CONSTRAINTź

TRIGGERý

VIEWý

ENUMERATIONý

Page 8: Middeb] 2 T4 PM U

Q2.102 Points

What would be the appropriate PostgreSQL feature to use to ensure that each

of the musicians owns at least one guitar?

� Correct

Save Answer Last saved on Nov 24 at 12:31 AM

Q2.112 Points

What would be the appropriate PostgreSQL feature to use to ensure that the

make of each guitar is drawn from a specified set of allowable values (e.g.,

Fender, Gibson, Epiphone, Yamaha, Heritage, G&L, or Dean)?

� Correct

Save Answer Last saved on Nov 24 at 12:31 AM

FOREIGN KEY CONSTRAINTý

CHECK CONSTRAINTý

PRIMARY KEY CONSTRAINTý

TRIGGERź

VIEWý

ENUMERATIONý

FOREIGN KEY CONSTRAINTý

CHECK CONSTRAINTý

PRIMARY KEY CONSTRAINTý

TRIGGERý

VIEWý

ENUMERATIONź

Page 9: Middeb] 2 T4 PM U

Q2.122 Points

Consider the following pair of queries (Query 1 and Query 2):

Query 1 and Query 2 return different results for the given data.

� Correct

Save Answer Last saved on Nov 24 at 12:31 AM

Q2.132 Points

Is the following GROUP BY query legal (runnable) according to PostgreSQL?

Trueź

Falseý

Page 10: Middeb] 2 T4 PM U

� Correct

Save Answer Last saved on Nov 24 at 12:31 AM

Q3 Taking an Equivalence Class27 Points

For each of the following problem series, you will be given a relational query or

an English query along with a set of additional queries. For each problem, you

are to indicate which query or queries in the additional 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 CREATE TABLE

statements.) Note: Be sure to examine each query carefully!

Q3.13 Points

Query 1: Find the ids of musicians who own all of the "ES-355" guitars.

Yesý

Noź

Page 11: Middeb] 2 T4 PM U

{t(ownerid) | ∃o ∈ Owns (t.ownerid = o.ownerid ∧ ∀g ∈ Guitar (g.model = 'ES-355' ∧ o.gid = g.gid)}

The query above is equivalent to Query 1.

� Correct

Save Answer Last saved on Nov 24 at 12:31 AM

Q3.23 Points

Query 1: Find the ids of musicians who own all of the "ES-355" guitars.

(π ownerid, gid (Owns)) ÷ (π gid σ model = 'ES-355' (Guitar))

The query above is equivalent to Query 1.

� Correct

Save Answer Last saved on Nov 24 at 12:32 AM

Q3.33 Points

Query 1: Find the ids of musicians who own all of the "ES-355" guitars.

SELECT o.ownerid FROM Owns o WHERE (SELECT COUNT (*) FROM Guitar g WHERE g.model = 'ES-355')

Trueý

Falseź

Trueź

Falseý

Page 12: Middeb] 2 T4 PM U

= (SELECT COUNT (*) FROM Guitar g WHERE g.model = 'ES-355' AND g.gid = o.gid);

The query above is equivalent to Query 1.

� Correct

Save Answer Last saved on Nov 24 at 12:32 AM

Q3.43 Points

Query 2: Find the ids and names of musicians who own at least two "Les Paul"

guitars.

{t(mid, name) | ∃m ∈ Musician (t.mid = m.mid ∧ t.name = m.name ∧ ∃o1 ∈ Owns (o1.ownerid = m.mid ∧ ∃o2 ∈ Owns (o2.ownerid = m.mid ∧ ∃g1 ∈ Guitar (g1.gid = o1.gid ∧ g1.make = 'Les Paul') ∧ ∃g2 ∈ Guitar (g2.gid = o2.gid ∧ g2.make = 'Les Paul'))))

The query above is equivalent to Query 2.

Trueý

Falseź

Trueź

Falseý

Michael Carey
Michael Carey
Page 13: Middeb] 2 T4 PM U

� Correct

Save Answer Last saved on Nov 24 at 12:33 AM

Q3.53 Points

Query 2: Find the ids and names of musicians who own at least two "Les Paul"

guitars.

SELECT m.mid, m.name FROM Musician mWHERE 1 < (SELECT COUNT(*) FROM Owns o, Guitar g WHERE o.ownerid = m.mid AND o.gid = g.gid AND g.model = 'Les Paul')

The query above is equivalent to Query 2.

� Correct

Save Answer Last saved on Nov 24 at 12:33 AM

Q3.63 Points

Query 2: Find the ids and names of musicians who own at least two "Les Paul"

guitars.

SELECT m.mid, m.name FROM Musician mWHERE m.mid IN (SELECT o.ownerid FROM Owns o WHERE o.gid IN (SELECT g1.gid FROM Guitar g1 WHERE model = 'Les Paul') AND o.gid IN (SELECT g2.gid FROM Guitar g2 WHERE model = 'Les Paul'))

Trueź

Falseý

Page 14: Middeb] 2 T4 PM U

The query above is equivalent to Query 2.

� Correct

Save Answer Last saved on Nov 24 at 12:33 AM

Q3.73 Points

Query 3:

SELECT MIN(g.year) AS minyr FROM Guitar g WHERE g.model = 'Les Paul';

SELECT g.year AS minyr FROM Guitar g WHERE g.model = 'Les Paul' AND g.year = (SELECT MIN(year) FROM Guitar);

The query above is equivalent to Query 3.

� Correct

Save Answer Last saved on Nov 24 at 12:33 AM

Trueý

Falseź

Trueý

Falseź

Page 15: Middeb] 2 T4 PM U

Q3.83 Points

Query 3:

SELECT MIN(g.year) AS minyr FROM Guitar g WHERE g.model = 'Les Paul';

SELECT g.year AS minyr FROM Guitar g WHERE g.model = 'Les Paul' ORDER BY g.year ASC LIMIT 1;

The query above is equivalent to Query 3.

� Correct

Save Answer Last saved on Nov 24 at 12:33 AM

Q3.93 Points

Query 3:

SELECT MIN(g.year) AS minyr FROM Guitar g WHERE g.model = 'Les Paul';

SELECT g.year AS minyr FROM Guitar g WHERE g.model = 'Les Paul' ORDER BY g.year ASC OFFSET 1 LIMIT 0;

The query above is equivalent to Query 3.

� Correct

Save Answer Last saved on Nov 24 at 12:33 AM

Trueź

Falseý

Trueý

Falseź

Page 16: Middeb] 2 T4 PM U

Q4 Query Me This, Batman29 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 Musician WHERE mid < 3;

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

mid,name,mail,year,networth 1,"Steve","[email protected]",1954,NULL 2,"Dickey","[email protected]",1943,NULL

Note: Be sure to include the first row, listing the result column names, as well as listing the data rows!

In preparation for what follows, here's a repeat of the database's current

contents:

Page 17: Middeb] 2 T4 PM U

Q4.111 Points

Write a SQL query to find the names and e-mails of musicians who own guitars

made by Gibson. Order your results by the guitars' ages (newest first).

Save Answer Last saved on Nov 24 at 12:34 AM

Q4.26 Points

Write the body of a SQL function to compute the net worth of a musician given

their id. (Recall that a musician's net worth should be computed by adding up

the prices of the guitars that they own.) Below is PostgreSQL's syntax for

creating such a function, but minus the SQL query that you are to provide.

Enter the missing SQL query (a.k.a. the function body) in the answer box below.

Hint: To get your query 100% right in all cases, you may find PostgreSQL's

COALESCE function useful. This function returns the first of its arguments that

is non-NULL, so, for example, COALESCE (2500.00, 0.0) returns 2500.00, while

COALESCE (NULL, 0.0) is 0.00. (Don't panic if you don't see how to use

COALESCE here; it's only worth one point to use it successfully.)

CREATE FUNCTION NetWorth(mid int) RETURNS DECIMAL(8,2) AS $$ -- your query goes here $$ LANGUAGE SQL;

SELECT m.name, m.email, g.year

FROM Musician m, Owns o, Guitar g

WHERE g.gid = o.gid

AND m.mid = o.ownerid

AND g.make = 'Gibson'

ORDER BY g.year DESC;

SELECT COALESCE(SUM(g.price), 0.0)

FROM Owns o, Guitar g

Page 18: Middeb] 2 T4 PM U

Save Answer Last saved on Nov 24 at 12:35 AM

Q4.310 Points

Musician: mid, name, email, year, networth 1, 'Steve', '[email protected]', 1954, NULL 2, 'Dickey', '[email protected]', 1943, NULL 3, 'Jimi', '[email protected]', 1942, NULL 4, 'BB', '[email protected]', 1925, NULL 5, 'Dickey', '[email protected]', 1990, NULL 6, 'Nona', '[email protected]', 2001, NULL

Guitar: gid, model, make, price, year 1, 'Stratocaster', 'Fender', 2500.00, 1963 2, 'Stratocaster', 'Fender', NULL, 1963 3, 'Telecaster', 'Fender', 2000.00, NULL 4, 'Les Paul', 'Gibson', 4000.00, 1957 5, 'Les Paul', 'Gibson', 2400.00, 2011 6, 'Les Paul', 'Epiphone', 1000.00, 1989 7, 'ES-355', 'Gibson', 3600.00, 1980

Owns:ownerid, gid, year 1, 1, 19822, 4, 19802, 6, 19913, 2, 19664, 7, 19805, 5, 2011

Consider the following view definition:

CREATE VIEW FenderOwners AS WITH Fenders AS (SELECT * FROM Guitar WHERE make = 'Fender') SELECT m.mid, m.name, m.email, COUNT(f.gid) AS numguitars

WHERE o.gid = g.gid

AND o.ownerid = mid;

Page 19: Middeb] 2 T4 PM U

FROM Musician m LEFT OUTER JOIN Owns o ON m.mid = o.ownerid LEFT OUTER JOIN Fenders f ON o.gid = f.gid GROUP BY m.mid;

What will the following query over this view return?

(Remember to show the column names as well as the data, as illustrated

above.)

SELECT * FROM FenderOwners WHERE mid < 4 ORDER BY numguitars ASC;

Save Answer Last saved on Nov 24 at 12:35 AM

Q4.42 Points

As you know, views are virtual tables, and some views are updatable. Would

the following update be possible on this view?

UPDATE FenderOwners

SET numguitars = numguitars - 1

WHERE mid = 2;

� Correct

Save Answer Last saved on Nov 24 at 12:36 AM

Q5 To Err is Human, to Succeed Divine15 Points

mid,name,email,numguitars

2,"Dickey","[email protected]",0

1,"Steve","[email protected]",1

3,"Jimi","[email protected]",1

Yesý

Noź

Page 20: Middeb] 2 T4 PM U

It's time to move past SELECT queries and views and put some of SQL's more

advanced features to use on the guitar-trading database. Below is what the

database contains prior to the start of this problem (based on the exam's initial

DDL).

Musician: mid, name, email, year, networth 1, 'Steve', '[email protected]', 1954, NULL 2, 'Dickey', '[email protected]', 1943, NULL 3, 'Jimi', '[email protected]', 1942, NULL 4, 'BB', '[email protected]', 1925, NULL 5, 'Dickey', '[email protected]', 1990, NULL 6, 'Nona', '[email protected]', 2001, NULL

Guitar: gid, model, make, price, year 1, 'Stratocaster', 'Fender', 2500.00, 1963 2, 'Stratocaster', 'Fender', NULL, 1963 3, 'Telecaster', 'Fender', 2000.00, NULL 4, 'Les Paul', 'Gibson', 4000.00, 1957 5, 'Les Paul', 'Gibson', 2400.00, 2011 6, 'Les Paul', 'Epiphone', 1000.00, 1989 7, 'ES-355', 'Gibson', 3600.00, 1980

Owns:ownerid, gid, year 1, 1, 19822, 4, 19802, 6, 19913, 2, 19664, 7, 19805, 5, 2011

Q5.16 Points

Up until now the new website's net worth functionality has been ignored.

Suppose that you now run the following UPDATE command which uses the

NetWorth function that you defined earlier. (Assume that your function does

what it's supposed to, whether or not your answer earlier was actually correct

SQL.)

Page 21: Middeb] 2 T4 PM U

UPDATE Musician SET networth = NetWorth(mid);

Show the resulting contents of the just-updated Musician table. (Note that you

can cut/paste its original contents from above and then just change the values

in the last column.)

Save Answer Last saved on Nov 24 at 12:36 AM

Q5.23 Points

One of the guitar-trading website's application developers has created the

following trigger:

CREATE FUNCTION UpdateNetWorth ( ) RETURNS Trigger AS $$ BEGIN IF (NEW.ownerid IS NOT NULL) THEN UPDATE Musician SET networth = NetWorth(NEW.ownerid) WHERE mid = NEW.ownerid; END IF; IF (OLD.ownerid IS NOT NULL) THEN UPDATE Musician SET networth = NetWorth(OLD.ownerid) WHERE mid = OLD.ownerid; END IF; RETURN NEW; END; $$ LANGUAGE PLPGSQL;

CREATE TRIGGER NetWorthUpTracker AFTER INSERT OR UPDATE OR DELETE ON Owns FOR EACH ROW EXECUTE FUNCTION UpdateNetWorth ( );

mid, name, email, year, networth

1,"Steve","[email protected]",1954,2500.00

2,"Dickey","[email protected]",1943,5000.00

3,"Jimi","[email protected]",1942,0.00

4,"BB","[email protected]",1925,3600.00

5,"Dickey","[email protected]",1990,2400.00

6,"Nona","[email protected]",2001,0.00

Page 22: Middeb] 2 T4 PM U

Show what the impact will be of each of the following SQL statements by

showing the contents of the Musician table after each one. To save time and

effort, you only need to list the rows whose contents were affected by the

statement in question. If the statement had no impact on the values in the

table, check the "no impact" indicator instead.

Statement:

INSERT INTO Owns (ownerid, gid, year) VALUES (1, 3, 2021);

Impact:

Save Answer Last saved on Nov 24 at 12:37 AM

Q5.33 Points

Statement:

DELETE FROM Owns WHERE gid = 7;

Impact:

Save Answer

1,"Steve","[email protected]",1954,4500.00

4,"BB","[email protected]",1925,0.00

No impact.ý

1 or more Musician rows affected:ź

No impact.ý

1 or more Musician rows affected:ź

Page 23: Middeb] 2 T4 PM U

Save Answer Last saved on Nov 24 at 12:37 AM

Q5.43 Points

Statement:

UPDATE Owns SET ownerid = 6 WHERE gid = 4;

Impact:

Save Answer Last saved on Nov 24 at 12:37 AM

Q6 Enough Is Enough!1 Point

I once again certify that I have completed this exam on my own, with honesty

and integrity, without interaction with others or accessing forbidden materials

during the exam, and without having obtained any information about the

exam's content from any source prior to taking it.

� Correct

Save Answer Last saved on Nov 24 at 12:36 AM

2,"Dickey","[email protected]",1943,1000.00

6,"Nona","[email protected]",2001,4000.00

No impact.ý

1 or more Musician rows affected:ź

Trueź

Falseý

Page 24: Middeb] 2 T4 PM U

Save All Answers Submit & View Submission \