Check if table inherits from other table in PostgreSQL

后端 未结 3 1772
南方客
南方客 2021-01-17 17:05

In PostgreSQL for these tables

CREATE TABLE cities (
    name            text,
    population      float,
    altitude        int     -- in feet
);

CREATE T         


        
3条回答
  •  Happy的楠姐
    2021-01-17 17:34

    The following statement will retrieve the tables that cities inherits from. If the table does not inherit from another table, the result will be empty:

    select bt.relname as table_name, bns.nspname as table_schema 
    from pg_class ct 
        join pg_namespace cns on ct.relnamespace = cns.oid and cns.nspname = 'public' 
        join pg_inherits i on i.inhrelid = ct.oid and ct.relname = 'cities ' 
        join pg_class bt on i.inhparent = bt.oid 
        join pg_namespace bns on bt.relnamespace = bns.oid
    

提交回复
热议问题