Updated on 2024-12-13 GMT+08:00

DROP SCHEMA

Syntax

DROP (DATABASE|SCHEMA) [IF EXISTS] databasename [RESTRICT|CASCADE]

Description

DATABASE and SCHEMA are conceptually equivalent and interchangeable.

This syntax is used to delete the database name. If the target database does not exist, an error message is displayed. However, if the IF EXISTS clause is used, no error message is displayed.

The optional parameter RESTRICT|CASCADE is used to specify the deletion mode. The default mode is RESTRICT. In this mode, the database can be deleted only when it is empty without any table. In CASCADE mode, tables in the database are deleted first, and then the database.

Example

  • To drop the schema web.
    DROP SCHEMA web;
  • If the schema sales exists, drop the schema:
    DROP SCHEMA IF EXISTS sales;
  • Delete schema test_drop in cascading mode. The tb_web table that exists in schema test_drop is deleted before schema test_drop is deleted.
    CREATE SCHEMA test_drop;
     
    USE test_drop;
     
    CREATE TABLE tb_web(col1 int);
     
    DROP DATABASE test_drop CASCADE;