Ik gebruik PostgreSQL 9.1. Ik heb de kolomnaam van een tabel. Is het mogelijk om de tabel(len) te vinden die deze kolom heeft/hebben? Zo ja, hoe?
Antwoord 1, autoriteit 100%
U kunt ook doen
select table_name from information_schema.columns where column_name = 'your_column_name'
Antwoord 2, autoriteit 44%
u kunt systeemcatalogi:
opvragen
select c.relname
from pg_class as c
inner join pg_attribute as a on a.attrelid = c.oid
where a.attname = <column name> and c.relkind = 'r'
Antwoord 3, autoriteit 5%
Ik heb de query van @Roman Pekar als basis gebruikt en een schemanaam toegevoegd (relevant in mijn geval)
select n.nspname as schema ,c.relname
from pg_class as c
inner join pg_attribute as a on a.attrelid = c.oid
inner join pg_namespace as n on c.relnamespace = n.oid
where a.attname = 'id_number' and c.relkind = 'r'
Antwoord 4, autoriteit 2%
Gewoon:
$ psql mydatabase -c '\d *' | grep -B10 'mycolname'
Vergroot -B offset om de tabelnaam te krijgen, indien nodig
Antwoord 5
Wildcard-ondersteuning
Zoek het tabelschema en de tabelnaam die de tekenreeks bevat die u wilt vinden.
select t.table_schema,
t.table_name
from information_schema.tables t
inner join information_schema.columns c on c.table_name = t.table_name
and c.table_schema = t.table_schema
where c.column_name like '%STRING%'
and t.table_schema not in ('information_schema', 'pg_catalog')
and t.table_type = 'BASE TABLE'
order by t.table_schema;
Antwoord 6
select t.table_schema,
t.table_name
from information_schema.tables t
inner join information_schema.columns c on c.table_name = t.table_name
and c.table_schema = t.table_schema
where c.column_name = 'name_colum'
and t.table_schema not in ('information_schema', 'pg_catalog')
and t.table_type = 'BASE TABLE'
order by t.table_schema;