1

在 Postgres 中对表执行 a\d+时,它会列出表模式以及索引,以及将其作为 FK 引用的其他表。例子:

    Table "public.foo_table"
   Column   | Type |   Modifiers   | Storage  | Description 
------------+------+---------------+----------+-------------
 id         | text |               | extended | 
 foo        | text |               | extended | 
 bar        | text |               | extended | 
Indexes:
    "foo_table_id_idx" btree (id)
    "foo_table_foobar_idx" btree (foo,bar)
Foreign-key constraints:
    "foo_table_bar_fk" FOREIGN KEY (bar) REFERENCES public.bar_table(id)
Referenced by:
    TABLE "public.bar_table" CONSTRAINT "bar_table_foo_fk" FOREIGN KEY (foo) REFERENCES public.foo_table(foo)
Has OIDs: no

您可以做一些事情$dbh->statistics_info(...)来检索索引信息。是否有类似的东西来检索 FK 信息(引用和引用)?


看来我的下一个选择是发出->do()命令或查询系统表。

4

2 回答 2

1

到目前为止我发现了什么:

$dbh->foreign_key_info( pk_cat, pk_schema, pk_tbl
                      , fk_cat, fk_schema, fk_tbl      );

# FK References
$dbh->foreign_key_info( undef , undef    , undef   
                      , undef , undef    , $table_name );

# FK Referenced By
$dbh->foreign_key_info( undef , undef    , $table_name 
                      , undef , undef    , undef       );

## Putting the schema/catalog info only ensures you are hitting the intended 
##    table. If you have dupicate tables, or your table is not in the public 
##    schema it's probably a good idea to include the schema.
## Catalog is generally unnecessary for Postgres
于 2012-04-16T13:50:31.663 回答
0

如果您使用该-E选项运行 psql,它将显示它为响应\d(和其他)元数据请求而运行的所有查询。从中复制/粘贴以获得您想要的查询非常容易。

于 2012-04-16T14:14:10.260 回答