EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial Delete Database MySQL

Delete Database MySQL

By Payal UdhaniPayal Udhani

Delete Database MySQL

Introduction to Delete Database MySQL

We can delete the database and all the contents of the database when they are no more helpful by using the DROP DATABASE command. This deletes the contents and all the schema-related information stored about the database entirely from your MySQL database server. You will find no trace of the deleted database. Hence, it is necessary to be extra careful while using this command, as restoration of the database is impossible if you do not have any backup, as the DROP DATABASE command permanently deletes the contents.

Syntax

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

DROP DATABASE [IF EXISTS] name_of_database;

Explanation: where name_of_database helps specify the name of the database that you wish to delete completely, including its contents like tables, stored procedures, triggers, etc. When you execute the DROP DATABASE command in MySQL, and the database server does not have a database named “name_of_database,” the command will return an error stating that there is no such database. However, you can use the IF EXISTS clause in the command to raise a warning instead of an error. This warning indicates that the database with the specified name does not exist.

Examples to Implement Delete Database MySQL

Below are the examples of the DROP DATABASE command:

Example #1

Firstly we will have to open the MySQL connection using the command:

Code:

mysql -u root -p

Output:

DROP DATABASE

Explanation: provided if you wish to login with root as the username and further, it will prompt for the password if any password is set.

Example #2

Then you will have to execute the following command to list all the databases in your MySQL database server.

Code:

SHOW DATABASES;

Output:

MySQL database server

Example #3

Now, let us try to delete the database that is not present on our database server and see the outputs:

Code:

DROP DATABASE educba;

Output:

delete the database

Explanation: It can be seen that an error with error code 1008 is thrown, saying that the ‘educba’ database cannot be dropped as no such database exists.

Example #4

Now, let us study the output after adding the IF EXISTS clause in our query statement:

Code:

DROP DATABASE IF EXISTS educba;

Output:

IF EXISTS clause

Explanation: It can be seen from the output that no error is raised even when the educba named database doesn’t exist on our database. It shows that the query was fine, no rows were affected, and that one warning is there.

Example #5

Now, let us create one database named educba using CREATE DATABASE command:

Code:

CREATE DATABASE educba;

Output:

CREATE DATABASE command

Example #6

Now let us insert two tables named to test and development:

Code:

CREATE TABLE test(
testCaseId INT NOT NULL AUTO_INCREMENT,
testCase VARCHAR(100),
trials INT,
cleared BOOLEAN,
PRIMARY KEY (testCaseId)
);

Output:

test and development

Example #7

One more create table query for development that is as follows:

Code:

CREATE TABLE development(
moduleId INT NOT NULL AUTO_INCREMENT,
scenario VARCHAR(100),
hoursRequired INT,
selfTested BOOLEAN,
PRIMARY KEY (moduleId)
);

Output:

delete database mysql7

Example #8

Now, let us drop the educba database using the drop database command:

Code:

DROP DATABASE IF EXISTS educba;

Output:

delete database mysql8

Explanation: The query was completed correctly, with no errors or warnings, and returned two impacted rows. The DROP DATABASE command returned the number of tables in that database and deleted when it was dropped.

Alternative to DROP DATABASE

In MySQL, the terms database and schema are used interchangeably, which means they refer to the same thing and are synonyms. Hence, we can make the use of

DROP SCHEMA [IF EXISTS] name_of_database;

This functions in the same manner as that of the DROP DATABASE command.

Step 1: Let us try to delete the database named mysqlDropDemo using the following query statement:

Code:

DROP SCHEMA mysqlDropDemo;

Output:

delete database mysql9

Explanation: It throws an error saying no database named mysqlDropDemo exists in my database server.

Step 2: Now, let us execute the same query with the IF EXISTS clause in it:

Code:

DROP SCHEMA IF EXISTS mysqlDropDemo;

Output:

delete database mysql10

Step 3: Let us create the database named mysqlDropDemo using the following query:

Code:

CREATE DATABASE  mysqlDropDemo;

Output:

delete database mysql11

Step 4: Now, if you execute the same query of DROP SCHEMA, then the output will be different. We will be executing the following query statement:

Code:

DROP SCHEMA IF EXISTS mysqlDropDemo;

Output:

delete database mysql12

Explanation: Because the “mysqlDropDemo” database contained no tables, the database was successfully dropped, and no records were returned or affected.

Using the SHOW DATABASES, you can also confirm the presence and dropping of databases at each stage while performing this task. When you run this command, the program returns a list of all databases on your database server.

Conclusion

We can delete the database using the SQL command DROP DATABASE, which completely and permanently deletes the database and all the contents of that database. Hence, we should be careful while using this command, as it is impossible to restore the dropped database if you don’t have any backup. You can use an alternative command of DROP SCHEMA to delete the existing database on your database server.

Recommended Articles

We hope that this EDUCBA information on “Delete Database MySQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. MySQL Index
  2. Date Functions in MySQL
  3. MySQL Administration
  4. MYSQL Database
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle1500+ Hour of HD Videos | 80 Learning Paths | 360+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more