Creating a Database


A database is a container for data sets. These data sets are modeled by and stored in tables. Databases (along with tables) also provide objects that MySQL users can be granted access to, allowing the activities of a user to be mostly restricted to what is inside of one database or just a set of one or more tables.

In this chapter, you'll create a database called library. Before you do, you should check the other databases in this MySQL installation and ensure that the database does not already exist.

Showing Existing Databases

Because users are assigned rights to specific databases, the current user can only see the databases for which he has rights. If you're logged in as the MySQL root user, all databases should be visible.

To view a list of databases, run the following query:

SHOW DATABASES; 


This should return a list of the existing databases. If querying a new MySQL installation, there will be two databases:

  • The mysql database (which contains administrative data and documentation for the MySQL server)

  • The test database (which is commonly used as a sandbox where ideas and features can be safely tested)

If the server is not a new installation, it will probably contain additional databases.

The output of the SHOW DATABASES command should look something like this:

+-----------------+ | Database        | +-----------------+ | mysql           | | test            | +-----------------+ 


Running the CREATE DATABASE Command

If the MySQL server you are querying does not already contain a database called library, create the database as follows. (If a database called library exists, substitute a different name in the following command and in the subsequent examples.)

CREATE DATABASE library; 


Your MySQL client should display a message like

Query OK, 1 row affected (0.01 sec) 


If you run the SHOW DATABASES query again, you will see that the library database has been created.



MySQL Phrasebook. Essential Code and Commands
MySQL Phrasebook
ISBN: 0672328399
EAN: 2147483647
Year: 2003
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net