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.
Step 2: Enter the password and you will get redirected to MySQL Workbench.
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.
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.
Step 5: Another screen will appear where it will ask to apply the script on the database, again click Apply button.
Step 6: A Screen will appear with the statement Execute SQL Statement. On clicking the Finish button it will create a 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.
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.
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.
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']
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;
Step 2: In this step, we will review the created database using the below code statement.
mysql> SHOW CREATE DATABASE studentsdb;
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;
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.
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:
- How to Install MySQL on Windows
- Different Datatypes in MySQL
- How to create a new User in MySQL
- How to Insert Single Record into a Table in MySQL
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.