MySQL Create Database

Table of Contents

Introduction

The CREATE DATABASE command is used to create a new database in MySQL.

If you’re starting to build your own data structure, the first thing you’ll need to do is create a database.
Databases are the backbone of every application, in fact they contain all the data divided into tables.

Always carefully execute commands that interact with databases and tables because if you delete something by mistake you will lose all your data.

If you are a beginner don’t worry, this post will show you the command syntax and several practical examples to best learn how to use it.

Preliminary operations

First of all, for this tutorial I assume that you have MySQL installed on your computer.
If not, you can download it and follow the instructions to install it.

If you are using Ubuntu and you are in trouble, please check this post!

How to check list of databases in MySQL

Since we are about to create new databases, I think it is appropriate to show you a command that is used to show you which databases are present on your machine.

First you need to access the mysql console, and you can do this with this command:

sudo mysql

Once inside, just use the SHOW DATABASES command which is used to check which databases have already been created.

Let’s see the syntax first and then an example of output.

 SHOW DATABASES;

Output

SHOW DATABASES output
SHOW DATABASES output

As you can see this command returns the list of databases that are already present.
Keep in mind that some of the ones you will see in the list are system ones, for example, in my case I just created the devinsimplewords database.

In the next section we will use this command after creating a new database in order to check that it has been created correctly.

Create Database MySQL

In this section we will see how to create a database via command line using MySQL.
The command to use is CREATE DATABASE and it is very simple.

In the next sections we will see how to use it in all scenarios.

How to create a new database in MySQL

To create a new database in MySQL use the CREATE DATABASE command.
The syntax is as follows:

CREATE DATABASE <database_name>;

Pretty simple isn’t it?

Let’s try to create a database called test_db.

CREATE DATABASE test_db;

The new database will be created by running this command. To verify, you can use the command seen in the previous section.

CREATE DATABASE output
CREATE DATABASE output

How to create a new database if it does not exist in MySQL

In the previous section we saw how to create a database, in particular we created the test_db database.
But what happens if we try to repeat the same command?
MySQL does not allow us to create two databases with the same name, so an error is raised.

However in MySQL it is possible to slightly modify the CREATE DATABASE command to work around this problem.

CREATE DATABASE IF NOT EXISTS test_db;

By adding IF NOT EXISTS in fact we can run the same command several times and the database will be created only the first time.

How to select a database in MySQL

In the previous chapters we have seen how it is possible to create a database in MySQL.
However, to start using a database, you don’t just need to create it, you need to use another command first.
The command in question is USE.

The syntax is very simple, take a look here:

USE <database_name>

Please remember to substitue the placeholder with the real name.

As you can imagine this command allows you to use a certain database, basically it’s like selecting it.
From now on, all queries you launch will count against that database.
When you want to change database, just re-launch the same command but with another database.

So to recap, the steps to use a database are:

  • creation with the CREATE DATABASE command
  • selection with the USE command

Conclusion

Here we are at the end of this tutorial, as always I hope it was useful to you and that now you know how to best manage each scenario for creating new databases using MySQL.

Of course, if you are in trouble, do not hesitate to contact me or ask in the comments. I will reply as soon as possible.

If, on the other hand, everything is clear, take a look at the latest posts!

Leave a Comment

Your email address will not be published. Required fields are marked *