• 跳至主要導覽
  • 跳至主要內容
  • 跳至主要資訊欄
Office 指南

Office 指南

辦公室工作實用教學

  • Excel
  • Word
  • PowerPoint
  • Windows
  • PowerShell
  • R

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;

參考資料

  • PostgreSQL Tutorial:PostgreSQL CREATE TABLE
  • PostgreSQL Tutorial:PostgreSQL ALTER TABLE
  • PostgreSQL Tutorial:PostgreSQL DROP TABLE

分類:資料庫 標籤:PostgreSQL

讀者互動方式

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

主要資訊欄

搜尋

近期文章

  • Linux 檢查、測試 CPU AES-NI 硬體加速加密指令集教學
  • CentOS Linux 7.9 自行編譯、安裝 OpenSSH 9.0p1 伺服器教學與範例
  • Python 使用 zipfile 模組壓縮、解壓縮 ZIP 檔案教學與範例
  • Python 以 LINE Notify 自動傳送即時訊息、圖片教學與範例
  • Linux 使用 Prometheus 與 Grafana 監控伺服器狀態、發送告警 Email 簡訊教學與範例
  • Linux 設定 pam_tty_audit 記錄 SSH 使用者操作指令教學與範例
  • Linux 封鎖、解鎖登入失敗次數過多的帳號 pam_faillock 教學與範例
  • Python 使用 pytube 自動下載 YouTube 影片教學與範例

推薦網站

  • Udemy 線上教學課程
  • Coursera 線上教學課程

關注本站

  • 電子郵件
  • Facebook

公益

  • 家扶基金會
  • 台灣世界展望會
  • Yahoo 奇摩公益
  • igiving 公益網
  • 兒福聯盟

Copyright © 2021 · Office Guide