Open In App

MySQL CREATE DATABASE Statement

Last Updated : 18 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MySQL is a popular relational database management system that allows users to create and manage databases. As we know databases are containers for our tables where we store our data creating a database is a crucial task.

In this article, we are going to learn how we can create databases in the MySQL database management system through the command line client tool and the MySQL Workbench application. We will provide a step-by-step tutorial to create databases in both of these applications. So deep dive into this article and master how to create databases in MySQL.

MySQL CREATE DATABASE

Creating a database is one of the most fundamental tasks while working with MYSQL. A database serves as a container for tables, views, procedures, and several other database objects. The CREATE DATABASE statement is used in MYSQL to create a new database that will store all the tables that the user will create to store the data. To create a database the following syntax is followed:

Syntax:

CREATE DATABASE [IF NOT EXISTS] Nameof_database

[CHARACTER SET Nameof_charset]

[COLLATE Nameof_Collation];

  • First, the user will have to specify the name of the database he/she wants to create after the CREATE DATABASE [IF NOT EXISTS] statement. The IF NOT EXISTS statement checks if there is already a database of the same name provided by the user. If there already exists a database of that name then an error message is generated.
  • The CHARACTER SET field is an optional value that defines the name of the character set to store every character in the string. Various types of character sets are supported in MySQL however if the user skips to provide this value then the default character set is used by MySQL.
  • The COLLATE field is also an optional value that is used to compare the characters in a particular character set.

Methods to Create a Database in MySQL

There are mainly 2 ways through which MYSQL allows the users to create a database, through the MYSQL command line client and MYSQL Workbench Application.

1. Create a Database Using the MySQL Command Line Client

The command line client comes as an default tool when you install MySQL on your system .You can follow this article if you want a step by step guide to install MYSQL on your systems.

The MYSQL command line client is a command line interface that allows the users to directly interact with the MYSQL database server directly through the command. It provides a text based environment to the users for executing the SQL queries directly and allows the users to manage their databases as well. To create a database through the MYSQL command line client follow the below steps:

1. Search for MYSQL Command line Client in start.

Method1_CommandLineClient_Step1

2. Open the application and then Enter your password.

Method1_CommandLineClient_Step2

3. Run the following command to check the existing databases in the system.

SHOW DATABASES;

Output:

Method1_CommandLineClient_Step3

All databases present in the system

4. After that run the following command to create database of your choice.

CREATE DATABASE IF NOT EXISTS DATABASE_NAME

Output:

Method1_CommandLineClient_Step4

If you see the following output then you can ensure that your database was successfully created.

5. Again run the SHOW Databases command and you will see your database is created.

Output:

Method1_CommandLineClient_Step5

6. Run the following command to use the database you have just created.

USE DATABASE_NAME;

2. Create Database using the MySQL Workbench Application

You can follow this article if you want to install MYSQL Workbench on your systems.

The MySQL workbench is a visual database design and management tool that provides a user friendly Graphical user interface(GUI) to the users through which they can interact with the MySQL database directly. This application is beneficial for those users who prefers a visual approach for development of databases rather than the command line interfaces. To create a database through MySQL Workbench follow the below steps:

1. Search for MySQL workbench in start.

Method2_MYSQLWorkBench_Step1

2. Click on the following icon to create a local instance for your database.

Method2_MYSQLWorkBench_Step2

3. Enter Local as your connection name and click on the Test connection button.

Method2_MYSQLWorkBench_Step3

4. Enter your Password. The password should be the same which you set during the MYSQL installation.

Method2_MYSQLWorkBench_Step4

5. If the connection was successful then you will see the following output.

Method2_MYSQLWorkBench_Step5

6. Click on the Local instance you created .

Method2_MYSQLWorkBench_Step6

7. Click on this icon to create the database schema.

Method2_MYSQLWorkBench_Step7

8. Enter the name of your database here and select the optional charset and collation values if you want and then click on apply.
Method2_MYSQLWorkBench_Step8

9. Click on the apply button don’t change any default settings here.

Method2_MYSQLWorkBench_Step9

10. Click on the finish button to finish the database craetion.

Method2_MYSQLWorkBench_Step10

11. You will be able to see your created database on the left panel of your screen.

Method2_MYSQLWorkBench_Step11

12. Right click on the database you created and click on set as Default Schema to use your database. Now your database is ready to use.

Method2_MYSQLWorkBench_Step12

Conclusion

In this article we have learned how we can create databases in MYSQL. We have explored 2 methods step by step which allows the users to create databases in MYSQL. The choice of method completely depends on the user, if the users is not comfortable with the command line environment then they can use the second method of MYSQL workbench to create the databases they want. We hope this article has helped you to learn about the CREATE Database statment in MYSQL.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads