SORU
4 ŞUBAT 2010, PERŞEMBE


PostgreSQL içinde dizin sütunları listesi

Bir dizin üzerinde PostgreSQL bu sütunlara almak istiyorum.

MySQL SHOW INDEXES FOR table Column_name sütun bakabilirsiniz.

mysql> show indexes from foos;

 ------- ------------ --------------------- -------------- ------------- ----------- ------------- ---------- -------- ------ ------------ --------- 
| Table | Non_unique | Key_name            | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
 ------- ------------ --------------------- -------------- ------------- ----------- ------------- ---------- -------- ------ ------------ --------- 
| foos  |          0 | PRIMARY             |            1 | id          | A         |       19710 |     NULL | NULL   |      | BTREE      |         | 
| foos  |          0 | index_foos_on_email |            1 | email       | A         |       19710 |     NULL | NULL   | YES  | BTREE      |         | 
| foos  |          1 | index_foos_on_name  |            1 | name        | A         |       19710 |     NULL | NULL   |      | BTREE      |         | 
 ------- ------------ --------------------- -------------- ------------- ----------- ------------- ---------- -------- ------ ------------ --------- 

Böyle bir şey PostgreSQL için var mı?

psql > \d istemi denedim (-E SQL göster seçeneği ile) ama aradığım bilgi görünmüyor.

Güncelleme:Cevapları eklendi herkese teşekkürler. cope360 tam olarak aradığım şeyi bana verdi, ama birkaç kişi çok kullanışlı bağlantılar ile çaldı. Gelecekte başvurmak için, pg_index (Milen A. Radev) belgelerine ve çok yararlı makale Extracting META information from PostgreSQL (Michał Niklas) bak.

CEVAP
6 ŞUBAT 2010, CUMARTESİ


Bazı test verileri oluşturmak...

create table test (a int, b int, c int, constraint pk_test primary key(a, b));
create table test2 (a int, b int, c int, constraint uk_test2 unique (b, c));
create table test3 (a int, b int, c int, constraint uk_test3b unique (b), constraint uk_test3c unique (c),constraint uk_test3ab unique (a, b));

Liste dizinler ve sütun dizini:

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%'
order by
    t.relname,
    i.relname;

 table_name | index_name | column_name
------------ ------------ -------------
 test       | pk_test    | a
 test       | pk_test    | b
 test2      | uk_test2   | b
 test2      | uk_test2   | c
 test3      | uk_test3ab | a
 test3      | uk_test3ab | b
 test3      | uk_test3b  | b
 test3      | uk_test3c  | c

Yukarı sütun adları Roll:

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;

 table_name | index_name | column_names
------------ ------------ --------------
 test       | pk_test    | a, b
 test2      | uk_test2   | b, c
 test3      | uk_test3ab | a, b
 test3      | uk_test3b  | b
 test3      | uk_test3c  | c

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • ghosti66

    ghosti66

    27 AĞUSTOS 2006
  • kremosakhaz

    kremosakhaz

    25 AĞUSTOS 2006
  • thetrollska

    thetrollska

    2 EKİM 2009