Tôi đang sử dụng PostgreSQL 9.1. Tôi có tên cột của một bảng. Có thể tìm thấy (các) bảng có / có cột này không? Nếu vậy, làm thế nào?
Tôi đang sử dụng PostgreSQL 9.1. Tôi có tên cột của một bảng. Có thể tìm thấy (các) bảng có / có cột này không? Nếu vậy, làm thế nào?
Câu trả lời:
bạn có thể truy vấn danh mục hệ thống :
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'
    Bạn cũng có thể làm
 select table_name from information_schema.columns where column_name = 'your_column_name'
    Tôi đã sử dụng truy vấn @Roman Pekar làm cơ sở và thêm tên lược đồ (có liên quan trong trường hợp của tôi)
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'
    Hỗ trợ ký tự đại diện Tìm lược đồ bảng và tên bảng có chứa chuỗi bạn muốn tìm.
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;
    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;