TUTORIAL INDEX

MySQL DROP DATABASE

|

What is the DROP DATABASE Statement in MySQL and Why Do We Use It?

If you want to delete a database that you created, you can use the DROP DATABASE statement. When you use this statement, it removes the entire database, including all of its tables, data, and associated objects.

The DROP DATABASE statement is used to permanently remove an existing database from the MySQL server.


Syntax:

DROP DATABASE database_name;

Example:

DROP DATABASE PersonDB;

Output:

If the specified database exists, MySQL removes the database and displays the following message:

Query OK, 0 rows affected

If the specified database does not exist, MySQL displays the following error message:

ERROR 1008 (HY000): Can't drop database 'database_name'; database doesn't exist

How the DROP DATABASE Statement Works Internally


When you want to delete a database in MySQL, you type the DROP DATABASE statement. After pressing the Enter key, the DROP DATABASE statement is sent to the MySQL server. The server checks whether the database exists. If it exists, MySQL deletes that database along with all tables, data, and associated objects stored inside it. If it does not exist, MySQL displays an error message.

Remember that the DROP DATABASE statement does not create, copy, move, or modify any data. It permanently deletes the database and all objects stored inside it. Once a database is deleted, it cannot be recovered unless a backup is available.

Flowchart:

👤 User
   |
   ▼
⌨️ DROP DATABASE database_name;
   |
   ▼
🖥️ MySQL Server
   |
   ▼
🔍 Check Database Exists
   |
 ┌───────┴───────┐
 ▼               ▼
✅ Yes       ❌ No
 |               |
 ▼               ▼
🗑️ Delete   ⚠️ Error
 Database        Message
 |
 ▼
✅ Database Removed

Precautions While Using DROP DATABASE Statement:

  1. Before using the DROP DATABASE statement, you should know the correct database name and its spelling. Otherwise, MySQL will display an error message.
  2. Before using the DROP DATABASE statement, make sure that you want to permanently delete the database.
  3. You should take a backup of important data before deleting a database.
  4. Before executing queries, always check the selected database; otherwise, you may delete the wrong database.

How to Use the DROP DATABASE Statement in MySQL: Step-by-Step Guide

If you use the DROP DATABASE statement in MySQL, you first need to check the available databases on the MySQL server. After confirming the required database, you use the DROP DATABASE statement to delete that database. The complete process is as follows:

Step 1: You connect to the MySQL server by using the MySQL login command.

mysql -u username -p

Step 2: You type the SHOW DATABASES statement to view the list of available databases.

SHOW DATABASES;

Step 3: You delete the required database by typing the DROP DATABASE statement.

DROP DATABASE database_name;

Step 4: After executing the statement, MySQL permanently deletes the selected database along with all tables, data, and associated objects stored inside it.

Note:

  • The DROP DATABASE statement permanently deletes a complete database.
  • The DROP DATABASE statement removes all tables, data, and associated objects stored inside the database.
  • Be careful while using the DROP DATABASE statement because this action cannot be undone.

Don't Confuse DROP DATABASE with the DELETE Statement

Many beginners get confused between the DROP DATABASE statement and the DELETE statement because both are used to remove data. However, they perform different tasks. The DROP DATABASE statement permanently deletes the complete database along with all tables and data stored inside it, whereas the DELETE statement removes specific records from a table.

Therefore, when you want to permanently remove an entire database, use the DROP DATABASE statement. If you want to remove only specific records from a table, use the DELETE statement.

ONLINE WEB TOOLS

WhatIsMyIpAddress
Shorterner