CS40 Exam #2 November 14, 2001 Name: Except where explicitly noted, all of the questions on this exam refer to the database defined by the relational schemas given on the last page of this exam. 1. Suppose that the Student Records database is to be augmented by a table called Testbank, which will contain exam questions. Assume that each question must relate to one or more courses. The schema is given on the last page. a. Write an SQL statement that will create this table, using the most appropriate data types. (You do not have to set any constraints other than those required by the problem statement.) [8 points] b. Write an SQL statement to insert the question: What is your favorite color? for CS40, with the answer Blue. No yell-oooooaaaah!. [6 points] c. Without making any assumptions about existing database contents, explain a possible anomaly that might occur when this insertion is made. [6 points]
2. a. You have cleverly gained access to the airtight Furman grades database. (No easy task!) Your student number is 19, your roommate s is 34 and your girl/boyfriend s is 41. Write an SQL statement to change all grades to A for all classes for the three of you. (For full credit, you must not use stunum= three times.) [7 points] b. Write a SQL statement to delete all of the records in the Course table for CS40. [7 points] c. What would happen as a result of the statement given in part (b) [6 points] if Sections was defined using the clause ON DELETE CASCADE? if Testbank was defined using the clause ON DELETE SET NULL? if Sections or Testbank was defined using the clause ON DELETE RESTRICT? 3. Write SQL queries for the following: [10 points each] a. List (in alphabetical order by name) the course number and student name for all courses taken by CS majors. b. List the names of all students who have received an A in a course taught by Dr. Gabbert. Don t repeat any names.
4. Write SQL queries for the following: [10 points each] a. List the student number, student name and count of grades for each student. The column header for the grade count should be NumGrades. b. List the student numbers and student names of all students who have received at least one grade, but have never failed a class. 5. a. List and briefly explain the steps that must be taken by a Java program to run a SELECT statement on a database using JDBC. You should add at least four steps to the list below. Your answers should demonstrate that you understand what you re talking about, not just that you can regurgitate something you read. [12 points] First step Create a null (empty) Connection object from the java.sql package Last step Loop through the ResultSet object, printing the attributes for each row, formatted appropriately. b. Consider the PINonlyGrades.php and JDBCTest.java applications that we studied in the last two labs. Suppose we wanted to modify them to run a different query against data in a different DBMS. For which application would it be easier to do this? Justify your answer with specific details. [8 points]
6. Short Answer: [5 points each] a. What is the purpose of the getmetadata method of the ResultSet class? b. In the following SQL statement: SELECT title FROM Movie WHERE genre= comedy ; which relational algebra operation is used to implement the underlined part? c. Which of the database management systems that we have studied support QBE as a predominant technique? d. Suppose you have executed the following SQL command in Oracle: create view StudentGrades as select st.stunum, st.stuname, g.secid, g.grade from student st, grades g where st.stunum = g.stunum; Give a short SQL statement that would produce the student number, student name, section ID and grade for every grade earned by student #19.
Relational Schemas for Course and Grade Database Table Attribute Data Type Keys and custom rules Student stunum number(4) primary key specify a value range: stunum between 8 and 20 stuname varchar2(8) class number(1) custom rule: class in ( 1, 2, 3, 4 ) major varchar2(4) Course cournum varchar2( 6) primary key courname varchar2( 30) credithr number( 1) custom rule: credithr in ( 2, 4 ) dept varchar2( 4) Prereq cournum varchar2( 6) primary key and foreign key prereqnm varchar2( 6) primary key and foreign key Cournum and prereqnm together form the primary key. Separately they are foreign keys. Sections secid number(4) primary key cournum varchar2 (6) foreign key semester varchar2(6) acadyear number(4) custom rule: year between 1997 and 2001 instr varchar2(10) Grades stunum number( 4) part of primary key and a foreign key secid number( 4) part of primary key and a foreign key Stunum and secid together form the primary key. Separately they are foreign keys. grade char 1 custom rule : grade in ( A, B, C, D, F ) Testbank questionid?? primary key, automatically generated cournum?? foreign key question?? could potentially be very long answer?? could potentially be very long