How to create Database?

Creating a database is a first step in MySQL (or any other DB!).

In MySQL you create a database by using CREATE DATABASE:

CREATE DATABASE [IF NOT EXISTS] database_name;

IF NOT EXISTS is an optional statement and if we are not sure whether a database we want to create already exists, or not, it is suggested to include it in your CREATE statement.

Viewing Databases

In order to display existing databases in the opened MYSQL database server, SHOW DATABASES statement is used:

SHOW DATABASES;

If the database mysql_tutorials has been created, than the SHOW DATABASES statement will show 3 databases. These are:

  • information_schema
  • mysql_tutorials
  • mysql

The information_schema and mysql are the default databases installed with the MySQL server.

Selecting currently used Database

Before starting using a database, we must initiate which one to use. That is achieved with applying the statement USE as shown here:

USE mysql_tutorials;

Removing / Deleting Databases

To delete a database physically and therefore permanently from our server, a DROP DATABASE statement is used:

DROP DATABASE [IF EXISTS] mysql_tutorials;

Same as with the creation of a database, IF EXISTS is an optional statement but suggested in order to prevent errors if the database does not exists.

Example of creating and dropping database:

CREATE DATABASE IF NOT EXISTS temp_database;

SHOW DATABASES;

DROP DATABASE IF EXISTS temp_database;