Updated on 2024-11-08 GMT+08:00

Deleting a Database

Function

This statement is used to delete a database.

Syntax

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

Keywords

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.

Parameters

Table 1 Parameter

Parameter

Description

db_name

Database name, which consists of letters, digits, and underscores (_). The value cannot contain only digits or start with a digit or underscore (_).

Example

  1. Create a database, for example, testdb, by referring to Example.
  2. Run the following statement to delete database testdb if it exists:
    1
    DROP DATABASE IF EXISTS testdb;