PostgreSQL中的COMMENT用法說明
PostgreSQL附帶了一個命令 - COMMENT 。如果想要記錄數(shù)據(jù)庫中的內(nèi)容,這個命令很有用。本文將介紹如何使用此命令。
隨著數(shù)據(jù)庫的不斷發(fā)展和數(shù)據(jù)關(guān)系變得越來越復(fù)雜,跟蹤數(shù)據(jù)庫中添加的所有內(nèi)容會變得非常困難。要記錄數(shù)據(jù)的組織方式以及可能隨時間添加或更改的組件,有必要添加某種文檔。
例如,文檔可以寫在外部文件中,但這會產(chǎn)生一種問題,他們很快就會變?yōu)檫^時的文件。PostgreSQL有一個解決這個問題的方法:COMMENT命令。使用它可以向各種數(shù)據(jù)庫對象添加注釋,例如在需要時更新的列,索引,表和函數(shù)。
查看數(shù)據(jù)和添加注釋
PostgreSQL的psql交互式shell包含許多強(qiáng)大的命令來查看和操作數(shù)據(jù)。\d命令會顯示所有可見表,視圖,物化視圖,序列和外部表的列表。還有幾種\d命令的組合可用于指定是否要查看索引,映射,約束等。結(jié)合+(例如\d+),該命令將為您提供對象的擴(kuò)展視圖,包含一個描述列,這是文檔或COMMENT編寫的位置。
COMMENT命令是我們將數(shù)據(jù)描述添加到數(shù)據(jù)庫對象的方法。不要將COMMENT與\ * * \或 SQL中的 -- 相混淆,因為它們是在SQL文件中編寫的,在數(shù)據(jù)庫中不可見。另一方面,COMMENT不是標(biāo)準(zhǔn)SQL,而是PostgreSQL獨有的。
有很多數(shù)據(jù)庫對象可供我們使用COMMENT命令。其中最常見的是表,索引和列。但是,必須是對象的所有者或管理員才能使用COMMENT。
運行\(zhòng)d+以顯示表及其描述,例如:
postgres=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+------------------+---------------+----------+------------+---------------
public | commenttest | table | postgres | 8192 bytes |
由于commenttest是一個剛剛創(chuàng)建的新表,因此Description列為空。可以通過以下命令添加注釋:
postgres=# COMMENT ON TABLE commenttest IS 'A table of students in different departments'; COMMENT
現(xiàn)在再次運行\(zhòng)d+,可以看到描述列填充了注釋。
postgres=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+------------------+---------------+----------+------------+---------------
public | commenttest | table | postgres | 8192 bytes | A table of students in different departments
這是向表中添加描述信息的步驟。 接著,我們需要考慮如何向表的列中添加描述。
要查看表中每個列的描述列,可以運行類似以下命令:
postgres=# \d+ commenttest
Table "public.commenttest"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
-----------------+---------+-----------+----------+---------+----------+--------------+-------------
student_id | integer | | | | plain | |
student_name | text | | | | extended | |
student_major | text | | | | extended | |
department_id | integer | | | | plain | |
department_name | text | | | | extended | |
nationality | text | | | | extended | |
為每列添加描述與我們在表中添加一個列的方式類似。例如:
postgres=# COMMENT ON COLUMN commenttest.student_id IS 'ID of the student'; COMMENT postgres=# COMMENT ON COLUMN commenttest.student_name IS 'name of the student'; COMMENT postgres=# COMMENT ON COLUMN commenttest.student_major IS 'major of the student'; COMMENT postgres=# COMMENT ON COLUMN commenttest.department_id IS 'ID of the department'; COMMENT postgres=# COMMENT ON COLUMN commenttest.department_name IS 'name of the department'; COMMENT postgres=# COMMENT ON COLUMN commenttest.nationality IS 'nationality of the student'; COMMENT
添加描述后,再次查看表的描述列信息:
postgres=# \d+ commenttest
Table "public.commenttest"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
-----------------+---------+-----------+----------+---------+----------+--------------+----------------------------
student_id | integer | | | | plain | | ID of the student
student_name | text | | | | extended | | name of the student
student_major | text | | | | extended | | major of the student
department_id | integer | | | | plain | | ID of the department
department_name | text | | | | extended | | name of the department
nationality | text | | | | extended | | nationality of the student
可以看到描述列已經(jīng)添加好相應(yīng)注釋。這樣添加過注釋之后,名字復(fù)雜且難懂的列名就能讓最終用戶比較容易理解且不會產(chǎn)生歧義。
我們也可以使用類似的方式向索引中添加描述,這樣在數(shù)據(jù)庫使用過程中,可以防止由于索引數(shù)量的增加而導(dǎo)致的混淆和歧義問題。
而且如果使用pg_dump遷移PostgreSQL數(shù)據(jù)庫,則使用COMMENT進(jìn)行的任何注釋都會存儲在轉(zhuǎn)儲文件中。
補(bǔ)充:給postgresql數(shù)據(jù)庫的表和列添加注釋(comment)
postgresql 數(shù)據(jù)庫國內(nèi)用的人并不是很多,而一些老項目采用了這個數(shù)據(jù)庫。維護(hù)起來特別麻煩,因為國內(nèi)用的人比較少,相關(guān)資料也很少。
另外還有一些函數(shù),postgresql 也沒有對應(yīng)的提供。還有對于表分區(qū),低版本的 postgresql 數(shù)據(jù)庫根本都沒有這個功能,不支持。需要自己自動的創(chuàng)建表進(jìn)行分區(qū)。
總之 postgresql 數(shù)據(jù)庫用起來實在是太過麻煩,本文總結(jié)了一些給 postgresql 數(shù)據(jù)庫的表和列添加注釋的方法,方便已經(jīng)采用 postgresql 數(shù)據(jù)庫而不得不用的程序員。
首先說給表添加注釋:
comment on table xttblog is '業(yè)余草';
其中 xttblog 是表名,添加的注釋是“業(yè)余草”。
給列添加注釋的方法如下:
create table xttblog(id int not null, url_id int); comment on column xttblog.id is '主鍵ID,自增';
注意創(chuàng)建表的時候,不能再列后面加 comment 。添加后執(zhí)行會報錯,因為這是 MySQL,Oracle的用法,不是 Postgresql 的用法。
下面再說說如何查詢表中的注釋。sql 語句如下:
select description from pg_descriptionjoin pg_class on pg_description.objoid = pg_class.oid where relname = 'xttblog'
其中以 pg_ 開頭的表都是 Postgresql 數(shù)據(jù)庫的系統(tǒng)表。系統(tǒng)表中存儲著很多與表和配置相關(guān)的信息。
PostgreSQL 獲取數(shù)據(jù)表的注釋信息和表中字段的注釋信息和上面的 SQL 類似。
和表相關(guān)的信息都在 pg_description 這個表中,查 pg_description 這個系統(tǒng)表,里面有存表和字段的備注。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
- PostgreSQL LIKE 大小寫實例
- Postgresql中LIKE和ILIKE操作符的用法詳解
- 使用PostgreSQL為表或視圖創(chuàng)建備注的操作
- postgresql安裝及配置超詳細(xì)教程
- Docker環(huán)境下升級PostgreSQL的步驟方法詳解
- postgresql insert into select無法使用并行查詢的解決
- postgreSQL 使用timestamp轉(zhuǎn)成date格式
- postgresql varchar字段regexp_replace正則替換操作
- 關(guān)于PostgreSQL錯誤日志與慢查詢?nèi)罩臼占?/a>
- 淺談PostgreSQL中大小寫不敏感問題
相關(guān)文章
使用pg_basebackup對Postgre進(jìn)行備份與恢復(fù)的實現(xiàn)
這篇文章主要介紹了使用pg_basebackup對Postgre進(jìn)行備份與恢復(fù)的實現(xiàn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
postgresql兼容MySQL on update current_timestamp
這篇文章主要介紹了postgresql兼容MySQL on update current_timestamp問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
sqoop讀取postgresql數(shù)據(jù)庫表格導(dǎo)入到hdfs中的實現(xiàn)
這篇文章主要介紹了sqoop讀取postgresql數(shù)據(jù)庫表格導(dǎo)入到hdfs中的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
PostgreSQL設(shè)置時區(qū)、時間/日期函數(shù)匯總大全
PostgreSQL是一款簡介而又性能強(qiáng)大的數(shù)據(jù)庫應(yīng)用程序,其在日期時間數(shù)據(jù)方面所支持的功能也都非常給力,這篇文章主要給大家介紹了關(guān)于PostgreSQL設(shè)置時區(qū)、時間/日期函數(shù)的相關(guān)資料,需要的朋友可以參考下2023-09-09
postgresql 實現(xiàn)數(shù)據(jù)的導(dǎo)入導(dǎo)出
這篇文章主要介紹了postgresql 實現(xiàn)數(shù)據(jù)的導(dǎo)入導(dǎo)出,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
PostgreSQL模式匹配與正則表達(dá)式方法總結(jié)
在postgresql中使用正則表達(dá)式時需要使用關(guān)鍵字“~”,以表示該關(guān)鍵字之前的內(nèi)容需匹配之后的正則表達(dá)式,這篇文章主要給大家介紹了關(guān)于PostgreSQL模式匹配與正則表達(dá)式的相關(guān)資料,需要的朋友可以參考下2022-11-11
Postgresql數(shù)據(jù)庫之創(chuàng)建和修改序列的操作
這篇文章主要介紹了Postgresql數(shù)據(jù)庫之創(chuàng)建和修改序列的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
淺談PostgreSQL中的孤兒文件用法(orphaned data files)
這篇文章主要介紹了淺談PostgreSQL中的孤兒文件用法(orphaned data files),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL查詢修改max_connections(最大連接數(shù))及其它配置詳解
postgresql數(shù)據(jù)庫最大連接數(shù)是系統(tǒng)允許的最大連接數(shù),當(dāng)數(shù)據(jù)庫并發(fā)用戶超過該連接數(shù)后,會導(dǎo)致新連接無法建立或者連接超時,這篇文章主要給大家介紹了關(guān)于PostgreSQL查詢修改max_connections(最大連接數(shù))及其它配置的相關(guān)資料,需要的朋友可以參考下2024-01-01

