Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Selecting, Creating, Dropping, and Altering Databases

MySQL provides several database-level statements: USE for selecting a default database, CREATE DATABASE for creating databases, DROP DATABASE for removing them, and ALTER DATABASE for modifying global database characteristics.

Note: From MySQL 5.0.2 on, you can use the keyword SCHEMA as a synonym for DATABASE in any statement where the latter occurs.

Selecting Databases

The USE statement selects a database to make it the default (current) database for a given connection to the server:

USE db_name;

You must have some access privilege for the database or you cannot select it.

It is not strictly necessary to select a database explicitly. If you have access to a database, you can use its tables without selecting it if you use qualified names that identify both the database and the table. For example, to retrieve the contents of the president table in the sampdb database without selecting the database first, write the query like this:

SELECT * FROM sampdb.president;

However, it's much more convenient to refer to tables without having to specify a database qualifier.

Selecting a database doesn't mean that it must be the default for the duration of the connection. You can issue any number of USE statements to switch back and forth among databases. Nor does selecting a database limit you to using tables only from that database. While one database is the default, you can refer to tables in other databases by qualifying their names with the appropriate database identifier.

When your connection to the server terminates, any notion by the server of which database was the default for the connection disappears. That is, if you connect to the server again, it doesn't remember what database you had selected previously.

Creating Databases

To create a database, use a CREATE DATABASE statement:


The conditions on database creation are that the name must be a legal identifier, the database must not already exist, and you must have sufficient privileges to create it.

When you create a database, the MySQL server creates a directory under its data directory that has the same name as the database. The new directory is called the database directory. The server also creates a db.opt file in the database directory to store database attributes.

CREATE DATABASE supports several optional clauses. The full syntax is as follows:

 [CHARACTER SET charset] [COLLATE collation];

The options have the following meanings:

  • Normally, an error occurs if you try to create a database that already exists. To suppress this error and create a database only if it does not already exist, add an IF NOT EXISTS clause:

  • You can use the CHARACTER SET and COLLATE clauses to set the database default character set and collation. For example:

  • CREATE DATABASE mydb CHARACTER SET utf8 COLLATE utf8_icelandic_ci;

    The character set and collation attributes are stored in the db.opt file. They are used when you create a new table: If the table definition does not specify its own default character set and collation, the database defaults become the table defaults.

    If CHARACTER SET is given without COLLATE, the default collation for the character set is used. If COLLATE is given without CHARACTER SET, the first part of the collation name determines the character set. If no character set or collation is specified, the database defaults are taken from the server default character set and collation.

    The character set must be one of those supported by the server, such as latin1 or sjis. The collation should be a legal collation for the character set. For further discussion of character sets and collations, see Chapter 3.

To see the definition for an existing database, use a SHOW CREATE DATABASE statement:

*************************** 1. row ***************************
       Database: mydb
Create Database: CREATE DATABASE ´mydb´
                 /*!40100 DEFAULT CHARACTER SET utf8
                 COLLATE utf8_icelandic_ci */

Dropping Databases

Dropping a database is just as easy as creating one, assuming that you have sufficient privileges:


The DROP DATABASE statement is not something to use with wild abandon. It removes the database and all tables within it, so the database is gone forever unless you have been making backups regularly. In other words, don't try this statement just to see how it works unless you don't care about the database contents.

A database is represented by a directory under the data directory, and the directory is intended for storage of table data. If a DROP DATABASE statement fails, the reason most likely is that the database directory contains non-table files. DROP DATABASE will not delete such files, and as a result will not delete the directory, either. This means that the database directory continues to exist (albeit empty of any tables) and will show up if you issue a SHOW DATABASES statement. To really drop the database if this occurs, manually remove any remaining files in the database directory and the directory itself.

Altering Databases

The ALTER DATABASE statement makes changes to a database's global attributes. Currently, the only such attributes are the default character set and collation:

ALTER DATABASE db_name [CHARACTER SET charset] [COLLATE collation];

The earlier discussion for CREATE DATABASE describes the effect of the optional CHARACTER SET and COLLATE clauses.

  • + Share This
  • 🔖 Save To Your Account