PostgreSQL 刪除表格

PostgreSQL 使用 DROP TABLE 語句來刪除表格,包含表格數據、規則、觸發器等,所以刪除表格要慎重,刪除後所有資訊就消失了。

語法

DROP TABLE 語法格式如下:

DROP TABLE table_name;

實例

上一章節中我們創建了 COMPANY 和 DEPARTMENT 兩個表格,我們可以先使用 \d 命令來查看表格是否創建成功:

zaixiandb=# \d
           List of relations
 Schema |    Name    | Type  |  Owner
--------+------------+-------+----------
 public | company    | table | postgres
 public | department | table | postgres
(2 rows)

從以上結果可以看出,我們表格已經創建成功,接下來我們刪除這兩個表格:

zaixiandb=# drop table department, company;
DROP TABLE

再使用 \d 命令來查看就找不到表格了:

testdb=# \d
Did not find any relations.