In this post, we will discuss how to create a database in MySQL using the command line and phpMyAdmin.
According to MySQL documentation, the general syntax to create a database using the command line is
CREATE DATABASE [IF NOT EXISTS] database_name [CHARACTER SET charset_name] [COLLATE collation_name]
The syntaxes inside square brackets are optional, where
IF NOT EXISTSsyntax ensures that we do not get an error in case the particular database already exists.
CHARACTER SEToption specifies the default database character set.
COLLATEoption specifies the default database collation.
Most of the time we do not require these options and so we can simplify the above query as:
CREATE DATABASE database_name
Let’s see how we can use this command to create a database in MySQL using the command line or phpMyAdmin.
Create Database in MySQL using Command Line
Note: Make sure that MySQL is installed on your machine or server otherwise download it from here.
- Open command line and log in to the MySQL Server using the root user using
mysql -u root -p. For MySQL command line root is by default selected.
- Input the password or simply press ENTER, if the password is not set to the root user.
- Type the following command
CREATE DATABASE database_name;, replacing the
database_namewith the name of the database of your choice.
SHOW databases;to see the list of databases along with the new database which you have created just now.
- The new database is created but is not in use, so type
USE database_name;replacing the database_name with the new database name which you have created in the previous steps.
The new database in MySQL is created and is ready for use.
Create Database in MySQL using phpMyAdmin
There are two ways to create a database using phpMyAdmin.
- Using SQL command
create database database_name;
- Without using any command.
Create database using SQL Command
To create a database using SQL command, simply Go to SQL tab > Enter the SQL command
create database database_name; > Run.
Note: Make sure to replace database_name with the name of your database.
Create database without using SQL Command
To create a MySQL database without using any SQL command, simply Go to DATABASES tab > Enter the database name > Create.
That’s all you need to do in order to create a database in MySQL. If you have any doubts or suggestions then please comment below.