PostgreSQL中的COMMENT

版权声明:本文为博主原创之文章,未经博主允许谢绝转载。 https://blog.csdn.net/pg_hgdb/article/details/83148360

PostgreSQL附带了一个命令 - COMMENT 。如果想要记录数据库中的内容,这个命令很有用。本文将介绍如何使用此命令。

随着数据库的不断发展和数据关系变得越来越复杂,跟踪数据库中添加的所有内容会变得非常困难。要记录数据的组织方式以及可能随时间添加或更改的组件,有必要添加某种文档。

例如,文档可以写在外部文件中,但这会产生一种问题,他们很快就会变为过时的文件。PostgreSQL有一个解决这个问题的方法:COMMENT命令。使用它可以向各种数据库对象添加注释,例如在需要时更新的列,索引,表和函数。

查看数据和添加注释

PostgreSQL的psql交互式shell包含许多强大的命令来查看和操作数据。\d命令会显示所有可见表,视图,物化视图,序列和外部表的列表。还有几种\d命令的组合可用于指定是否要查看索引,映射,约束等。结合+(例如\d+),该命令将为您提供对象的扩展视图,包含一个描述列,这是文档或COMMENT编写的位置。

COMMENT命令是我们将数据描述添加到数据库对象的方法。不要将COMMENT与\ * * \或 SQL中的 -- 相混淆,因为它们是在SQL文件中编写的,在数据​​库中不可见。另一方面,COMMENT不是标准SQL,而是PostgreSQL独有的。

有很多数据库对象可供我们使用COMMENT命令。其中最常见的是表,索引和列。但是,必须是对象的所有者或管理员才能使用COMMENT。

运行\d+以显示表及其描述,例如:

postgres=# \d+
                                 List of relations
 Schema |       Name       |     Type      |  Owner   |    Size    |  Description  
--------+------------------+---------------+----------+------------+---------------
public | commenttest      | table         | postgres | 8192 bytes |

由于commenttest是一个刚刚创建的新表,因此Description列为空。可以通过以下命令添加注释:

postgres=# COMMENT ON TABLE commenttest IS 'A table of students in different departments';  
COMMENT

现在再次运行\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

可以看到描述列已经添加好相应注释。这样添加过注释之后,名字复杂且难懂的列名就能让最终用户比较容易理解且不会产生歧义。

我们也可以使用类似的方式向索引中添加描述,这样在数据库使用过程中,可以防止由于索引数量的增加而导致的混淆和歧义问题。

而且如果使用pg_dump迁移PostgreSQL数据库,则使用COMMENT进行的任何注释都会存储在转储文件中。

 

By kalath

猜你喜欢

转载自blog.csdn.net/pg_hgdb/article/details/83148360