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.
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.
CREATE DATABASE db_name;
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:
CREATE DATABASE [IF NOT EXISTS] db_name [CHARACTER SET charset] [COLLATE collation];
The options have the following meanings:
mysql> SHOW CREATE DATABASE mydb\G *************************** 1. row *************************** Database: mydb Create Database: CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_icelandic_ci */
DROP DATABASE db_name;
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.
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.