Specifications
The first thing you’ll need to do when you log in is to specify which database you want to use.
You can do this by typing
mysql> use dbname;
where dbname is the name of your database.
Alternatively, you can avoid the use command by specifying the database when you log in, as
follows:
mysql dbname -h hostname -u username -p
In this example, we’ll use the books database:
mysql> use books;
When you type this command, MySQL should give you a response such as
Database changed
If you don’t select a database before starting work, MySQL will give you an error message
such as
ERROR 1046: No Database Selected
Creating Database Tables
The next step in setting up the database is to actually create the tables. You can do this using
the SQL command CREATE TABLE. The general form of a CREATE TABLE statement is
CREATE TABLE tablename(columns)
You should replace the tablename placeholder with the name of the table you want to create,
and the columns placeholder with a comma-separated list of the columns in your table.
Each column will have a name followed by a datatype.
Here’s the Book-O-Rama schema:
Customers(CustomerID, Name, Address, City)
Orders(OrderID, CustomerID, Amount, Date)
Books(ISBN, Author, Title, Price)
Order_Items(OrderID, ISBN, Quantity)
Book_Reviews(ISBN, Review)
Listing 8.1 shows the SQL to create these tables, assuming you have already created the
database called books. You can find this SQL on the CD-ROM in the file chapter8/
bookorama.sql
Using MySQL
P
ART II
194
11 7842 CH08 3/6/01 3:38 PM Page 194