Deleting a Database

Function

This statement is used to delete a database.

Syntax

1
DROP [DATABASE | SCHEMA] [IF EXISTS] db_name [RESTRICT|CASCADE];

Keyword

IF EXISTS: Prevents system errors if the database to be deleted does not exist.

Precautions

  • DATABASE and SCHEMA can be used interchangeably. You are advised to use DATABASE.
  • RESTRICT: If the database is not empty (tables exist), an error is reported and the DROP operation fails. RESTRICT is the default logic.
  • CASCADE: Even if the database is not empty (tables exist), the DROP will delete all the tables in the database. Therefore, exercise caution when using this function.

Example

If database testdb exists, run the following statement to delete database testdb:

1
DROP DATABASE IF EXISTS testdb;