Create a New Database in MySQL

The first part after installing a DBMS, like MySQL, is to create a database and start building it. In this article, we will create a new database. We will learn about the Create Command in MySQL and also see how we can grant privileges to other users of the newly created database? So, let’s get started.

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:

mysql -V

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:

sudo systemctl status mysql

If it is running for you, then that’s good. Otherwise, you can start the mysql.service using the following command:

sudo systemctl start mysql

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:

sudo mysql -u root -p

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:

CREATE DATABASE database_name;

However, if the name of the database already existed. You can use “IF NOT EXISTS” with the CREATE DATABASE command. Like this:

CREATE DATABASE IF NOT EXISTS new_database_name;

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.



from Linux Hint https://ift.tt/2VFac4i

Post a Comment

0 Comments