The SQL DROP database statement is used to delete the existing database from the database management system. This statement permanently deletes the specified database from the system which includes all the tables, schemas, views and all the data that is stored inside the specified database.
SQL DROP database syntax
DROP DATABASE databaseName;
There are two things that you need to remember while deleting a database:
1. Dropping a database is not frequently used as it can delete the important data which is not used now but maybe required later. This is why it is important to take a backup of the database before you delete it using DROP DATABASE statement. The backup ensures that if in case, you need the data, you can easily restore the database and get the required data.
2. If the database is already in use while you are deleting it then you will get the following error message: Let’s say you are dropping the database ‘Employee’ while other database user is currently working on the database ‘Employee’
"Cannot drop database "Employee" because it is currently in use".
Drop Multiple Databases using DROP DATABASE statement
DROP DATABASE db1, db2, db3;
This statement will drop three databases db1, db2 and db3 from the database system.
SQL DROP DATABASE statement Example
The following statement would delete the database named “Student”.
SQL> DROP DATABASE Student;
Note: By deleting a database, you are deleting all of its tables implicitly. For example, the above statement would delete all the tables that are stored inside “Student” database, along with the database.
After dropping a database you can check the database list to cross verify that the database has been successfully dropped or not. This is how you can do it.
Before deleting “Student” Database:
SQL> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | Abc | | Xyz | | Student | | Demo | | Test | +--------------------+ 5 rows in set (0.00 sec)
After deleting “Student” Database:
SQL> DROP DATABASE Student;
List down the databases:
SQL> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | Abc | | Xyz | | Demo | | Test | +--------------------+ 4 rows in set (0.00 sec)
Leave a Reply