MySQL Basics: Create a Database in MySQL

Creating Database In MySQL

In this tutorial, you’ll learn to create a database in MySQL in two different ways, command line and MySQL Workbench.

But before we start to learn how to create a database, Let’s first understand what a database is!

In simple words, a database is a collection of tables, which includes rows and columns, where you can store structured data in a computer system. Let’s imagine we have to create a database for a bookstore. You’ll have a table for books, a table for buyers, a table for sellers and publishers, and so forth. The collection of all these tables makes a database.

MySQL is an open-source relational database management system, owned and distributed by Oracle Corporation. Using MySQL, you can create a database as well as manage it. 

If you don’t have MySQL installed on your system then you can download MySQL from https://dev.mysql.com/downloads/, and you’ll be good to go. 

The syntax for creating a database using MySQL is,

CREATE DATABASE databasename;Code language: SQL (Structured Query Language) (sql)

The above command is all you need to write to create a database, one thing to keep in mind is to not use the name of a database that already exists. If you use a database name that already exists then MySQL will throw an error. 

Creating a Database using SQL Command Line

There are two ways to open the SQL command line.

First, you can go to start and type command-line client, this will help you to directly open the SQL command-line client. 

Opening Command Line Client

The second way is by using windows command prompt, go to Start and type cmd,  when you open cmd then type the command, 

mysql -u username -pCode language: Bash (bash)

you can use root as username as the default setting of your MySQL connection’s username is the root, just as shown in the image below.

Opening SQL Command Line Using Cmd

In both cases(using cmd or directly opening MySQL command-line client) you’ll have to enter your MySQL password. If in case you forgot the password you can refer to the resources present at the end of the tutorial. 

Now you’re connected to the MySQL server, you can type the command,

CREATE DATABASE databasename;Code language: SQL (Structured Query Language) (sql)

You can name the database as you prefer, but mindful naming of the database is a good practice.

Creating First Database
create database db; Code language: SQL (Structured Query Language) (sql)

Here, db is used as the name for the database. This creates your first database, if you would like to check if you’re database is successfully created then you can write the command, 

show databases; Code language: SQL (Structured Query Language) (sql)

This shows all the databases present in MySQL and if you’ve successfully created the database then the name of your database will be visible in the list.

Creating Database using MYSQL Workbench

MySQL Workbench is a popular GUI database management tool for administrating, designing and developing databases. 

Below are the steps to be used to create a database using MySQL Workbench, 

  1. Search for MySQL workbench on the start menu and launch MySQL workbench.
MySQL WorkBench

2. When the Workbench window is open, click on the +(add) button to create a new connection.

Click The Button

3. After clicking on the add button, you’ll be prompted Setup a new connection window, just enter the connection name, and click on test connection.

MySQL WorkBench Setup

4.  A password prompt will pop up, enter your MySQL password, check the save password in the vault checkbox, and click on OK.

MySQL WorkBench Password Verification Prompt

5. Your connection is now created. Now to create a database, click on the Create a new schema in the connected server button. You’ll be prompted by a new tab to create a new schema, just set the name of the database and click on apply.

Creating New Schema In MySQL

6. After configuring the database name, a window as Apply SQL Script to Database will appear, click on apply and then click on finish.

Configuring Name Of Database In MySQL WorkBench

7. You can check if you’ve successfully created the database, by checking in the schema’s section under the navigation bar.

Check Under The Schema Section

This is how you create a database using MySQL workbench.

Conclusion

With every application around you now requiring a database, it’s important for us to know how to create one using MySQL and then use it within our apps. I hope this tutorial has helped you create databases. Next, you can learn how to create tables in MySQL databases.

Other Resources:
https://dev.mysql.com/downloads/ – for MySQL community downloads
https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html – for resetting the root password
https://dev.mysql.com/doc/ – Official MySQL documentation