PostgreSQL 删除表


PostgreSQL DROP TABLE 语句用于删除表定义以及该表的所有关联数据、索引、规则、触发器和约束。

使用此命令时必须小心,因为一旦表被删除,表中的所有可用信息也将永远丢失。

语法


DROP TABLE 语句的基本语法如下:

DROP TABLE table_name;

例子


我们在上一章中创建了表 DEPARTMENT 和 COMPANY。首先,验证这些表(使用 \d 列出表格):

testdb-# \d

这将产生以下结果:

           List of relations
 Schema |    Name    | Type  |  Owner
--------+------------+-------+----------
 public | company    | table | postgres
 public | department | table | postgres
(2 rows)

这意味着存在 DEPARTMENT 和 COMPANY 表。因此,让我们将它们删除如下:

testdb=# drop table department, company;

这将产生以下结果:

DROP TABLE
testdb=# \d
relations found.
testdb=# 

返回 DROP TABLE 消息表示 drop 命令执行成功。