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.
To create a database using the mysqladmin utility:
show databases;
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;
USE test;
The image below also shows what happens if we try to create a database that already exists.