Download Exam 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft Access wikipedia , lookup

IMDb wikipedia , lookup

Oracle Database wikipedia , lookup

Concurrency control wikipedia , lookup

Functional Database Model wikipedia , lookup

Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Ingres (database) wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

SQL wikipedia , lookup

Clusterpoint wikipedia , lookup

Versant Object Database wikipedia , lookup

Join (SQL) wikipedia , lookup

PL/SQL wikipedia , lookup

Relational algebra wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

Transcript
Spring 2017
CS 370 Exam 2
Page 1
Database Management Systems
4/3/17
Name__________________________
1. True/False.
[25 pts]
______ A 1NF relation disallows null values and multi-valued attributes.
______ The primary key of a 3NF relation is exactly one attribute.
______ Normalization attempts to identify and then reduce or eliminate redundant facts stored in a
database table tuple through decomposition.
______ A lossless join decomposition of a table results in two or more tables such that join of the tables
can regenerate the original table.
______ It is desirable to have a lossy join decomposition.
______ Normalization should remove transitive functional dependencies.
______ In a CREATE TABLE command, each attribute must have a data type associated with it.
______ NOT NULL UNIQUE tags on an attribute implies the attribute is a candidate key.
______ The FLOAT or DOUBLE type is the best for accounting and monetary representation.
______ Referential integrity ensures that a value stored in the foreign key matches a primary key in the
referred table and what and rejects an insertion if the FK value is not found as the PK there.
______ A view is classified as part of the physical schema
_______A view is referred to as other tables in a SELECT.
_______A view is inefficient and should be used as a last resort.
______ Besides a shorthand reference to the table throughout the query, using a table alias also
implements the rename capability of relational algebra.
______ UPDATE-ing from a view that is based on a simple selection from its base table is acceptable.
______ A view can only be constructed from a single table; no joins.
______ In an object database, the database objects do not have to have persistent storage in secondary
storage as they can remain in the object store in primary memory.
______ In an object database, an object can be related to many other objects through the set collection.
______ The UML diagram rectangles essentially represent entities, their attributes and methods.
______ Isa relationships in an EER diagram represent the object form of relationships.
______ An INSERT requires the input values ordered in sequence to match the attribute order in the
CREATE TABLE statement.
______ The SQL SELECT statement specifies essentially what is to be answered, not how.
______ Triggers provide a mechanism to monitor specific changes to a table and perform additional
actions based on the altered data.
______ JDBC uses a call level interface and access protocol for embedded SQL in Java.
______ The SQLSTATE system variable holds a 5 digit error or success code string for the last SQL
operation.
Spring 2017
CS 370 Exam 2
Page 2
2. JDBC and embedded SQL short answers.
[15 pts]
a. What is the purpose of the database driver in a JDBC application? What is the advantage of it
being a separate class library? [3]
b. Describe in detail what is supplied to establish a database connection. What can go wrong?
[4]
c. Describe a prepared statement in JDBC. What is the advantage of using one? [3]
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");
. . . .
d. What is the purpose of a cursor in the result set? [2]
e. Describe what is happening in the assignment statements in the loop of the code above. [3]
3. Security. Describe what an access matrix is used for in database security. What SQL statements
are used to manage security?
[8 pts]
Spring 2017
CS 370 Exam 2
Page 3
4. Assume the object database schema segment below. Answer the short questions.
[7 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 Set Faculty has Advisor
Inverse Faculty::advises;
};
a. What are all of the top level attributes of a Student data object? [3]
b. How many classes can a student enroll in? [1] ______
c. How many Faculty can serve as an advisor for a Student? [1] ______
d. Name one class that refers to the Student class. [1] _______________
e. What would the getGpa() method correspond to, or implement, in an ER diagram? [1]
Spring 2017
CS 370 Exam 2
Page 4
For the remaining questions, use the following relational schema for a company database tracking trips
and expenses. Keys are underlined. The attributes should be self-evident. If not, please ask for
clarification. Departure and return dates can be past or future.
EMPS (empId, name, address, zip, phone, deptId, jobTitle, salary)
CSZ (zip, city, state)
DEPTS (deptId, deptName, deptMgrId)
TRIPS (tripId, destinationCity, departureDate, returnDate, empId)
EXPENSES (tripId, item, date, amount)
[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 who earn more than $50000 in salary.
b) List employee names and job titles of those who are currently traveling. You can use NOW()
to refer to today.
c) List all the expense items, dates, amounts and destination city for employee John Dough.
Spring 2017
CS 370 Exam 2
Page 5
EMPS (empId, name, address, zip, phone, deptId, jobTitle, salary)
CSZ (zip, city, state)
DEPTS (deptId, deptName, deptMgrId)
TRIPS (tripId, destinationCity, departureDate, returnDate, empId)
EXPENSES (tripId, item, date, amount)
d) List employee names from the Accounting department who have not traveled (Hint: use outer
join or except and nested selects).
e) List employee names who have traveled to Paris at least twice. (Hint: group by having)
Spring 2017
CS 370 Exam 2
Page 6
5. 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();
Consider the company database schema that tracks trip expense totals at the trip level (italicized). This is
slightly different from the previous questions.
EMPS (empId, name, address, zip, phone, deptId, jobTitle, salary)
CSZ (zip, city, state)
DEPTS (deptId, deptName, deptMgrId)
TRIPS (tripId, destinationCity, departureDate, returnDate, empId, tripTotal)
EXPENSES (tripId, item, date, amount)
a) Fill in a plpgsql function in which the body recalculates the total expenses for a trip (TRIPS) based on the
amounts in the EXPENSES table. [9]
CREATE _________ updateTotals()
RETURNS trigger AS $$
BEGIN
UPDATE _________ T
SET _________ = (SELECT Sum(_________) from __________ E
WHERE E.________ = NEW.tripID)
WHERE _____ .tripId = _______.tripId;
RETURN null;
END;
$$ LANGUAGE ________;
b) Code the trigger(s) you would need to ensure the totals are kept accurate. [6]