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]
The syntax inside square brackets are optional, where
IF NOT EXISTSsyntax ensures that the particular database if exists then we do not get an error.
CHARACTER SEToption specifies the default database character set and
COLLATEoption specifies the default database collation.
Most of the times we do not require these options and so we can simplify the command to create MySQL database into
CREATE DATABASE database_name
Let’s see how we can use this command to create a database in MySQL using 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_name with the name of 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 prvious 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 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 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 suggestion then please comment below.