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
| 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;
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot