DROP DATABASE in SQL
The SQL DROP DATABASE statement is a powerful command in SQL that is used to permanently delete an existing database from a system, including all its data, tables, views, indexes, stored procedures , constraints and any other elements that belong to it. Once executed, the database and all its contents are permanently deleted and cannot be recovered unless a backup is available. This command should be used with caution, because it permanently deletes the database and all its contents.
Syntax
DROP DATABASE DatabaseName;
Example
If you are planning to DROP a database (named Users). Your database DROP command would be
DROP DATABASE Users;
Note :- This operation is irreversible so it's essential to ensure that a full backup of your database is available before running this query.
If Database Not exist
If the database does not exist, you will get an error. To avoid this, you can use the IF EXISTS
DROP DATABASE IF EXISTS DatabaseName;
Example :-
If you are planning to drop a database (named Users) if exist. Your database command would be
DROP DATABASE IF EXISTS Users;
Verifying Database
If you want to check is database present or not. You would run below command
SHOW DATABASE;
Key Characteristics
- Before using the DROP DATABASE command, ensure that the database is not in use. If there are any active connections to the database, the DROP DATABASE command will fail.
- The user executing the DROP DATABASE command must have the necessary privileges to perform the operation. Typically, executing this command requires administrative access or a specific DROP privilege for the database.
- This operation is irreversible so it's essential to ensure that a full backup of the database is available before running this query, especially for production databases.
- DROP DATABASE command delete the entire database along with all of its contents. On the other hand, if you want to delete only a specific table, you should use the DROP TABLE command.
- DROP DATABASE should be used only when you want to delete everything associated with the database.