PostgreSQL中的COMMENT用法說(shuō)明
PostgreSQL附帶了一個(gè)命令 - COMMENT 。如果想要記錄數(shù)據(jù)庫(kù)中的內(nèi)容,這個(gè)命令很有用。本文將介紹如何使用此命令。
隨著數(shù)據(jù)庫(kù)的不斷發(fā)展和數(shù)據(jù)關(guān)系變得越來(lái)越復(fù)雜,跟蹤數(shù)據(jù)庫(kù)中添加的所有內(nèi)容會(huì)變得非常困難。要記錄數(shù)據(jù)的組織方式以及可能隨時(shí)間添加或更改的組件,有必要添加某種文檔。
例如,文檔可以寫在外部文件中,但這會(huì)產(chǎn)生一種問(wèn)題,他們很快就會(huì)變?yōu)檫^(guò)時(shí)的文件。PostgreSQL有一個(gè)解決這個(gè)問(wèn)題的方法:COMMENT命令。使用它可以向各種數(shù)據(jù)庫(kù)對(duì)象添加注釋,例如在需要時(shí)更新的列,索引,表和函數(shù)。
查看數(shù)據(jù)和添加注釋
PostgreSQL的psql交互式shell包含許多強(qiáng)大的命令來(lái)查看和操作數(shù)據(jù)。\d命令會(huì)顯示所有可見(jiàn)表,視圖,物化視圖,序列和外部表的列表。還有幾種\d命令的組合可用于指定是否要查看索引,映射,約束等。結(jié)合+(例如\d+),該命令將為您提供對(duì)象的擴(kuò)展視圖,包含一個(gè)描述列,這是文檔或COMMENT編寫的位置。
COMMENT命令是我們將數(shù)據(jù)描述添加到數(shù)據(jù)庫(kù)對(duì)象的方法。不要將COMMENT與\ * * \或 SQL中的 -- 相混淆,因?yàn)樗鼈兪窃赟QL文件中編寫的,在數(shù)據(jù)庫(kù)中不可見(jiàn)。另一方面,COMMENT不是標(biāo)準(zhǔn)SQL,而是PostgreSQL獨(dú)有的。
有很多數(shù)據(jù)庫(kù)對(duì)象可供我們使用COMMENT命令。其中最常見(jiàn)的是表,索引和列。但是,必須是對(duì)象的所有者或管理員才能使用COMMENT。
運(yùn)行\(zhòng)d+以顯示表及其描述,例如:
postgres=# \d+ List of relations Schema | Name | Type | Owner | Size | Description --------+------------------+---------------+----------+------------+--------------- public | commenttest | table | postgres | 8192 bytes |
由于commenttest是一個(gè)剛剛創(chuàng)建的新表,因此Description列為空??梢酝ㄟ^(guò)以下命令添加注釋:
postgres=# COMMENT ON TABLE commenttest IS 'A table of students in different departments'; COMMENT
現(xiàn)在再次運(yù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
這是向表中添加描述信息的步驟。 接著,我們需要考慮如何向表的列中添加描述。
要查看表中每個(gè)列的描述列,可以運(yùn)行類似以下命令:
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 | |
為每列添加描述與我們?cè)诒碇刑砑右粋€(gè)列的方式類似。例如:
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)注釋。這樣添加過(guò)注釋之后,名字復(fù)雜且難懂的列名就能讓最終用戶比較容易理解且不會(huì)產(chǎn)生歧義。
我們也可以使用類似的方式向索引中添加描述,這樣在數(shù)據(jù)庫(kù)使用過(guò)程中,可以防止由于索引數(shù)量的增加而導(dǎo)致的混淆和歧義問(wèn)題。
而且如果使用pg_dump遷移PostgreSQL數(shù)據(jù)庫(kù),則使用COMMENT進(jìn)行的任何注釋都會(huì)存儲(chǔ)在轉(zhuǎn)儲(chǔ)文件中。
補(bǔ)充:給postgresql數(shù)據(jù)庫(kù)的表和列添加注釋(comment)
postgresql 數(shù)據(jù)庫(kù)國(guó)內(nèi)用的人并不是很多,而一些老項(xiàng)目采用了這個(gè)數(shù)據(jù)庫(kù)。維護(hù)起來(lái)特別麻煩,因?yàn)閲?guó)內(nèi)用的人比較少,相關(guān)資料也很少。
另外還有一些函數(shù),postgresql 也沒(méi)有對(duì)應(yīng)的提供。還有對(duì)于表分區(qū),低版本的 postgresql 數(shù)據(jù)庫(kù)根本都沒(méi)有這個(gè)功能,不支持。需要自己自動(dòng)的創(chuàng)建表進(jìn)行分區(qū)。
總之 postgresql 數(shù)據(jù)庫(kù)用起來(lái)實(shí)在是太過(guò)麻煩,本文總結(jié)了一些給 postgresql 數(shù)據(jù)庫(kù)的表和列添加注釋的方法,方便已經(jīng)采用 postgresql 數(shù)據(jù)庫(kù)而不得不用的程序員。
首先說(shuō)給表添加注釋:
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)建表的時(shí)候,不能再列后面加 comment 。添加后執(zhí)行會(huì)報(bào)錯(cuò),因?yàn)檫@是 MySQL,Oracle的用法,不是 Postgresql 的用法。
下面再說(shuō)說(shuō)如何查詢表中的注釋。sql 語(yǔ)句如下:
select description from pg_descriptionjoin pg_class on pg_description.objoid = pg_class.oid where relname = 'xttblog'
其中以 pg_ 開(kāi)頭的表都是 Postgresql 數(shù)據(jù)庫(kù)的系統(tǒng)表。系統(tǒng)表中存儲(chǔ)著很多與表和配置相關(guān)的信息。
PostgreSQL 獲取數(shù)據(jù)表的注釋信息和表中字段的注釋信息和上面的 SQL 類似。
和表相關(guān)的信息都在 pg_description 這個(gè)表中,查 pg_description 這個(gè)系統(tǒng)表,里面有存表和字段的備注。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- PostgreSQL LIKE 大小寫實(shí)例
- Postgresql中LIKE和ILIKE操作符的用法詳解
- 使用PostgreSQL為表或視圖創(chuàng)建備注的操作
- postgresql安裝及配置超詳細(xì)教程
- Docker環(huán)境下升級(jí)PostgreSQL的步驟方法詳解
- postgresql insert into select無(wú)法使用并行查詢的解決
- postgreSQL 使用timestamp轉(zhuǎn)成date格式
- postgresql varchar字段regexp_replace正則替換操作
- 關(guān)于PostgreSQL錯(cuò)誤日志與慢查詢?nèi)罩臼占?/a>
- 淺談PostgreSQL中大小寫不敏感問(wèn)題
相關(guān)文章
Mysql?8.0.33?如何遷移至?Postgresql?16.2
由于云平臺(tái)需要改造,將Mysql替換成Postgresql,話說(shuō)回來(lái),Postgresql和Mysql語(yǔ)法有些差異,如何穩(wěn)妥的進(jìn)行遷移,下面給大家分享Mysql?8.0.33?如何遷移至?Postgresql?16.2,感興趣的朋友跟隨小編一起看看吧2024-05-05postgresql 實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入導(dǎo)出
這篇文章主要介紹了postgresql 實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入導(dǎo)出,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12postgreSQL自動(dòng)生成隨機(jī)數(shù)值的實(shí)例
這篇文章主要介紹了postgreSQL自動(dòng)生成隨機(jī)數(shù)值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01自定義函數(shù)實(shí)現(xiàn)單詞排序并運(yùn)用于PostgreSQL(實(shí)現(xiàn)代碼)
這篇文章主要介紹了自定義函數(shù)實(shí)現(xiàn)單詞排序并運(yùn)用于PostgreSQL,本文給大家分享實(shí)現(xiàn)代碼,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04postgresql pg_hba.conf 簡(jiǎn)介及配置詳解
配置文件之pg_hba.conf該文件用于控制訪問(wèn)安全性,管理客戶端對(duì)于PostgreSQL服務(wù)器的訪問(wèn)權(quán)限,本文給大家介紹postgresql pg_hba.conf 簡(jiǎn)介及配置,感興趣的朋友跟隨小編一起看看吧2024-03-03在postgresql數(shù)據(jù)庫(kù)中創(chuàng)建只讀用戶的操作
這篇文章主要介紹了在postgresql數(shù)據(jù)庫(kù)中創(chuàng)建只讀用戶的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12