database - How can I find all the tables of db that contains primary key or particular column of a single table in Postgresql -


how can find all tables of db contains primary key or particular column of single table in postgresql database....means column of perticular table included in many tables either foreign key or non - foreign key...column can primary key or non - primary key....

use below query find particular column of single table:

select table_name information_schema.columns table_schema = 'public'   , column_name   = 'your_column_name' 

use below query find list of all tables of db contains primary key

 select  t.table_catalog,           t.table_schema,           t.table_name,           kcu.constraint_name,           kcu.column_name,           kcu.ordinal_position     information_schema.tables t           left join information_schema.table_constraints tc                   on tc.table_catalog = t.table_catalog                   , tc.table_schema = t.table_schema                   , tc.table_name = t.table_name                   , tc.constraint_type = 'primary key'           left join information_schema.key_column_usage kcu                   on kcu.table_catalog = tc.table_catalog                   , kcu.table_schema = tc.table_schema                   , kcu.table_name = tc.table_name                   , kcu.constraint_name = tc.constraint_name    t.table_schema not in ('pg_catalog', 'information_schema')  order t.table_catalog,           t.table_schema,           t.table_name,           kcu.constraint_name,           kcu.ordinal_position;  

use below query find list of table having foreign key in table

 select  t.table_name fk_table, tc.constraint_name,tc.constraint_type,ccu.table_name pk_table    information_schema.tables t           inner join information_schema.table_constraints tc                   on tc.table_catalog = t.table_catalog                   , tc.table_schema = t.table_schema                   , tc.table_name = t.table_name            inner join information_schema.constraint_column_usage ccu                   on ccu.constraint_name = tc.constraint_name            /* remove comments check based on constraint'name or 'primary_key' name */                  --and ccu.constraint_name = 'constraint_name'                  --and  , ccu.column_name = 'column_name'   t.table_schema not in ('pg_catalog', 'information_schema')  

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -