Delete a table

更新时间:
复制 MD 格式

DROP TABLE removes a table and all its associated indexes, rules, triggers, and constraints from the database. Only the table owner can drop a table.

Prerequisites

Before you begin, ensure that you have:

  • Ownership of the table to drop

Syntax

DROP TABLE name [CASCADE | RESTRICT | CASCADE CONSTRAINTS]

Parameters

ParameterDescription
nameThe name of the table to drop. The name can be schema-qualified.
RESTRICTRefuses to drop the table if any objects depend on it. This is the default behavior.
CASCADEDrops the table and removes any dependent constraints on the specified table. Other object types are not included. Dependent tables themselves are not dropped.
CASCADE CONSTRAINTSDrops the table and removes any dependent constraints on the specified table. Other object types are not included.

Examples

Drop a table with no dependencies

DROP TABLE emp;

Drop a table with dependencies

If a table has dependent objects, the outcome depends on the option you specify. The following example sets up two tables where items has a foreign key referencing orders:

CREATE TABLE orders
  (order_id int PRIMARY KEY, order_date date, …);
CREATE TABLE items
  (order_id int REFERENCES orders, quantity int, …);

PolarDB behaves as follows when you drop the orders table:

  • DROP TABLE orders RESTRICT — PolarDB reports an error because items depends on orders.

  • DROP TABLE orders CASCADE — PolarDB drops orders and removes the foreign key constraint from items. The items table itself is not dropped.

  • DROP TABLE orders CASCADE CONSTRAINTS — PolarDB drops orders and removes the foreign key constraint from items. The items table itself is not dropped.

What's next

  • Create a table

  • Alter a table