First of all, open up your terminal using CTRL + ALT + T and check the version of MySQL installed on your computer using the following command:
The illustration showed that you have already installed MySQL in your Operating system.
If it is not installed and you want to install it, kindly visit our dedicated article related to the installation of MySQL on Ubuntu 20.04. If it is installed, then you are good to go and can just follow this article.
First, verify the status of the system’s mysql.service. Whether it is running or not, run the following command:
If it is running for you, then that’s good. Otherwise, you can start the mysql.service using the following command:
After starting it successfully, you can connect to the MySQL client using the terminal. There is a GUI also available for MySQL known as MySQL workbench, but we will use the terminal to demonstrate the process. So, in order to connect or login to the MySQL, you can connect to the MySQL shell as a root user using the following command:
After connecting to the MySQL database, it is kind of obvious that you want to create and manage a database.
Now, there can be two possibilities if you are going to create a database in MySQL. One is whether the database’s name already existed in MySQL or not. So, if the name of the database does not exist in MySQL, then run the following command in MySQL shell to create a new database:
However, if the name of the database already existed. You can use “IF NOT EXISTS” with the CREATE DATABASE command. Like this:
By using the “IF NOT EXISTS” clause, MySQL will not create the table if the name already existed and will not throw any error as well. On the other hand, if we avoid using the “IF NOT EXISTS” clause, MySQL will throw the error.
Conclusion
This article contains two different methods to create a new database in MySQL; when using the “IF NOT EXISTS” clause and when not using it. We have also seen the error if we do not use this clause.