How to create database using MySQL

In this MySQL database tutorial, we will learn how to create a database using MySQL. With the help of a database, we can store our data and files in an organized manner and when required we can manage and access those database files easily.

There are two methods to create a database in MySQL, the first method is with the help of the MySQL command line, and the second one is with the help of the GUI tool MySQL Workbench.

Let’s move ahead and learn each of the methods in detail with an example as follow.

Creating Database using MySQL Workbench

MySQL Workbench is a versatile and commonly used database GUI tool. It provides data modeling, SQL development, and comprehensive administration tools for server configuration, user administration, backup, and much more.

To create a database in MySQL Workbench firstly you need to install MySQL and MySQL Workbench. Now after the installation follows the below steps to create a database table.

Step 1: Open the MySQL Workbench and it will show the screen as shown in the below picture, on clicking Local instance MySQL80 it will redirect you to the MySQL Workbench login where you need to enter the password you created at the time of MySQL installation.

MySQL database Workbench
MySQL database Workbench

Step 2: Enter the password and you will get redirected to MySQL Workbench.

MySQL Workbench login
MySQL Workbench login

Step 3: Open the navigation tab at the top of the page and open the schema function as shown in the picture below. Through the Schema menu, we can create the database and can also view the list of databases that were created previously. Now to create a database click on the database menu shown in the picture below.

MySQL Workbench database icon
MySQL Workbench database icon

Step 4: After clicking on the database, the screen will show the new schema where it will ask to name the database. In this task, we will call it employeedb and click the apply button.

MySQL Workbench database schema
MySQL Workbench database schema

Step 5: Another screen will appear where it will ask to apply the script on the database, again click Apply button.

Create Database in MySQL
Create a Database in MySQL

Step 6: A Screen will appear with the statement Execute SQL Statement. On clicking the Finish button it will create a database.

MySQL Workbench database
MySQL Workbench database

Step 7: To view the created database we go to the schema section on the left side of the screen. Under the SCHEMAS section, you will see the created database. We can see more information about the created database by clicking the icon “i”. The information window will display options like Tables, Columns, Indexes, Store Procedures, etc.

Database MySQL Workbench in Windows
Database MySQL Workbench in Windows

With this, we have successfully created a database in MySQL using the MySQL Workbench. Now, we will proceed and learn to create the database using the MySQL command line.

Read How to Create Table in MySQL

Creating a database using the MySQL command line

To create a Database in MySQL command line first and enter the password then it will show the screen as shown in the picture below.

Database MySQL Command line
Database MySQL Command line

Next, we will create a new database using the CREATE DATABASE statement with the syntax shown in the below code.

CREATE DATABASE [IF NOT EXIST] "database_name"
[CHARACTER SET 'charset_name']  
[COLLATE 'collation_name'];  

The parameters shown in the above code are described as follows:

  • database_name = Name of database we are going to create. Make sure the name you are using is not already existed in your database otherwise, it will return an error. To avoid this error we use the IF NOT EXIST‘ clause.
  • charset_name =  It is the name of the character set to store every character in a string. It is optional to use this field.
  • collation_name = It compares characters in the particular character set. It is optional to use this field.

Let’s see an example and learn to create a MySQL database using the MySQL command line. The steps are as follow:

Step 1: We will create the database “studentsdb” using the below command in the MySQL command line terminal.

mysql> CREATE DATABASE studentsdb;
Command line database MySQL
Command line database MySQL

Step 2: In this step, we will review the created database using the below code statement.

mysql> SHOW CREATE DATABASE studentsdb;
MySQL Command line client database
MySQL Command line client database

Step 3: Now we will use the SHOW DATABASES query to get the list in we will view the database which is newly created along with the previously created database (if created earlier).

mqsql> SHOW DATABASES;
command line MySQL database
command line MySQL database

Later on, we can use this database to create tables by using the query USE studentsdb. So, with this, we have learned to create a database using the command line in MySQL.

Conclusion:

In this MySQL database tutorial, we have learned about how to create a Database in MySQL Workbench and MySQL Command line.

We have also learned how to view the created database in the MySQL command line client and in MySQL Workbench.

You may also like to read the folllowing articles: