PostgreSQL COMMENT

2021-09-09 09:50 更新

COMMENT — 定義或者更改一個對象的注釋

大綱

COMMENT ON
{
  ACCESS METHOD object_name |
  AGGREGATE aggregate_name ( aggregate_signature ) |
  CAST (source_type AS target_type) |
  COLLATION object_name |
  COLUMN relation_name.column_name |
  CONSTRAINT constraint_name ON table_name |
  CONSTRAINT constraint_name ON DOMAIN domain_name |
  CONVERSION object_name |
  DATABASE object_name |
  DOMAIN object_name |
  EXTENSION object_name |
  EVENT TRIGGER object_name |
  FOREIGN DATA WRAPPER object_name |
  FOREIGN TABLE object_name |
  FUNCTION function_name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] |
  INDEX object_name |
  LARGE OBJECT large_object_oid |
  MATERIALIZED VIEW object_name |
  OPERATOR operator_name (left_type, right_type) |
  OPERATOR CLASS object_name USING index_method |
  OPERATOR FAMILY object_name USING index_method |
  POLICY policy_name ON table_name |
  [ PROCEDURAL ] LANGUAGE object_name |
  PROCEDURE procedure_name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] |
  PUBLICATION object_name |
  ROLE object_name |
  ROUTINE routine_name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] |
  RULE rule_name ON table_name |
  SCHEMA object_name |
  SEQUENCE object_name |
  SERVER object_name |
  STATISTICS object_name |
  SUBSCRIPTION object_name |
  TABLE object_name |
  TABLESPACE object_name |
  TEXT SEARCH CONFIGURATION object_name |
  TEXT SEARCH DICTIONARY object_name |
  TEXT SEARCH PARSER object_name |
  TEXT SEARCH TEMPLATE object_name |
  TRANSFORM FOR type_name LANGUAGE lang_name |
  TRIGGER trigger_name ON table_name |
  TYPE object_name |
  VIEW object_name
} IS 'text'

其中 aggregate_signature 是:

* |
[ argmode ] [ argname ] argtype [ , ... ] |
[ [ argmode ] [ argname ] argtype [ , ... ] ] ORDER BY [ argmode ] [ argname ] argtype [ , ... ]

描述

COMMENT存儲關(guān)于一個數(shù)據(jù)庫對象的注釋。

對每一個對象只保存一個注釋字符串,因此為了修改一段注釋,對同一個對象 發(fā)出一個新的COMMENT命令。要移除一段注釋,可在文 本字符串的位置上寫上NULL。當(dāng)對象被刪除時,其注釋 也會被自動刪除。

對大部分類型的對象,只有對象的擁有者可以設(shè)置注釋。角色沒有擁有者,因此 COMMENT ON ROLE的規(guī)則是你必須作為一個超級用戶來對一個 超級用戶角色設(shè)置注釋,或者具有CREATEROLE特權(quán)來對非超級用 戶角色設(shè)置注釋。同樣的,訪問方法也沒有擁有者,你必須作為一個超級用戶來 對一個訪問方法設(shè)置注釋。當(dāng)然,一個超級用戶可以對任何東西設(shè)置注釋。

使用psql\d 命令家族可以查看注釋。其他檢索注釋的用戶接口可以構(gòu)建在 psql使用的內(nèi)建函數(shù)之上,即 obj_description、col_description 以及shobj_description (見 表 9.73)。

參數(shù)

object_name
relation_name.column_name
aggregate_name
constraint_name
function_name
operator_name
policy_name
procedure_name
routine_name
rule_name
trigger_name

要被注釋的對象的名稱。表、聚集、排序方式、轉(zhuǎn)換、域、外部表、函數(shù)、 索引、操作符、操作符類、操作符族、存儲過程、例程、序列、統(tǒng)計信息、文本搜索對象、類型和視圖 的名稱可以被模式限定。在注釋一列時, relation_name必須 引用一個表、視圖、組合類型或者外部表。

table_name
domain_name

當(dāng)在一個約束、觸發(fā)器、規(guī)則或者策略上創(chuàng)建一段注釋時,這些參數(shù)指定在其上定義 該對象的表或域的名稱。

source_type

造型的源數(shù)據(jù)類型的名稱。

target_type

造型的目標(biāo)數(shù)據(jù)類型的名稱。

argmode

一個函數(shù),存儲過程或者聚集函數(shù)的參數(shù)的模式:INOUT、INOUT或者VARIADIC。 如果被省略,默認(rèn)值是IN。注意 COMMENT并不真正關(guān)心 OUT參數(shù),因為決定函數(shù)的身份只需要輸入?yún)?shù)。因此 列出IN、INOUTVARIADIC 參數(shù)就足夠了。

argname

一個函數(shù),存儲過程或者聚集函數(shù)參數(shù)的名稱。注意 COMMENT并不真正關(guān)心參數(shù)名稱, 因為決定函數(shù)的身份只需要參數(shù)數(shù)據(jù)類型。

argtype

