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

Concurrency control wikipedia , lookup

Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Ingres (database) wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

SQL wikipedia , lookup

Clusterpoint wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

PL/SQL wikipedia , lookup

Join (SQL) wikipedia , lookup

Database model wikipedia , lookup

Relational algebra wikipedia , lookup

Relational model wikipedia , lookup

Transcript
Fall 2014
CS 370 / IT 376 Exam 2
Page 1
Database Management Systems
11/7/14
Name__________________________
1. True/False.
[25 pts]
______ A 1NF relation has no null values or multi-valued attributes.
______ A 1NF relation’s primary key is exactly one attribute.
______ Normalization attempts to reduce or eliminate redundant facts stored in a database table tuple.
______ Decomposition of a table for normalization results in two or more tables to replace the original
table.
______ When rejoining the tables from the decomposition process, tuples are not lost from the original
table—it is a lossless join decomposition.
______ 3NF decomposition removes transitive functional dependencies.
______ In a CREATE TABLE command, attribute entries may be omitted.
______ In a CREATE TABLE command, each attribute must have a data type associated with it.
______ The FLOAT 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.
______ 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.
_______A view is referred to like other tables in a SELECT.
______ Besides a shorthand reference to the table throughout the query, using an alias makes the query
run much faster as a result.
______ DELETE-ing from a view that is based on a natural join of its base tables is acceptable.
______ A view can provide a rejoining of decomposed relations.
______ In an object database, the database objects must have persistent storage in secondary storage.
______ In an object database, an object can be related to many other objects through the set collection.
______ NOT NULL UNIQUE tags on an attribute implies the attribute is a candidate key.
______ JDBC uses a statement level interface and access protocol for embedded SQL in Java.
______ The SQLSTATE system variable holds a 5 digit error or success code string of the last SQL
operation.
______ The UML diagram rectangles essentially represent entities, their attributes and methods.
______ Isa “relationships” in an EER diagram represent inheritance.
______ An INSERT requires the input values sequence to match the attribute sequence when the
attributes are specified in the INSERT statement.
______ The SQL SELECT statement specifies not only the operations the server uses to answer the
query, but the sequence of steps as well.
______ Triggers provide a mechanism to monitor specific changes to a table and perform additional
actions based on the altered data.
Fall 2014
CS 370 / IT 376 Exam 2
Page 2
2. Answer the short questions below regarding this Postgres trigger on the Presidents database.
[10 pts]
CREATE FUNCTION resetPopTotal()
RETURNS trigger AS $$
BEGIN
UPDATE state_stats
SET attrValue = (SELECT Sum(pop) from states)
WHERE attrName = 'total';
RETURN null;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER UpdateTotalTrigger
AFTER UPDATE OR DELETE OR INSERT ON states
FOR STATEMENT EXECUTE PROCEDURE resetPopTotal();
a.
What are the events that would cause the trigger activation? [3]
b.
What is the trigger’s granularity? [2]
c.
Explain the action of the trigger. Alternatively, how can you characterize/narrate the integrity
constraint that is maintained here? [5]
Fall 2014
CS 370 / IT 376 Exam 2
Page 3
3. JDBC and PHP short answers.
[20 pts]
a. What is the purpose of the initially loaded database driver in a JDBC application? How is it
useful as a separate class library? [4]
b. Describe 3 elements of, or established in, a database connection (JDBC or PHP). [6]
i.
ii.
iii.
c. What is the advantage of using a JDBC prepared statement for querying? [3]
d. What is a cursor in the result set? And how is it used and advanced? [4]
e. Below is a segment of Java code that is processing a result set. Describe what is happening in
the assignment statements. [3]
ResultSet res = stmt.executeQuery(query);
while(res.next()){
stateName = res.getString("STATE");
population = res.getInt("POP");
. . . .
Fall 2014
CS 370 / IT 376 Exam 2
Page 4
4. Assume the object database schema segment below. Answer the short questions.
[10 pts]
class Person
{ attribute pId int;
attribute name string;
attribute Struct Addr(string street,
string city,
string state,
string zip) address;
attribute phone string;
string getName( );
void setName(string newName);
};
class Student extends Person
{
attribute credits int;
attribute gpa real(3,2);
int getCredits( );
void addCredits(int numCredits);
relationship Set takesClass
Inverse ClassSection::hasStudent;
relationship Set earnedGrade
Inverse Grade::givenStudent;
relationship Faculty has Advisor
Inverse Faculty::advises;
};
a. What other attributes will a Student data object contain than those listed in the class? [3]
b. How many classes can a student take? [1] ______
c. How many advisors can a student have? [1] ______
d. Give an example of a composite attribute above. [2] ________________________________
e. How might you redesign the Student class to better represent the gpa attribute? Think how
GPAs are calculated. [3]
Fall 2014
CS 370 / IT 376 Exam 2
Page 5
For the remaining questions, use the following relational schema for an apartment rental company. They
track tenants or potential tenants. Apartments have a unique id and are spread around in several
towns and have different rental rates. They track the current leasing of an apartment to a tenant
as well as past leases to each apartment and tenant. Let me know if you have any other
clarifications.
TENANTS (PID, name, PriorAddress, Priorzip, phone)
CSZ (zip, city, state)
APTS (AptID, AptNo, AptAddress, AptZip, bdrms, Monthrent)
RENTED (PID, AptId, StartDate, EndDate, deposit, YTDPaid)
//past renter data are kept; if EndDate is null, the apartment is currently rented if
StartDate is null then the apartment is not rented.
[5 pts each = 35 pts]
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 attr [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 those who lived at zip 90210 prior to
renting.
b) List all names of those who lived in ‘PA’ prior to renting.
c) List names and phones of past renters (or have switched apartments) who had monthly
payments greater than $1500.
Fall 2014
CS 370 / IT 376 Exam 2
Page 6
d) Get a list of names and PIDs in the tenant table that have not rented. (Hint: Subselect and IN
operator or outer join useful.)
e) List names of renters who have rented at lease two different apartments. (Hint: use group by
having)
f) Get a list that displays the count of apartments and the total rent that are at each different
address. There are many apartment units at the same address/zip.
g) List names and phone numbers who are renting, or have rented, the apartments with the most
bedrooms. You will need a subselect to determine what is the largest number of bedrooms.