Page 391 - Beginning PHP 5.3
P. 391
Chapter 12: Introducing Databases and SQL
Creating additional MySQL users is outside the scope of this book, but you can find out how to do it in
the Account Management Statements section of the MySQL manual ( http://dev.mysql.com/doc/
refman/5.1/en/account-management-sql.html ).
The mysql program stores a list of your most recently entered statements. (Press the Up key to move
back through the items in the list.) For security reasons, it ’ s a good idea to delete this list once you ’ ve set
the root password, because the password will appear in the SET PASSWORD FOR lines in this list. To do
this on Ubuntu and Mac OS X, exit the command - line tool and delete the .mysql_history file in your
home directory ( rm ~/.mysql_history ). On Windows, simply exit the command - line tool then close
the Command Prompt window.
By the way, if you ’ re not comfortable with the command - line tool, many graphical applications are
available that you can use to administer and talk to your MySQL server. Try the free MySQL
Administrator and MySQL Query Browser programs, available from http://dev.mysql.com/
downloads/gui-tools/5.0.html . A good Web - based tool is phpMyAdmin ( http://www
.phpmyadmin.net/ ), which also comes bundled with WampServer on Windows (click the
WampServer taskbar icon to access it).
A Quick Play with MySQL
Now that you ’ ve set up the MySQL root user, you can start working with databases. In the following
sections, you create a new database, add a table to the database, and add data to the table. You also learn
how to query databases and tables, update data in tables, and delete data, tables, and databases.
Most of the examples in the following sections show commands, statements, and other SQL keywords
being entered using all - uppercase letters. Though SQL keywords are traditionally in uppercase, MySQL
also lets you enter keywords in lowercase. So use lowercase if you prefer.
Creating a New Database
It ’ s easy to create a new MySQL database. First, fire up the MySQL command - line tool using the same
method that you used when changing the root password. Open a shell prompt, change to the correct
folder (if using Windows or Mac OS X), and then on Ubuntu or Windows type:
mysql -u root -p
On the Mac type:
./mysql -u root -p
Press Enter. Now enter the root password you specified earlier, and press Enter again. You should see the
prompt appear:
mysql >
To create a new database, all you have to do is use the CREATE DATABASE command. Type the following to
create a new database called mydatabase :
CREATE DATABASE mydatabase;
353
9/21/09 9:11:12 AM
c12.indd 353
c12.indd 353 9/21/09 9:11:12 AM