| CSC 325 | Grinnell College | Fall, 2008 |
| Databases and Web Application Design | ||
This laboratory exercise provides practice with working directly with a MySQL database.
On the CS/Math/Stat network, the MySQL database facility resides on server aiken. The package is open source, and may be obtained from www.mysql.com.
Prepare to use MySQL:
mysql -pThe -p indicates that you will supply a password. Use the MySQL password distributed in class.
use csc325;As with the C and Java programming languages, many MySQL commands end with a semicolon.
Within a relational database, the table is the basic organizational unit. Data are organized into rows, called records. Columns indicate a type of information; and each column is given a title, called a field name.
For example, in the csc325 database, table faculty contains the listing of faculty for the Department of Mathematics and Computer Science for 1998-1999. The first four records for this table are:
+---------+---------+-----------+------------------------------------+------------------------+-----+------------+ |facultyID|first |last |title |email |phone|office | +---------+---------+-----------+------------------------------------+------------------------+-----+------------+ | 1 |Arnold |Adelberg |Myra Steele Professor of Mathematics|adelbe@math.grin.edu |4201 |Science 2405| | 2 |Nathaniel|Borenstein |Noyce Visiting Professor |borenste@cs.grinnell.edu|3017 |Science 1131| | 3 |Marc |Chamberland|Assistant Professor of Mathematics |chamberl@math.grin.edu |4207 |Science 2410| | 4 |Pamela |Ferguson |Professor of Mathematics |fergusoa@ac.grin.edu |3017 |Science 1131|
In this table, the first row shows the field names. Each subsequent row provides a separate directory entry, beginning with a unique facultyID number. Thus, the first record provides information for Arnold Adelberg, the Myra Steele Professor of Mathematics, whose office was Science 2405 and whose telephone extension was 4201. Prof. Adelberg's facultyID in this record is the integer 1.
Issue the following commands to get an overview of the csc325 database, its faculty table, and several versions of the SELECT statement.:
show tables; describe faculty; SELECT * FROM faculty; SELECT last, email FROM faculty WHERE title > "Associate"; SELECT faculty.* FROM faculty WHERE last="Moore" AND first="Emily";
In each case, record the results obtained.
It is common to capitalize keywords in SQL queries, but to what extent is capitalization necessary?
Using the textbook for reference, develop queries to retrieve the following:
Subsequent parts of this lab will ask you to change existing entries in the faculty table and to add new entries. Periodically, however, you may want to restore the original database table.
The insert statement adds new records to the database. The simplest version of this statement specifies each field, in the order specified in the database. Thus, the following statement would add Arnold Adelberg's record to the campus directory:
INSERT INTO faculty VALUES
(NULL, "Arnold", "Adelberg",
"Myra Steele Professor of Mathematics",
"adelbe@math.grin.edu", 4201,"Science 2405");
As this example illustrates, it is common for keywords in an SQL statement to be capitalized, although this is not necessary.
The complete statement to add all records to this departmental database lists each record in parentheses, separated by commas.
When inserting records, we could include the facultyID number explicitly, using a statement:
INSERT INTO faculty VALUES
(1, "Arnold", "Adelberg",
"Myra Steele Professor of Mathematics",
"adelbe@math.grin.edu", 4201,"Science 2405");
However, since facultyID is defined with auto_increment, we can leave that field as NULL, in which case the database will determine an appropriate value; more about auto_increment shortly.
In yet another variation of the insert statement, we specify explicitly which value goes with which field:
INSERT INTO faculty
set first = "Arnold",
last = "Adelberg",
title = "Myra Steele Professor of Mathematics",
email = "adelbe@math.grin.edu",
phone= 4201,
office= "Science 2405";
Although this form is somewhat wordy, it clearly identifies exactly which value goes where within a record. Again, since no facultyID is given, the auto_increment attribute will assign a new record number to this field.
Insert 3 records of your choosing into the database. To avoid confusion among other students doing this lab, use your last name in each case. Also, use several variations of the insert statement to perform this task.
The Delete statement removes records from the database.
In using Delete be sure to limit the scope of your work, since
this statement has the potential to remove all records from the
database.
The following statement removes all records for those with the last name "Hill" from the database:
DELETE FROM faculty WHERE last="Hill";
Delete exactly one of your newly-inserted records from the database.
The Update statement changes fields within records.
As with Delete be sure to limit the scope of a Update,
since this statement has the potential to change all records in the
database.
The following statement updates Adelberg's e-mail and office information from the 1998-1999 directory:
UPDATE faculty
SET email="adelbe@math.grinnell.edu",
office="Carnegie Hall 308"
WHERE last="Adelberg";
Change one of your remaining two newly-created records, perhaps changing a first name and phone extension.
For this laboratory exercise, we will use the database csc223 that has been created specifically for this discussion.
Once a database has been established, the user(s) designated by the MathLAN Administrator can grant access to the database to other users. For the csc325 database, this adding of permissions utilizes a SQL statement with one of two forms:
GRANT insert, select, update, delete, alter
ON csc325.*
TO username IDENTIFIED BY 'password';
GRANT insert, select, update, delete, alter
ON csc325.*
TO username;
Each form allows the person with the given username to access all tables related to the csc223 database. The first form specifies the password for that access; the second form assumes the user is already known to MySQL and will use the existing password. Specific capabilities allow the specified user to add new records to any table (insert), retrieve records (select), modify existing records (update), remove current records (delete), and change the design of a table (e.g. add, remove, or change columns) (alter). Additional permissions are possible:
For the current lab, permission has been granted for a group account to insert, select, update, delete, alter. Username and password information will be distributed in class, rather than posted on this Web site.
The creation of a table involves a specification of the various fields for that table. For the current directory example, this might be done with the following statement:
create table faculty ( facultyID bigint(20) unsigned not null auto_increment, first varchar(25) not null, last varchar(25) not null, title varchar(255) not null, email varchar(255) not null, phone bigint(20) unsigned not null, office varchar(255) not null, primary key (facultyID) );
This SQL statement establishes a new table, called faculty. This table has 7 fields:
Further, the specification indicates that none of these fields can be null: that is, each of these fields must contain data, and cannot be omitted from a record.
The facultyID field has two additional properties. First, it is a "primary key", meaning that the value in this field must be unique; two records are not allowed to have the same facultyID. In this case, the primary key is a single field, although in other tables a primary key could involve a combination of fields that together would identify a record uniquely. Second, when information is inserted into this table, one could specify the facultyID explicitly, but one also could ask the database to take then next highest integer value for the facultyID field; if this field is not specified, the database will auto_increment the highest field value to maintain a new, unique integer for this field.
Create your own table within the csc325 database.
Write SQL statements to insert at least 3 records into your new table.
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/325.fa08/lab-database-mysql.shtml
|
created 20 August 2008 last revised 20 August 2008 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |