Menu Close

Create MySQL Database

Create MySQL Database

In this article, we are going to learn all about How to create a MySQL database server using CREATE DATABASE statement.

To use the MySQL database in your project or use MySQL for learning purposes you have to create a MySQL database. There are two ways to create a database in the MySQL server: Using MySQL Console and Workbench ( MySQL GUI Application ).

Here we will see both the ways to create a new MySQL database in the MySQL server.

CREATE MySQL DATABASE

There are two ways to create database in MySQL.

  1. Using MySQL client tool
  2. Using MySQL Workbench User Interface

Create MySQL Database using MySQL Client tool

MySQL provides us a keyword CREATE DATABASE to create the database in the MYSQL server.

Syntax

CREATE DATABASE [ IF NOT EXISTS ] database_name [ CHARACTER SET charset_name ] [ COLLECT collection_name ];

Let’s explain the above syntax one by one.

  • CREATE DATABASE statement is used to create new mysql database.You have to specify the name of the database.Remember that, you have to keep unique database name.If you take database name that already exists, then MySQL raise an error.
  • IF NOT EXISTS is conditionally return True to create database if not exists.
  • Third, specify the character set and collection for the new database.

let’s see how to create a MySQL database.

  • Login into MySQL client tool and issue following command.
mysql -u root -p

It will prompt you to enter your MySQL root password to authenticate yours. Enter your MySQL root password and hit enter.

  • Before, creating new MySQL database.Check your current databases available in MySQL server.Issue following command.
SHOW DATABASES;
  • Now, its time to create new database using following command.
CREATE DATABASE testdb;

After executing the above command, you will get an output like below.

Query OK, 1 row affected (0.76 sec)
  • After that, you have to issue following command to retrieve the information about created database.
SHOW CREATE DATABASE testdb;

After issuing the above command, you will get the following output as shown below.


+----------+----------------------------------------------------------------------------------------------------------------------------------+
| Database | Create Database                                                                                                                  |
+----------+----------------------------------------------------------------------------------------------------------------------------------+
| testdb   | CREATE DATABASE `testdb` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  • Finally, To use newly created database, you have to select it by using USE statement.
USE testdb;

Output

Database changed

Create MySQL Database Using Workbench

Here we are going to create a MySQL database using the MySQL workbench application.

  • First launch the Workbench application and click Create MySQL Database icon to setup new connection.
Create MySQL Database
  • Provide the name of the connection and click on Test Connection.
Create MySQL Database
  • Workbench raise a prompt box to enter MySQL root password to authenticate your MySQL database server credentials.
Create MySQL Database
  • If you get successfull message that means you have successfully added your connection to MySQL workbench.
  • Now, double click on connection name Test to open MySQL control panel.
Create MySQL Database
  • Now, click on Create MySQL Database create new schema in connected server buttom in the toolbar.
Create MySQL Database
  • Here, you don’t need to confuse regarding schema name because schema means database in the MySQL.
  • Now, Enter the schema name, change the character set and collection, if you required other click on apply.
Create MySQL Database
  • MySQL workbench display following window that show you SQL script to create new schema.Remember that CREATE SCHEMA command works same as CREATE DATABASE command.
Create MySQL Database
  • Ater completing above process you will get a successfully message.Now your newly created database will look in left navigator section, as you can see in below screenshot.
Create MySQL Database
  • Now you are able to work with MyDB database, you can create table inside this database.

Conclusion

So, in this article, we have seen all about process to create MySQL database in MySQL server.
We have covered total two ways to create new MySQL database: first is MySQL Client tool and second is MySQL workbench.

Reference:- Click Here

Drop MySQL Database

Related Posts