Laboratory Exercise 5

Database Modification (Command-line)

CMP 420: Database Systems
Lehman College, City University of New York
13 March 2003


Unix Commands

Each lab begins with several Unix commands that will be useful for completing today's lab and future labs and assignments. Try the following commands:

Creating Databases

For today's lab, we're going to create a database to hold information about students and courses.  This lab will have three tables: Students, Courses, and Enrollment.  The Students table will keep track of students' ID number, name, address, and phone number.  The Courses table will keep track of the courses' registration code, department, and course number.  The Enrollment table will keep track of which students are enrolled in which courses.  Since the
IDNumber is the key for Students and registrationCode is the key for Courses, the Enrollment table will only need to store those two attributes to tell which students are enrolled in which courses.

First, create a database at the command line for today's lab (see Lab 2 for details).  

Next, create the three tables in psql using the CREATE TABLE command.  Details of this command can be found in Chapter 6 of the textbook or in the PostgreSQL manual (see the list of commands).  The three tables should be:
Check using the "\d" command to make sure all tables were created correctly.

Making Data for your Database

One way to enter the information into your database is to type in every line by hand.  Another way is to read in the database stored as a file.  In Lab 2, we used a sample database from PostgreSQL Notes by Richard Huxton.  For this lab, you need to modify his program to create data for today's database.  First, download his perl program, and try running it (see directions on his webpage).  It will create a data file much like that used for Lab 2.  

Several quick hints for reading and modifying perl:

Next, modify the perl program to produce:
Save the output of your perl program to a file.  You might have to modify it several times before it matches your database schema. Now, load it into psql and try some simple queries to make sure that it works.

Database Modification

Using the notes from class and the list of commands for PostgreSQL, find the commands that:

Final Comments

Before leaving the lab, you need to log out and shut down your computer. First, you should quit all applications. Then, go to the K menu in the bottom left hand corner and select Log Out. Choose the Shut Down option, and shut off the monitor when you're done.

Remember to pick up any files you sent to the printer and to take any personal belongings. It is difficult to get back into the lab later (since it's locked when not in use), so, it's worth making sure you haven't forgotten anything.