Updated on 2026-11-06 GMT+08:00

Drop Database

Function

Drops a database and its associated file system directory. If the database does not exist, the system throws an exception.

Syntax

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

Keywords

IF EXISTS: Used when the database to be dropped does not exist, to prevent the system from reporting an error.

Precautions

  • DATABASE and SCHEMA have no difference and can be used interchangeably. DATABASE is recommended.
  • RESTRICT: If the database is not empty (has tables), the DROP operation reports an error and fails. RESTRICT is the default logic.
  • CASCADE: Even if the database is not empty (has tables), DROP will cascade and delete all tables under it. Use this feature with caution.
  • After dropping a database, its storage path on OBS is not automatically deleted and needs to be cleaned up manually.

Parameters

Table 1 Parameter description

Parameter

Description

db_name

Database name. Only letters, digits, and underscores (_) are allowed. It cannot be purely numeric and cannot start with a digit or underscore.

Example 1: Drop an Empty Database

Drop database inventory_db. If the database is not empty, the operation fails.

DROP DATABASE inventory_db;

Example 2: Drop a Non-Empty Database (Cascade Drop)

Drop database inventory_db and all tables within it.

DROP DATABASE inventory_db CASCADE;

Example 3: Safely Drop Using IF EXISTS

Drop database inventory_db. If the database does not exist, no error is raised.

DROP DATABASE IF EXISTS inventory_db CASCADE;

Example 4: Drop a Database Using the SCHEMA Keyword

SCHEMA is equivalent to DATABASE and can be used interchangeably.

DROP SCHEMA IF EXISTS inventory_db;