代码之家  ›  专栏  ›  技术社区  ›  Chris Martin

我如何看待PostgreSQL约束上的注释?

  •  1
  • Chris Martin  · 技术社区  · 7 年前

    PostgreSQL的语法是 COMMENT 在约束上:

    COMMENT ON CONSTRAINT` constraint_name ON table_name IS 'text'`
    

    例子:

    COMMENT ON CONSTRAINT bar_col_cons ON bar IS 'Constrains column col';
    

    告诉我怎么做 定义 关于约束的注释。那我该怎么办呢 看见 已定义的注释?

    产量 \d+ 表中包含约束列表,但不显示注释。

    2 回复  |  直到 7 年前
        1
  •  3
  •   Uku Loskit    7 年前

    \dd <constraint_name> 应显示注释,但不按表名筛选。

        2
  •  3
  •   klin    7 年前

    您可以使用系统目录 pg_constraint pg_description 查询约束的注释。

    带有约束注释的示例表:

    create table test(
        id int unique,
        str text check(str <> '')
    );
    
    comment on constraint test_id_key on test is 'my comment on test_id_key';
    comment on constraint test_str_check on test is 'my comment on test_str_check';
    

    选择表约束的所有注释 test 以下内容:

    select c.relname, t.conname, d.description
    from pg_class c
    join pg_constraint t on c.oid = t.conrelid
    join pg_description d on t.oid = d.objoid and t.tableoid = d.classoid
    where c.relname = 'test'
    
     relname |    conname     |         description          
    ---------+----------------+------------------------------
     test    | test_str_check | my comment on test_str_check
     test    | test_id_key    | my comment on test_id_key
    (2 rows)