資料庫

PostgreSQL 資料庫建立、修改、刪除資料表教學與範例

介紹如何在 PostgreSQL 資料庫中,以指令的方式建立、修改或刪除資料表。

建立資料表

在建立資料表之前,可以先檢查該資料表是否已經存在,若存在則先將其刪除:

-- 若 persons 資料表存在,則將其刪除
DROP TABLE IF EXISTS persons;

接著使用 CREATE TABLE 指令建立新的資料表:

-- 建立 persons 資料表
CREATE TABLE persons (
  id SERIAL PRIMARY KEY,          -- 序號,主鍵
  name VARCHAR(255),              -- 名字,可變長度的文字
  age SMALLINT,                   -- 年齡,整數
  created_on TIMESTAMP NOT NULL   -- 建立時間,非空
);

建立好資料表之後,檢查一下資料表的結構:

-- 查看資料表結構
\d persons
                                        Table "public.persons"
   Column   |            Type             | Collation | Nullable |               Default
------------+-----------------------------+-----------+----------+-------------------------------------
 id         | integer                     |           | not null | nextval('persons_id_seq'::regclass)
 name       | character varying(255)      |           |          |
 age        | smallint                    |           |          |
 created_on | timestamp without time zone |           | not null |
Indexes:
    "persons_pkey" PRIMARY KEY, btree (id)

新增欄位

若要在既有的資料表中,新增一個欄位,可以使用 ALTER TABLE 指令配合 ADD 動作處理:

-- 在 persons 資料表中新增 email 欄位
ALTER TABLE persons ADD email VARCHAR(50);

檢查一下資料表的結構:

-- 查看資料表結構
\d persons
                                        Table "public.persons"
   Column   |            Type             | Collation | Nullable |               Default
------------+-----------------------------+-----------+----------+-------------------------------------
 id         | integer                     |           | not null | nextval('persons_id_seq'::regclass)
 name       | character varying(255)      |           |          |
 age        | smallint                    |           |          |
 created_on | timestamp without time zone |           | not null |
 email      | character varying(50)       |           |          |
Indexes:
    "persons_pkey" PRIMARY KEY, btree (id)

刪除欄位

若要將資料表中指定的欄位刪除,可以使用 ALTER TABLE 指令配合 DROP 動作來處理:

-- 刪除 persons 資料表中的 email 欄位
ALTER TABLE persons DROP email;

這樣就可以將 email 這個欄位從 persons 資料表中刪除了。

修改欄位資料型態

如果想要修改既有資料表中某個欄位的屬性,可以使用 ALTER TABLE 指令配合 ALTER 處理。

例如將 age 欄位的資料型態改為 VARCHAR(3)

-- 將 age 欄位的資料型態改為 VARCHAR(3)
ALTER TABLE persons ALTER age TYPE VARCHAR(3);

檢查一下資料表的結構:

-- 查看資料表結構
\d persons
                                        Table "public.persons"
   Column   |            Type             | Collation | Nullable |               Default
------------+-----------------------------+-----------+----------+-------------------------------------
 id         | integer                     |           | not null | nextval('persons_id_seq'::regclass)
 name       | character varying(255)      |           |          |
 age        | character varying(3)        |           |          |
 created_on | timestamp without time zone |           | not null |
Indexes:
    "persons_pkey" PRIMARY KEY, btree (id)

修改欄位預設值

欄位的預設值也是使用 ALTER TABLE 指令配合 ALTER 處理。

-- 將 name 欄位預設值訂為 anonymous
ALTER TABLE persons ALTER name SET DEFAULT 'anonymous';

檢查一下資料表的結構:

-- 查看資料表結構
\d persons
                                        Table "public.persons"
   Column   |            Type             | Collation | Nullable |               Default
------------+-----------------------------+-----------+----------+-------------------------------------
 id         | integer                     |           | not null | nextval('persons_id_seq'::regclass)
 name       | character varying(255)      |           |          | 'anonymous'::character varying
 age        | character varying(3)        |           |          |
 created_on | timestamp without time zone |           | not null |
Indexes:
    "persons_pkey" PRIMARY KEY, btree (id)

若要移除 name 欄位的預設值設定,可以使用以下指令:

-- 移除 name 欄位預設值
ALTER TABLE persons ALTER name DROP DEFAULT;

刪除資料表

若要刪除資料表,可以使用 DROP TABLE 指令:

-- 刪除資料表
DROP TABLE persons;

若要刪除的資料表本來就不存在,這行指令就會生錯誤,這時候可以加上 IF EXISTS 自動檢查資料表是否存在,若存在則刪除,若不存在也不會產生錯誤:

-- 若資料表存在,則刪除
DROP TABLE IF EXISTS persons;

參考資料

Share
Published by
Office Guide
Tags: PostgreSQL

Recent Posts

Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤

本篇介紹如何在 Python ...

9 個月 ago

Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例

本篇介紹如何在 Ubuntu ...

9 個月 ago

Linux 網路設定 ip 指令用法教學與範例

本篇介紹如何在 Linux 系...

9 個月 ago

Windows 使用 TPM 虛擬智慧卡保護 SSH 金鑰教學與範例

本篇介紹如何在 Windows...

10 個月 ago

Linux 以 Shamir’s Secret Sharing 分割保存金鑰教學與範例

介紹如何在 Linux 中使用...

11 個月 ago

Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例

介紹如何在 Linux 系統中...

11 個月 ago