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.
Headings of Contents
CREATE MySQL DATABASE
There are two ways to create database in MySQL.
- Using MySQL client tool
- 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 icon to setup new connection.
- Provide the name of the connection and click on Test Connection.
- Workbench raise a prompt box to enter MySQL root password to authenticate your MySQL database server credentials.
- 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.
- Now, click on create new schema in connected server buttom in the toolbar.
- 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.
- MySQL workbench display following window that show you SQL script to create new schema.Remember that
CREATE SCHEMA
command works same asCREATE DATABASE
command.
- 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.
- 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