Summary: in this tutorial, you will learn how to manage databases in MySQL. You will learn how to create a new databases, remove databases from database catalog and display all databases in the MySQL database server.

Let’s start creating a new database in MySQL.

Creating Database

Before doing anything else with the data, you need to create a database. A database is a container of data. It stores contacts, vendors, customers or any kind of data you can think of. In MySQL, a database is a collection of objects that are used to store and manipulate data such as tables, database views, triggers, stored procedures…etc.

To create a database in MySQL, you use CREATE DATABASE statement as follows:

1
CREATE DATABASE [IF NOT EXISTS] database_name;

Let’s examine the CREATE DATABASE statement in greater detail:

  • Followed by CREATE DATABASE statement is database name that you want to create. It is recommended that database name should be meaningful and descriptive.
  • The IF NOT EXISTS is an optional element of the statement. The IF NOT EXISTS statement prevents you from error of creating a new database that already exists in the database catalog. You cannot have 2 databases with the same name in a same database catalog.

For example, to create classicmodels database, you need to execute the CREATE DATABASE statement as follows:

1
CREATE DATABASE classicmodels;

After executing the statement, MySQL will returns a message to notify if the new database  has been created successfully.

Displaying Databases

SHOW DATABASE statement displays all databases in your database server. You can use SHOW DATABASE statement to check the database you’ve created or to see all the databases on the database server before you create a new database, for example:

1
SHOW DATABASES;

show-database

Selecting a database to work with

Before working with database you must tell MySQL which database you want to work with by using the USE statement.

1
USE database_name;

You can select the sample database classicmodels  using the USE statement as follows:

1
USE classicmodels;

From now on all operations such as querying data, create new tables or stored procedures which you perform, will take effects on the current database.

Removing Databases

Removing database means you delete the database physically. All the data and related objects inside the database are permanently deleted and cannot be undone. So it is very important to execute this query with extra cautions. To delete a database, you use the DROP DATABASE statement as follows:

1
DROP DATABASE [IF EXISTS] database_name;

Followed the DROP DATABASE  is the database name that you want to remove. Similar to the CREATE DATABASE statement, the IF EXIST is an optional part to prevent you from removing database that does not exist in the database catalog.

If you want to practice with the DROP DATABASE statement, you can create a new temporary database, make sure that it is created and remove it. Take a look at the following queries:

1
2
3
CREATE DATABASE IF NOT EXISTS temp_database;
SHOW DATABASES;
DROP DATABASE IF EXISTS temp_database;

In this tutorial, you’ve learned various statements to manage databases including creating a new database, removing an existing database, selecting a database to work with and displaying all databases.

 

From http://www.mysqltutorial.org/