postgresql的注释工具层面的支持并不友好,因此可采用命令的形式来进行字段、表进行添加注释。同时,也可以通过一条SQL语句来查询字段的注释和类型。

首先我们来看添加注释:

表添加注释

comment on table tb_user is 'The user table';

其中tb_user替换成对应的表明,单引号内的描述替换为对应表的描述即可。

字段添加注释

comment on column tb_user.id is 'The user ID';

其中tb_user.id指的是表tb_user的id字段,同样单引号内的内容为注释的具体信息。

查询注释

SELECT  
a.attname as "字段名",  
col_description(a.attrelid,a.attnum) as "注释",  
concat_ws('',t.typname,SUBSTRING(format_type(a.atttypid,a.atttypmod) from '\(.*\)')) as "字段类型"  
FROM  
pg_class as c,  
pg_attribute as a,  
pg_type as t  
WHERE  
c.relname = 'tb_user'  
and a.atttypid = t.oid  
and a.attrelid = c.oid  
and a.attnum>0;

在上述查询注释语句中只需要替换掉c.relname=’tb_user’中的tb_user为你要查询的表即可。此时便可查询除对应表结构的描述和字段类型信息。

使用SQL Pro工具查询时,查询效果如下:

postgresql表、字段添加注释和查询注释插图


postgresql表、字段添加注释和查询注释插图1

关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台

除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接

本文链接:http://choupangxia.com/2020/06/19/postgresql-column-comment/