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 指示刪除命令已成功執行。

廣告
© . All rights reserved.