本文为您介绍Hologres中注释Comment使用相关内容。

语法介绍

在Hologres中,支持给内部表、外部表、列增加注释(Comment),命令语法如下。语法与PostgreSQL一致,详情请参见COMMENT。Comment可以在建表时使用,也可以建完表之后单独执行。
begin;
create table  [schema_name.]table_name ([
  {
   column_name column_type [column_constraints, [...]]
   | table_constraints
   [, ...]
  }
]);
comment on column <tablename.column> is 'value';--给列加注释
comment on table <tablename> is 'value';--给表加注释
commit;

使用示例

  • 建表时添加注释
    BEGIN;
    CREATE TABLE public."user" (
     "id" text NOT NULL,
     "name" text NOT NULL
    );
    COMMENT ON TABLE public."user" is '用户属性表';
    COMMENT ON COLUMN public."user".id is '身份证号';
    COMMENT ON COLUMN public."user".name is '姓名';
    COMMIT;
  • 单独执行添加注释。
    -- 给表增加注释
    COMMENT ON TABLE table_name IS 'my comments on table table_name.';
    
    -- 给列增加注释
    COMMENT ON COLUMN table_name.col1 IS 'This my first col1';
    
    -- 给外部表增加注释
    COMMENT ON FOREIGN TABLE foreign_table IS ' comments on my foreign table';

通过系统表查看注释

可以通过系统表查看为表、列设置的注释,命令示例如下。
SELECT a.attname as Column,
  pg_catalog.format_type(a.atttypid, a.atttypmod) as "Type",
  a.attnotnull as "Nullable",
  pg_catalog.col_description(a.attrelid, a.attnum) as "Description"
FROM pg_catalog.pg_attribute a
WHERE a.attnum > 0 AND NOT a.attisdropped AND a.attrelid = '<schema.tablename>'::regclass::oid
ORDER BY a.attnum;