// tables reference

SQL DROP SCHEMA

Removes a named container. PostgreSQL guards its contents behind CASCADE, SQL Server refuses until you empty it, and MySQL removes the database and everything in it.

类别
表
In this category
14 of 16

语法

DROP SCHEMA [IF EXISTS] name [CASCADE | RESTRICT]  -- PostgreSQL: CASCADE takes the contents with it
DROP SCHEMA [IF EXISTS] name  -- SQL Server: only drops an empty schema; IF EXISTS from 2016
DROP {DATABASE | SCHEMA} [IF EXISTS] name  -- MySQL: drops the database and every table in it
DROP USER name CASCADE  -- Oracle: the schema goes with the user
DETACH DATABASE name  -- SQLite: forgets an attached file, the file stays

Each block is a separate form. The support table and the examples show which engine takes which.

引擎支持

引擎Support备注
SQL Server 是 fails while the schema contains any object; drop them or move them out first
PostgreSQL 是 RESTRICT (the default) refuses a non-empty schema; CASCADE drops everything inside, following dependencies
MySQL 是 DROP DATABASE by another name: every table and the data with them, no confirmation
SQLite Different syntax nothing to drop; DETACH DATABASE removes the alias from the connection, the file survives
Oracle Different syntax DROP USER name; CASCADE is required once the user owns any object

示例

PostgreSQL

DROP SCHEMA IF EXISTS staging CASCADE;

SQL Server

-- empty first, then drop
DROP TABLE billing.invoices;
DROP SCHEMA billing;

MySQL

-- drops the database and all its tables
DROP DATABASE IF EXISTS staging;

Oracle

DROP USER staging CASCADE;

SQLite

-- the alias goes, billing.db stays on disk
DETACH DATABASE billing;

值得了解

  • MySQL's DROP SCHEMA has no empty-check: the statement deletes every table and its data in one shot. IF EXISTS only spares you a missing-name error; it never asks about contents.
  • PostgreSQL's CASCADE reaches further than the schema's own listing: it also drops objects in other schemas that depend on the doomed ones, such as views and foreign key constraints. One NOTICE reports the count and its DETAIL lists each object, but only the first 100; past that the rest are only in the server log. Read the DETAIL, or run plain DROP SCHEMA first, which fails and prints the same list without dropping anything.
  • SQL Server is the opposite temperament: it refuses while any object remains. The escape hatch is ALTER SCHEMA dbo TRANSFER billing.invoices, which moves an object between schemas without dropping it.
  • Oracle's DROP USER CASCADE is one statement with no checkpoint: every table, index, view and grant in the schema is gone when it returns, so export anything worth keeping first.

Questions about DROP SCHEMA

How do I empty a SQL Server schema before dropping it?

List what is in it with SELECT name, type_desc FROM sys.objects WHERE schema_id = SCHEMA_ID(N'billing'), then check sys.types and sys.xml_schema_collections with the same schema_id filter, because user types and XML schema collections also block the drop and sys.objects does not show them. Drop each object, or move it with ALTER SCHEMA dbo TRANSFER billing.invoices (TRANSFER TYPE::billing.name for a type). Only then does DROP SCHEMA run.

Does DROP SCHEMA delete the data files?

Only in MySQL, where the schema is the database and the drop removes its storage. PostgreSQL and SQL Server drop objects inside one database and keep the database itself. Oracle drops the user's objects with CASCADE. SQLite's DETACH deletes nothing: the file stays on disk.

What is the difference between RESTRICT and CASCADE?

PostgreSQL's choice: RESTRICT (the default) fails while anything still lives in the schema or depends on it; CASCADE removes the contents and dependent objects too. SQL Server always behaves like RESTRICT, MySQL always like CASCADE.

Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.

// dialect-aware editing

DDL that matches the engine you're connected to

SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.