首頁 > 資料庫 > mysql教程 > 如何檢索與 PostgreSQL 中的索引關聯的欄位?

如何檢索與 PostgreSQL 中的索引關聯的欄位?

Patricia Arquette
發布: 2024-12-31 06:48:09
原創
877 人瀏覽過

How to Retrieve Columns Associated with Indexes in PostgreSQL?

在 PostgreSQL 中擷取與索引相關的欄位

在 PostgreSQL 中,擷取與索引對應的資料列與 MySQL 的 SHOW INDEXES 指令不同。

獲取PostgreSQL 中所需的信息,請使用以下命令查詢:

select
    t.relname as table_name,
    i.relname as index_name,
    a.attname as column_name
from
    pg_class t,
    pg_class i,
    pg_index ix,
    pg_attribute a
where
    t.oid = ix.indrelid
    and i.oid = ix.indexrelid
    and a.attrelid = t.oid
    and a.attnum = ANY(ix.indkey)
    and t.relkind = 'r'
    and t.relname like 'test%';
登入後複製

此查詢檢索表和索引名稱以及關聯的列名稱。為了獲得更多見解,可以修改查詢以聚合列名稱:

select
    t.relname as table_name,
    i.relname as index_name,
    array_to_string(array_agg(a.attname), ', ') as column_names
from
    pg_class t,
    pg_class i,
    pg_index ix,
    pg_attribute a
where
    t.oid = ix.indrelid
    and i.oid = ix.indexrelid
    and a.attrelid = t.oid
    and a.attnum = ANY(ix.indkey)
    and t.relkind = 'r'
    and t.relname like 'test%'
group by
    t.relname,
    i.relname
order by
    t.relname,
    i.relname;
登入後複製

以上是如何檢索與 PostgreSQL 中的索引關聯的欄位?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板