Skip to main content

Deleting a Database in MySQL

In this tutorial, we'll walk through the process of deleting a database in MySQL. Before we get started, it's important to note that deleting a database is an irreversible operation. Therefore, make sure you've backed up any data you need from the database before you delete it.

Prerequisites

To follow this tutorial, you'll need:

  • MySQL installed on your local machine or server
  • Access to the MySQL command line interface or a MySQL GUI like phpMyAdmin
  • Permission to delete a database

Step 1: Log into MySQL

First, you'll need to log into the MySQL server. In your terminal or command prompt, enter the following command:

mysql -u root -p

In this command, -u root specifies that you want to log in as the root user, and -p tells MySQL to prompt you for the root user's password.

Step 2: Display all databases

Before you can delete a database, you need to know the exact name of the database. To display a list of all databases in MySQL, use the SHOW DATABASES; command:

SHOW DATABASES;

This will display a list of all the databases on the server.

Step 3: Delete the database

Now, you can delete the database. The command to do this is as follows:

DROP DATABASE database_name;

In this command, replace database_name with the name of the database you want to delete. For example, if you want to delete a database named my_database, you would type:

DROP DATABASE my_database;

This command will delete the database and all its associated data immediately. Remember, this operation is irreversible.

Step 4: Confirm the deletion

To confirm that the database has been deleted, you can use the SHOW DATABASES; command again. The name of the database you deleted should no longer appear in the list.

Conclusion

That's all there is to deleting a database in MySQL. Remember, this operation is permanent, so always make sure you're deleting the correct database and that you've backed up any necessary data first.