PostgreSQL附帶了一個(gè)命令 - COMMENT 。如果想要記錄數(shù)據(jù)庫中的內(nèi)容,這個(gè)命令很有用。本文將介紹如何使用此命令。
隨著數(shù)據(jù)庫的不斷發(fā)展和數(shù)據(jù)關(guān)系變得越來越復(fù)雜,跟蹤數(shù)據(jù)庫中添加的所有內(nèi)容會(huì)變得非常困難。要記錄數(shù)據(jù)的組織方式以及可能隨時(shí)間添加或更改的組件,有必要添加某種文檔。
例如,文檔可以寫在外部文件中,但這會(huì)產(chǎn)生一種問題,他們很快就會(huì)變?yōu)檫^時(shí)的文件。PostgreSQL有一個(gè)解決這個(gè)問題的方法:COMMENT命令。使用它可以向各種數(shù)據(jù)庫對(duì)象添加注釋,例如在需要時(shí)更新的列,索引,表和函數(shù)。
查看數(shù)據(jù)和添加注釋
PostgreSQL的psql交互式shell包含許多強(qiáng)大的命令來查看和操作數(shù)據(jù)。\d命令會(huì)顯示所有可見表,視圖,物化視圖,序列和外部表的列表。還有幾種\d命令的組合可用于指定是否要查看索引,映射,約束等。結(jié)合+(例如\d+),該命令將為您提供對(duì)象的擴(kuò)展視圖,包含一個(gè)描述列,這是文檔或COMMENT編寫的位置。
COMMENT命令是我們將數(shù)據(jù)描述添加到數(shù)據(jù)庫對(duì)象的方法。不要將COMMENT與\ * * \或 SQL中的 -- 相混淆,因?yàn)樗鼈兪窃赟QL文件中編寫的,在數(shù)據(jù)庫中不可見。另一方面,COMMENT不是標(biāo)準(zhǔn)SQL,而是PostgreSQL獨(dú)有的。
有很多數(shù)據(jù)庫對(duì)象可供我們使用COMMENT命令。其中最常見的是表,索引和列。但是,必須是對(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列為空??梢酝ㄟ^以下命令添加注釋:
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)注釋。這樣添加過注釋之后,名字復(fù)雜且難懂的列名就能讓最終用戶比較容易理解且不會(huì)產(chǎn)生歧義。
我們也可以使用類似的方式向索引中添加描述,這樣在數(shù)據(jù)庫使用過程中,可以防止由于索引數(shù)量的增加而導(dǎo)致的混淆和歧義問題。
而且如果使用pg_dump遷移PostgreSQL數(shù)據(jù)庫,則使用COMMENT進(jìn)行的任何注釋都會(huì)存儲(chǔ)在轉(zhuǎn)儲(chǔ)文件中。
補(bǔ)充:給postgresql數(shù)據(jù)庫的表和列添加注釋(comment)
postgresql 數(shù)據(jù)庫國內(nèi)用的人并不是很多,而一些老項(xiàng)目采用了這個(gè)數(shù)據(jù)庫。維護(hù)起來特別麻煩,因?yàn)閲鴥?nèi)用的人比較少,相關(guān)資料也很少。
另外還有一些函數(shù),postgresql 也沒有對(duì)應(yīng)的提供。還有對(duì)于表分區(qū),低版本的 postgresql 數(shù)據(jù)庫根本都沒有這個(gè)功能,不支持。需要自己自動(dòng)的創(chuàng)建表進(jìn)行分區(qū)。
總之 postgresql 數(shù)據(jù)庫用起來實(shí)在是太過麻煩,本文總結(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)建表的時(shí)候,不能再列后面加 comment 。添加后執(zhí)行會(huì)報(bào)錯(cuò),因?yàn)檫@是 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)表中存儲(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無法使用并行查詢的解決
- postgreSQL 使用timestamp轉(zhuǎn)成date格式
- postgresql varchar字段regexp_replace正則替換操作
- 關(guān)于PostgreSQL錯(cuò)誤日志與慢查詢?nèi)罩臼占?/li>
- 淺談PostgreSQL中大小寫不敏感問題