简体   繁体   中英

Query to get parent table using table child table

I'm looking for query to get the parent table details(name) using child table name and child table schema.

I browsed over the web but didn't get any query.

CREATE TABLE smt.items (
    item_code INTEGER PRIMARY KEY DEFAULT '1001'
    ,item_name CHARACTER(35) NOT NULL
    ,purchase_unit CHARACTER(10)
    ,sale_unit CHARACTER(10)
    ,purchase_price NUMERIC(10, 2)
    ,sale_price NUMERIC(10, 2)
    );

CREATE TABLE smt.sub_items (
    sub_item_id INTEGER PRIMARY KEY
    ,sub_items_name CHARACTER(35) NOT NULL
    ) inherits (smt.items);

Something like this:

select bt.relname as table_name, bns.nspname as table_schema 
from pg_class ct 
  join pg_namespace cns on ct.relnamespace = cns.oid 
  join pg_inherits i on i.inhrelid = ct.oid 
  join pg_class bt on i.inhparent = bt.oid 
  join pg_namespace bns on bt.relnamespace = bns.oid 
where bt.relkind <> 'p'
  and cns.nspname = 'public'
  and ct.relname = 'child_table_name';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM