Jump to content

Create a New Database in MySQL


Linux Hint

Recommended Posts

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

create-a-new-database-in-mysql-01.png

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

create-a-new-database-in-mysql-02.png

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

create-a-new-database-in-mysql-03.png

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;

create-a-new-database-in-mysql-04.png

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;

create-a-new-database-in-mysql-05.png

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.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...