Fall 2015 CS 370 Exam 2 Page 1 Database Management Systems 11/6/15 Name______KEY______1. True/False. [25 pts]

__F___ A 1NF relation has allows null values and multi-valued attributes. __ F ___ A normalized relation’s primary key is exactly one attribute. __ T ___ Normalization attempts to identify and then reduce or eliminate redundant facts stored in a database table tuple through decomposition. __ T ___ Decomposition of a table for normalization results in two or more tables to replace the original table. __ T ___ It is desirable to have a lossless join decomposition. __ T ___ Normalization should remove transitive functional dependencies. __ T ___ In a CREATE TABLE command, each attribute must have a data type associated with it. __ F ___ The INTEGER type is the best for accounting and monetary representation. __ T ___ Referential integrity ensures that a value stored in the foreign key matches a primary key in the referred table and what to do if that primary key value changes. __ T __ A view is classified as part of the external schema and is an acceptable way to offer simpler tables with customized attribute names to the user. ___ T ___A view is referred to like other tables in a SELECT. __ F ____A view is inefficient and should be used minimally. __ T ___ Besides a shorthand reference to the table throughout the query, using an alias also implements the rename capability of relational algebra. __ T ___ DELETE-ing from a view that is based on a simple selection of its base table is acceptable. __ T ___ A view can provide a rejoining of decomposed relations for the end user. ___ T __ In an object database, the database objects must have persistent storage in secondary storage. __ T ___ In an object database, an object can be related to many other objects through the set collection. __ F ___ NOT NULL UNIQUE tags on an attribute implies the attribute is a foreign key. __ F ___ JDBC uses a statement level interface and access protocol for embedded SQL in Java. __ T ___ The SQLSTATE system variable holds a 5 digit error or success code string of the last SQL operation. __ T ___ The UML diagram rectangles essentially represent entities, their attributes and methods. __ F ___ Isa relationships in an EER diagram represent the object form of relationships. __ T ___ An INSERT requires the input values sequence to match the attribute sequence when the attributes are specified in the INSERT statement. __ F ___ The SQL SELECT statement specifies not only the operations the server uses to answer the query, but the sequence of steps as well. __ T ___ Triggers provide a mechanism to monitor specific changes to a table and perform additional actions based on the altered data. Fall 2015 CS 370 Exam 2 Page 2

2. Below is the Postgres trigger on the Presidents database that was shown in class. It updates a population statistic anytime a population value changes in the States table. [15 pts] CREATE FUNCTION resetPopTotal() RETURNS trigger AS $$ BEGIN UPDATE state_stats SET attrValue = (SELECT Sum(pop) from states) WHERE attrName = 'total'; INSERT INTO STATELOG (State,oldPop,newPop) VALUES (OLD.state, OLD.Pop, NEW.Pop); RETURN null; END; $$ LANGUAGE plpgsql;

CREATE TRIGGER UpdateTotalTrigger AFTER UPDATE OR DELETE OR INSERT ON states FOR STATEMENT EXECUTE PROCEDURE resetPopTotal();

All of your projects could calculate a “balance due” based on the sum of the charges, less the payments.  Photography balance due based on event scheduled, photos bought less payments made  University pledges not fulfilled have balance on the pledge not paid  The medical center patients have balance on their account of visits, procedures, prescriptions and insurance payments  The theatre has balances due on ticket sales and membership dues. For your project, or any of the four projects, assume there is an attribute (that you’ve been told not to have in your ER diagram) called BalanceDue on the Person table. Rough code a function body that would calculate the balance due based on the sources of the charges and the sources of the payments. Correct syntax of plpgsql is not required; use pseudo-code as necessary. [9]

CREATE FUNCTION setBalanceDue() RETURNS trigger AS $$ DECLARE Charges DEC (10,2); PayIns DEC (10,2); BEGIN Charges := SELECT sum(amount) from PLEDGES P WHERE P.PID = NEW.PID; PayIns := SELECT sum(amount) from PAYMENTS P WHERE P.PID = NEW.PID UPDATE PERSON SET BalanceDue = (Charges-Payins) WHERE P.PID = NEW.PID; RETURN null; END; $$ LANGUAGE plpgsql;

a) Rough code the triggers you would need to ensure the balance due kept accurate. You may need more than one. [6]

CREATE TRIGGER UpdateBalDueTrigger AFTER UPDATE OR DELETE OR INSERT ON Pledges FOR STATEMENT EXECUTE PROCEDURE setBalanceDue ();

CREATE TRIGGER UpdateBalDueTrigger Fall 2015 CS 370 Exam 2 Page 3 AFTER UPDATE OR DELETE OR INSERT ON Payments FOR STATEMENT EXECUTE PROCEDURE setBalanceDue (); Fall 2015 CS 370 Exam 2 Page 4

3. JDBC short answers. [12 pts] a. What is the purpose of the initially loaded database driver in a JDBC application? Why is it a separate class library? [3]

It’s the specific code to map the Java calls to the particular type of data base. If the database is moved to a different type of server, the application only needs to replace the driver.

b. What is happening in a database connection? (And don’t just say that a connection is being made to the database.) [3]

It establishes a comunication path to the server, verifies the username and password

Chooses the database on the server

