How to Create Database in MySQL?

How to create database in MySQL

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

  • The IF NOT EXISTS syntax ensures that we do not get an error in case the particular database already exists.
  • CHARACTER SET option specifies the default database character set.
  • COLLATE option 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.

  1. 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. open MySQL
  2. Input the password or simply press ENTER, if the password is not set to the root user.
  3. Type the following command CREATE DATABASE database_name;, replacing the database_name with the name of the database of your choice.
  4. Type SHOW databases; to see the list of databases along with the new database which you have created just now.
  5. 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.
create a database in MySQL

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.

  1. Using SQL command create database database_name;
  2. 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.

create MySQL database using SQL command in phpMyAdmin

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.

create MySQL database without SQL command in phpMyAdmin

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.

Leave a Reply

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