如何確認 PostgreSQL 中是否存在欄位輸入限制
Copyright Notice: This article is an original work licensed under the CC 4.0 BY-NC-ND license.
If you wish to repost this article, please include the original source link and this copyright notice.
Source link: https://v2know.com/article/1122
這是精簡版的技術筆記/博客草稿,只保留核心查詢與結論,適合快速記錄或內部分享用:
PostgreSQL 資料輸入限制快速排查(精簡版)
在維護 PostgreSQL 資料庫時,若想確認「是否有資料層級的欄位輸入限制」,可以透過以下 SQL 進行快速判斷。
🔍 查詢所有資料表的 CHECK 條件:
SELECT
t.relname AS table_name,
c.conname AS constraint_name,
pg_get_constraintdef(c.oid) AS definition
FROM pg_constraint c
JOIN pg_class t ON c.conrelid = t.oid
JOIN pg_namespace n ON n.oid = t.relnamespace
WHERE c.contype = 'c'
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY t.relname, c.conname;
若查無結果,即可確認目前資料庫中沒有任何 CHECK
約束。
🔍 查詢所有觸發器(Trigger):
SELECT
event_object_table AS table_name,
trigger_name,
action_timing,
event_manipulation,
action_statement
FROM information_schema.triggers
ORDER BY event_object_table, trigger_name;
如觸發器皆為自建且無驗證邏輯,則可確認資料庫未對輸入做任何限制。
✅ 總結
-
查無
CHECK
→ ✅ 無欄位限制 -
Trigger 無驗證邏輯 → ✅ 無複雜輸入限制
→ 可以判定:目前資料庫層不會限制輸入資料的合法性,一切輸入行為皆由應用層或使用者自行決定。
This article was last edited at