MySQL Create Database

In MySQL, we create a database using the CREATE DATABASE statement. MySQL implements a database as a directory containing all the files required and the tables in the database.

We can create a database using the MYSQL command line, i.e., mysqladmin or the MySQL Workbench. We will look at both options.

The basic syntax of creating a database is

CREATE DATABASE [IF NOT EXISTS] database_name
[CHARACTER SET charset_name]
[COLLATE collation_name]

The “IF NOT EXISTS” ensures that if a database exists already exists, MySQL does not create it again.

The character and collate are optional and can be dropped. MySQL uses the latin1 character set by default. The character set available is:

Collation refers to the rules used for comparing characters.

Create a database using MySQL command-line

To  create a database using the mysqladmin utility:

  • Login to MySQL command-line client with root credentials.
  • Type the following commands to see all available databases.
show databases;

  • Run the create database command
create database test;

To avoid creating a database with a name that may exist already, we run. If the If Exists call is not included, MySQL will throw an error.

create [If EXISTS] database test;

  • The database gets created.
  • To use the database, we have the command:
USE test;

The image below also shows what happens if we try to create a database that already exists.