Page 392 - Beginning PHP 5.3
P. 392
Part III: Using PHP in Practice
Press Enter, and MySQL creates your new database. You can see a list of all the databases in the system —
including your new database — by typing the command SHOW DATABASES :
mysql > SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydatabase |
| mysql |
+--------------------+
3 rows in set (0.00 sec)
Don ’ t forget to type a semicolon at the end of a command or statement before pressing Enter.
You can see that this system has three databases. information_schema and mysql are databases
connected with the operation of MySQL itself, and mydatabase is the database you just created.
Creating a Table
As you know, tables are where you actually store your data. To start with, you ’ ll create a very simple
table, fruit , containing three fields: id (the primary key), name (the name of the fruit), and color (the
fruit ’ s color).
The first thing to do is select the database you just created. Once you ’ ve selected a database, any
database - manipulation commands you enter work on that database. Type the following:
USE mydatabase;
Press Enter, and you should see:
Database changed
mysql >
Now create your table. Type the following at the mysql > prompt:
mysql > CREATE TABLE fruit (
- > id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
- > name VARCHAR(30) NOT NULL,
- > color VARCHAR(30) NOT NULL,
- > PRIMARY KEY (id)
- > );
Press Enter at the end of each line. Don ’ t enter the “ - > arrows; MySQL displays these automatically
“
each time you press Enter, to inform you that your statement is being continued on a new line.
If all goes well, you should see a response similar to the following:
Query OK, 0 rows affected (0.06 sec)
354
9/21/09 9:11:12 AM
c12.indd 354 9/21/09 9:11:12 AM
c12.indd 354