Delete/Drop a Database in MySQL


MySQL is an RDBMS (Relational Database Management System) that is famous for its speed and easy-to-use interface. In this article, you will learn about the different methods to delete or drop a database in MySQL. In this article, we assume that you already have a working knowledge of the creation and listing of databases in MySQL. So, feel free to read on if you have already installed MySQL on your system and have some dummy databases in MySQL that you want to delete. To get started with MySQL, open the terminal. First, check the version of MySQL via the following command:
mysql -V


If you have the latest version of MySQL, you are good to go.

Next, check the status of the system’s mysql.service by running the following command

sudo systemctl status mysql


If the service is not active, then start the service.

sudo systemctl start mysql

After starting the service, connect to the MySQL client or log in to the MySQL shell as a root user. If you do not have access to the root user login, replace ‘root’ with your username. In this article, we will use the terminal to demonstrate the process instead of the GUI, known as the MySQL WorkBench.

sudo mysql -u root -p


After logging in to MySQL, list the databases using the ‘SHOW DATABASES’ command.

SHOWDATABASES;


Once you have the list of databases, select the database that you want to delete. If you want to delete an existing database, you can run the simple ‘DROP DATABASE’ command, along with the database name, as follows:

DROPDATABASE database_name;


Keep in mind, you can only delete or drop a database if you have the privileges to delete that database. So, make sure to log in with the specific user who has the privileges to delete that database.

After deleting the database, we will list the databases again using the ‘SHOW DATABASES’ command.

SHOWDATABASES;


As you can see, the deleted database no longer exists in MySQL.

In another case, just like the creation of a database, you can use the ‘IF EXISTS’ clause to avoid the error if there is no database with the provided name. If you do not use the ‘If EXISTS’ clause and the database does not exist, MySQL will output an error. The syntax for using the ‘IF EXISTS’ clause is as follows

DROPDATABASEIFEXISTS database_name;

Conclusion

This article contains two different methods for deleting an existing database in MySQL, both with and without the ‘IF EXISTS’ clause.



from Linux Hint https://ift.tt/3fyImQw

Post a Comment

0 Comments