This topic describes how to delete a table.
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. |
Description
DROP TABLE removes tables including the indexes, rules, triggers, and constraints of the tables from the database. Only the owner of a table can destroy the table.
Include the RESTRICT keyword to specify that the server must refuse to drop the table if any objects depend on it. This is the default behavior. The DROP TABLE statement displays an error if any objects depend on the table.
Include the CASCADE or CASCADE CONSTRAINTS clause to specify that PolarDB must drop any dependent constraints on the specified table. Other object types are not included.
Examples
The following statement drops a table named emp that has no dependencies:
DROP TABLE emp;The outcome of a DROP TABLE statement varies depending on whether the table has any dependencies. You can control the outcome by specifying a drop behavior. For example, if you create two tables named orders and items, where the items table is dependent on the orders table:
CREATE TABLE orders
(order_id int PRIMARY KEY, order_date date, …);
CREATE TABLE items
(order_id int REFERENCES orders, quantity int, …); PolarDB performs one of the following actions when dropping the orders table, depending on the drop behavior that you specify:
If you specify
DROP TABLE orders RESTRICT, PolarDB reports an error.If you specify
DROP TABLE orders CASCADEorDROP TABLE orders CASCADE CONSTRAINTS, PolarDB drops the orders table and removes the foreign key specification from the items table, but does not drop the items table.