Docsity

Assignment II Solved - Information Systems | CMPSCI 445, Assignments of Management Information Systems

Toggle sidebar
bg1
CMPSCI 445 — Homework 2
107 Points
Due February 26th, at the beginning of class.
Typed solutions preferred. If hand-written, solutions must be legible.
1. (42 pts) Harder Practical SQL exercises
You will use the sample dataset consisting of the 3 tables described below:
•Suppliers(sid: integer, sname: varchar(50), address: varchar(60))
•Parts(pid: integer, pname: varchar(50), color: varchar(20))
•Catalog(sid: integer, pid: integer, cost: real)
Write SQL expressions for each of the following queries and execute them. Please submit for
each query below (i) a valid SQL query, and (ii) the output of the query on the sample data.
(1) For every supplier, print the name of the supplier and the total number of parts that he
or she supplies.
SELECT S.sname, COUNT(*) as PartCount
FROM Suppliers S, Catalog C
WHERE C.sid = S.sid
GROUP BY S.sname, S.sid
(2) For every supplier who supplies at least 1 red part, print the name of the supplier and
the total number of red parts that he or she supplies.
SELECT S.sname, COUNT(*) as PartCount
FROM Suppliers S, Catalog C, Parts P
WHERE C.sid = S.sid and P.pid = C.pid and P.color = ’Red’
GROUP BY S.sname, S.sid
(3) Find the sids of suppliers who supply only red parts.
SELECT DISTINCT C.sid
FROM Catalog C
WHERE NOT EXISTS ( SELECT *
FROM Parts P
WHERE P.pid = C.pid AND P.color <> Red )
(4) For each part, find the sname of the supplier who charges the most for that part.
1
pf3
pf4
pf5

Partial preview of the text

Download Assignment II Solved - Information Systems | CMPSCI 445 and more Assignments Management Information Systems in PDF only on Docsity!

CMPSCI 445 — Homework 2

107 Points

Due February 26th, at the beginning of class. Typed solutions preferred. If hand-written, solutions must be legible.

  1. (42 pts) Harder Practical SQL exercises

You will use the sample dataset consisting of the 3 tables described below:

  • Suppliers(sid: integer, sname: varchar(50), address: varchar(60))
  • Parts(pid: integer, pname: varchar(50), color: varchar(20))
  • Catalog(sid: integer, pid: integer, cost: real)

Write SQL expressions for each of the following queries and execute them. Please submit for each query below (i) a valid SQL query, and (ii) the output of the query on the sample data.

(1) For every supplier, print the name of the supplier and the total number of parts that he or she supplies.

SELECT S.sname, COUNT(*) as PartCount FROM Suppliers S, Catalog C WHERE C.sid = S.sid GROUP BY S.sname, S.sid

(2) For every supplier who supplies at least 1 red part, print the name of the supplier and the total number of red parts that he or she supplies.

SELECT S.sname, COUNT(*) as PartCount FROM Suppliers S, Catalog C, Parts P WHERE C.sid = S.sid and P.pid = C.pid and P.color = ’Red’ GROUP BY S.sname, S.sid

(3) Find the sids of suppliers who supply only red parts.

SELECT DISTINCT C.sid FROM Catalog C WHERE NOT EXISTS ( SELECT * FROM Parts P WHERE P.pid = C.pid AND P.color <> Red )

(4) For each part, find the sname of the supplier who charges the most for that part.

SELECT P.pid, S.sname FROM Parts P, Suppliers S, Catalog C WHERE C.pid = P.pid AND C.sid = S.sid AND C.cost = ( SELECT MAX (C1.cost) FROM Catalog C WHERE C1.pid = P.pid )

(5) Find the snames of suppliers who supply every part.

SELECT S.sname FROM Suppliers S WHERE NOT EXISTS ( ( SELECT P.pid FROM Parts P ) EXCEPT ( SELECT C.pid FROM Catalog C WHERE C.sid = S.sid ) )

(6) Find the snames of suppliers who supply every red part.

SELECT S.sname FROM Suppliers S WHERE NOT EXISTS ( ( SELECT P.pid FROM Parts P WHERE P.color = Red ) EXCEPT ( SELECT C.pid FROM Catalog C, Parts P WHERE C.sid = S.sid AND C.pid = P.pid AND P.color = Red ) )

  1. (20 pts) ER Diagrams

You’re establishing a database company, ArtBase, that builds a product for art galleries. The core of this product is a database with a schema that captures all the information that galleries need to maintain. Galleries keep information about artists, their names (which are unique), birthplaces, age, and style of art. For each piece of artwork, the artist, the year it was made, its unique title, its type of art (e.g., painting, lithograph, sculpture, photograph), and its price must be stored. Pieces of artwork are also classified into groups of various kinds, for example, portraits, still lifes, works by Picasso, or works of the 19th century; a given piece may belong to more than one group. Each group is identified by a name (like those just given) that describes the group. Finally, galleries keep information about customers. For each customer, galleries keep that persons unique name, address, total amount of dollars spent in the gallery, and the artists and groups of art that the customer tends to like. Draw the ER diagram for the database. Indicate one-to-many or one-to-one relationships with arrows where appropriate.

Figure 1: ER diagram

  1. (15 pts) ER Diagram to Relations

Write SQL statements to create the corresponding relations to the ER diagram you designed above. You should write SQL CREATE TABLE commands and specify primary keys and foreign keys. Solution: The statements to create tables corresponding to entity sets Customer, Group, and Artist are straightforward and omitted. The other required tables can be created as follows:

CREATE TABLE Classify ( title CHAR(20), name CHAR(20), PRIMARY KEY (title, name), FOREIGN KEY (title) REFERENCES Artwork_Paints, FOREIGN KEY (name) REFERENCES Group )

CREATE TABLE Like_Group ( name CHAR(20), cust name CHAR(20), PRIMARY KEY (name, cust_name), FOREIGN KEY (name) REFERENCES Group, FOREIGN KEY (cust_name) REFERENCES Customer )

CREATE TABLE Like_Artist ( name CHAR(20), cust name CHAR(20), PRIMARY KEY (name, cust_name), FOREIGN KEY (name) REFERENCES Artist, FOREIGN KEY (cust_name) REFERENCES Customer )

CREATE TABLE Artwork_Paints ( title CHAR(20), artist name CHAR(20), type CHAR(20), price INTEGER, year INTEGER, PRIMARY KEY (title), FOREIGN KEY (artist_name) References Artist )

  1. (15 points) Schema Refinement

Consider a schema Student(ssn, name, groupID, group-name, project-grade). A natural func- tional dependency on this table is groupID → group-name, project-grade. This means that the value of groupID determines a unique value for both group-name and project-grade.

(a) Construct a database instance for this schema (with say 6 tuples) that satisfies this functional dependency (and also helps you to complete the remaining problems below).

ssn name age groupID group-name project-grade 1 Alice 12 101 one A 2 Bob 19 102 two B 3 Carol 34 101 one A 4 Dave 51 103 three B+ 5 Edgar 72 104 four A- 6 Fred 33 103 three B+

(b) Indicate a single change to one value in the database that would violate the functional dependency and explain the violation. Change Carol’s project grade to A-. Then two students with the same groupID (Alice and Carol) have different grades.