CS395 Spring 2002

SQL Assignment

Due Wednesday 5/15 at 5 PM.

This assignment should be done individually. You may work with other people, get ideas, help each other debug, and so on; but the scripts that you submit should be your own.

Setting up Oracle

In order to be able to use Oracle, you furst need to add the following lines to your .cshrc file in your home directory:

setenv ORACLE_SID prism
setenv ORACLE_HOME /home/oracle/OraHome1
set path=($path /home/oracle/OraHome1/bin)

Make sure that you close all your terminal windows and open up new ones after making this change.

The Database

This assignment will use a database containing college-type data. The data is located in seven flat files, all with the extension ".data". You can find them in the directory /Accounts/courses/cs395/dmusican/sqldata/.

Your first goal is to import this data into relations in Oracle. The schema of the database that you should create is as follows (keys are in bold):

student (sid, sname, sex, age, year, gpa)
dept (dname, numphds)
prof (pname, dname)
course (dname, cno, cname)
major (dname, sid)
section (dname, cno, sectno, pname)
enroll (sid, dname, cno, sectno, grade)

To import a flat file into Oracle, you need to use the SQL*Loader utility. Here's how:

  1. Create a new directory for this assignment.
  2. Copy all the data files into your new directory.
  3. Copy "enroll.ctl" from the course directory to your new directory.
  4. Take a look at the contents of enroll.ctl. This is an SQL*Loader control file, which tells Oracle how to read and load in external data.
  5. Start up Oracle by typing sqlplus at the command line.
  6. Create a table in Oracle called enroll, which will contain the appropriate fields. I recommend that you put all your "CREATE TABLE" statements in a script called createtables.sql.
  7. Quit SQL*Plus.
  8. Load in the data by entering the following at the Linux prompt, then entering your Oracle userid and password as requested:

    sqlldr control=enroll.ctl

  9. Start up SQL*Plus again and verify that the data made it into Oracle correctly.

You should write control files similar to enroll.ctl for the other six relations, and import the data.

Queries

Design SQL queries that answer the questions given below (one query per question) and run them using Oracle's SQL*Plus. The query answers should be duplicate free, but you should use distinct only when necessary. The following questions are roughly in the order of degree of difficulty.

1. Print the names of professors who work in departments that have fewer than 50 PhD students.
2. Print the names of the students with the lowest GPA.
3. For each Computer Sciences class, print the class number, section number, and the average gpa of the students enrolled in the class.
4. Print the names and section numbers of all classes with more than six students enrolled in them.
5. Print the name(s) and sid(s) of the student(s) enrolled in the most classes.
6. Print the names of departments that have one or more majors who are under 18 years old.
7. Print the names and majors of students who are taking one of the College Geometry courses.
8. For those departments that have no major taking a College Geometry course print the department name and the number of PhD students in the department.
9. Print the names of students who are taking both a Computer Sciences course and a Mathematics course.
10. Print the age difference between the oldest and the youngest Computer sciences major.
11. For each department that has one or more majors with a GPA under 1.0, print the name of the department and the average GPA of its majors.
12. Print the ids, names and GPAs of the students who are currently taking all the Civil Engineering courses.

What to Turn In

All of your SQL queries should be contained in a single file called script.sql, which you should electronically submit. This file should be appropriately formatted and documented. Additionally, you should spool your output from SQL*Plus to an external file and submit this as well. To do this, type "spool answers.txt" within SQL*Plus. All SQL*Plus output will then be echoes to the file answers.txt. You should run your script while spooling your output, then electronically submit this answers.txt file. Finally, you should also submit your control files.