一個函數(shù),存儲過程或者聚集函數(shù)參數(shù)的數(shù)據(jù)類型。

large_object_oid

大對象的 OID。

left_type
right_type

操作符的參數(shù)的數(shù)據(jù)類型(可以是模式限定的)。對一個前綴后后綴操作符 的缺失參數(shù)可以寫NONE

PROCEDURAL

這是一個噪聲詞。

type_name

該轉(zhuǎn)換的數(shù)據(jù)類型的名稱。

lang_name

該轉(zhuǎn)換的語言的名稱。

text

寫成一個字符串的新注釋。如果要刪除注釋,寫成NULL。

注解

當(dāng)前對查看注釋沒有安全機制:任何連接到一個數(shù)據(jù)庫的用戶能夠看到 該數(shù)據(jù)庫中所有對象的注釋。對于數(shù)據(jù)庫、角色、表空間這類共享對象, 注釋被全局存儲,因此連接到集簇中任何數(shù)據(jù)庫的任何用戶可以看到共 享對象的所有注釋。因此,不要在注釋中放置有安全性風(fēng)險的信息。

示例

為表mytable附加一段注釋:

COMMENT ON TABLE mytable IS 'This is my table.';

移除它:

COMMENT ON TABLE mytable IS NULL;

更多的一些例子:

COMMENT ON ACCESS METHOD gin IS 'GIN index access method';
COMMENT ON AGGREGATE my_aggregate (double precision) IS 'Computes sample variance';
COMMENT ON CAST (text AS int4) IS 'Allow casts from text to int4';
COMMENT ON COLLATION "fr_CA" IS 'Canadian French';
COMMENT ON COLUMN my_table.my_column IS 'Employee ID number';
COMMENT ON CONVERSION my_conv IS 'Conversion to UTF8';
COMMENT ON CONSTRAINT bar_col_cons ON bar IS 'Constrains column col';
COMMENT ON CONSTRAINT dom_col_constr ON DOMAIN dom IS 'Constrains col of domain';
COMMENT ON DATABASE my_database IS 'Development Database';
COMMENT ON DOMAIN my_domain IS 'Email Address Domain';
COMMENT ON EVENT TRIGGER abort_ddl IS 'Aborts all DDL commands';
COMMENT ON EXTENSION hstore IS 'implements the hstore data type';
COMMENT ON FOREIGN DATA WRAPPER mywrapper IS 'my foreign data wrapper';
COMMENT ON FOREIGN TABLE my_foreign_table IS 'Employee Information in other database';
COMMENT ON FUNCTION my_function (timestamp) IS 'Returns Roman Numeral';
COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee ID';
COMMENT ON LANGUAGE plpython IS 'Python support for stored procedures';
COMMENT ON LARGE OBJECT 346344 IS 'Planning document';
COMMENT ON MATERIALIZED VIEW my_matview IS 'Summary of order history';
COMMENT ON OPERATOR ^ (text, text) IS 'Performs intersection of two texts';
COMMENT ON OPERATOR - (NONE, integer) IS 'Unary minus';
COMMENT ON OPERATOR CLASS int4ops USING btree IS '4 byte integer operators for btrees';
COMMENT ON OPERATOR FAMILY integer_ops USING btree IS 'all integer operators for btrees';
COMMENT ON POLICY my_policy ON mytable IS 'Filter rows by users';
COMMENT ON PROCEDURE my_proc (integer, integer) IS 'Runs a report';
COMMENT ON PUBLICATION alltables IS 'Publishes all operations on all tables';
COMMENT ON ROLE my_role IS 'Administration group for finance tables';
COMMENT ON ROUTINE my_routine (integer, integer) IS 'Runs a routine (which is a function or procedure)';
COMMENT ON RULE my_rule ON my_table IS 'Logs updates of employee records';
COMMENT ON SCHEMA my_schema IS 'Departmental data';
COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys';
COMMENT ON SERVER myserver IS 'my foreign server';
COMMENT ON STATISTICS my_statistics IS 'Improves planner row estimations';
COMMENT ON SUBSCRIPTION alltables IS 'Subscription for all operations on all tables';
COMMENT ON TABLE my_schema.my_table IS 'Employee Information';
COMMENT ON TABLESPACE my_tablespace IS 'Tablespace for indexes';
COMMENT ON TEXT SEARCH CONFIGURATION my_config IS 'Special word filtering';
COMMENT ON TEXT SEARCH DICTIONARY swedish IS 'Snowball stemmer for Swedish language';
COMMENT ON TEXT SEARCH PARSER my_parser IS 'Splits text into words';
COMMENT ON TEXT SEARCH TEMPLATE snowball IS 'Snowball stemmer';
COMMENT ON TRANSFORM FOR hstore LANGUAGE plpythonu IS 'Transform between hstore and Python dict';
COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for RI';
COMMENT ON TYPE complex IS 'Complex number data type';
COMMENT ON VIEW my_view IS 'View of departmental costs';

兼容性

SQL 標(biāo)準(zhǔn)中沒有COMMENT命令。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號