Below is a segment of Java code that is processing a result set. ResultSet res = stmt.executeQuery(query); while(res.next()){ stateName = res.getString("STATE"); population = res.getInt("POP"); . . . . c. What is a cursor in the result set? And how is it used and advanced? [3] An internal pointer to one of the tuples in the result set. It is typically advanced in a sequential method but can be more sophisticated. It may allow for local changes to be pushed back to the database.

d. Describe what is happening in the assignment statements in the loop of the code above. [3] The string passed to the get* methods are the attributes returned in the result set. Local conversions of type to the program variables take place here.

4. Disk drives. a. If a disk has sectors that are 2048 bytes and tuple sizes are 100 bytes, describe how the data in a very large relational table of those tuples would likely be placed in those sectors. Do not split a tuple across a sector; do not consider any indexing structures. [3 pts] You can fit up to 20 tuples per sector. Int(2048/100)

b. A disk spins at 6000 rpm (revolutions per minute or 100 rev/sec or 10 msec per revolution) and average read/write heads armature motion is 8 ms. (ms = .001 sec) [6 pts] Average rotational latency = ____5______ms, and average seek time = ______8______ms. If there are 100 sectors per track, the transfer time for 10 contiguous sectors = _____1______ms. The total expected access time for these 10 sectors is ______14______ms. Fall 2015 CS 370 Exam 2 Page 5

5. Assume the object database schema segment below. Answer the short questions. [9 pts] class Person { attribute int pId; attribute Struct Name (string fname, char mi, string lname) fullName; attribute string zip; attribute string strAddress; attribute String phone; Name getName( ); void setName(Name newName); }; class Student extends Person { attribute int credits; real getGpa(); int getCredits( ); void addCredits(int numCredits);

relationship hasZip Inverse CSZ::hasAddresses; relationship Set enrollsClass Inverse ClassSection::hasStudent; relationship Faculty has Advisor Inverse Faculty::advises; };

a. What other attributes will a Student data object contain than those listed in the class? [3]

Pid, name, zip, strAddress, phone

b. How many classes can a student enroll in? [1] _ many__

c. How many advisors can a student have? [1] __1___

d. Give an example of a composite attribute above. [2] ______Name______

e. Name one class being referenced in a relationship in the above code. [1] __CSZ,ClassSection, or Faculty__

f. Name a role being referenced in a relationship. [1] hasZip, has Address, enrollsClass, hasStudent, Advisor, advises

Fall 2015 CS 370 Exam 2 Page 6 For the remaining questions, use the following relational schema for a music albums database. Keys are underlined. The attributes should be self-evident. If not, please ask for clarification. For a given music track, we code the title, its play length in time (minutes:seconds), its genre (pop, metal, jazz, etc.) and a 5 star maximum rating. The musicians, singers and instrumentalists are all listed with their contribution to the track. A person may have 1 or more listing for a track. For example someone may both sing and play the piano. The album is a collection of tracks. An album is distributed and owned by a company called the “label”. Each album has a producer and an engineer.

PEOPLE (PID, name, address, zip, phone) CSZ (zip, city, state) TRACKS (trID, title, length, genre, rating, albID) //trID is unique across all albums ALBUMS (albID, albumTitle, year, labelID, prodPID, engPID, length, price) PERFORMERS (trID, PID, role) LABELS(labelID, name, address, zip) [30 pts total] Quick syntax for SQL, where [] means optional, {op1|op2|...} means choice SELECT [DISTINCT] {* | attribute-list | aggregate functions}... FROM table {, table | NATURAL JOIN table | LEFT OUTER JOIN table {USING(attr) | ON condition}}* WHERE condition [GROUP BY attribute-list [HAVING condition]]

SQL conditions consist of <,>,<=,>=, <>,=, AND, OR, BETWEEN value AND value, IN (SELECT…) [NOT] EXISTS ({list | SELECT...}), rel-op {ANY|SOME|ALL} ({ list | SELECT...}), IS [NOT] NULL Aggregate functions: COUNT(*|[DISTINCT] attr), MIN(attr), MAX(attr), SUM(attr), AVG(attr) (Select1 {UNION | INTERSECT | EXCEPT} Select2)

a) List all names and phone numbers of people from zip 90210. [3]

SELECT P.name, P.phone FROM People P WHERE P.zip=’90210’;

b) List album titles and their label names that have a list price of more than $18. [5]

SELECT A.albumTitle, L.name FROM Albums A [NATURAL JOIN], Labels L WHERE price >18 [ AND A.labelID=L.labelID]

c) List all the performer names and their role on all jazz genre tracks. [5]

SELECT P.name, P.role FROM Tracks T [NATURAL JOIN] Performers P [NATURAL JOIN] People A WHERE T.genre=’jazz’ [AND T.trID=P.trID AND P.PID=A.PID] Fall 2015 CS 370 Exam 2 Page 7 d) Get a list of names of people who produced OR engineered an album, AND also performed on any track. [6]

SELECT P.name FROM Performers P, People L, Albums A WHERE P.PID=L.PID AND (L.PID=A.prodID OR L.PID=A.engID) (can be intersection and union approaches too)

e) List years of the albums and average price of albums for each year, but only list those year and averages if there are at least 5 albums that year. (Hint: use group by having). [5]

SELECT A.year, avg(A.price) FROM Albums A GROUP BY year HAVING count(*)>=5

f) List names of musicians who have contributed in at least two different roles on tracks that are of the ‘classics’ genre. (Hint: use group by having). [6]

SELECT L.name FROM People L [NATURAL JOIN] Performer P [NATURAL JOIN] Tracks T WHERE T.genre=’classics’ [AND L.PID=P.PID AND T.TrID=P.TrID ] GROUP BY P.PID HAVING count(DISTINCT roles) >= 2;