ホームページ >データベース >mysql チュートリアル >PostgreSQLでインデックスに関連付けられた列を取得するにはどうすればよいですか?

PostgreSQLでインデックスに関連付けられた列を取得するにはどうすればよいですか?

Patricia Arquette
Patricia Arquetteオリジナル
2024-12-31 06:48:09870ブラウズ

How to Retrieve Columns Associated with Indexes in PostgreSQL?

PostgreSQL でのインデックスに関連付けられた列の取得

PostgreSQL でのインデックスに対応する列の取得は、MySQL の SHOW INDEXES コマンドとは異なります。

取得するにはPostgreSQL で必要な情報を取得するには、以下を利用します。 query:

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。