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
| Parameter | Description |
|---|---|
name | The name of the table to drop. The name can be schema-qualified. |
RESTRICT | Refuses to drop the table if any objects depend on it. This is the default behavior. |
CASCADE | Drops the table and removes any dependent constraints on the specified table. Other object types are not included. Dependent tables themselves are not dropped. |
CASCADE CONSTRAINTS | Drops 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 becauseitemsdepends onorders.DROP TABLE orders CASCADE— PolarDB dropsordersand removes the foreign key constraint fromitems. Theitemstable itself is not dropped.DROP TABLE orders CASCADE CONSTRAINTS— PolarDB dropsordersand removes the foreign key constraint fromitems. Theitemstable itself is not dropped.
What's next
Create a table
Alter a table