Estrazione Modello dati FISICO - Query Postgres

Query da utilizzare per ottenere un tracciato, esportabile in excel e ovviamente da impaginare, che descrive il modello dati fisico (tabelle e viste) di un DB.

Rif. ABA

SELECT a.table_schema AS schema,a.table_name,a.ordinal_position, a.column_name, a.is_nullable, a.data_type,a.character_maximum_length,b.description AS comment,
CASE WHEN a.data_type IS NOT NULL AND a.data_type IS NOT NULL THEN a.data_type||' ('||a.character_maximum_length||')'  ELSE a.data_type END AS final_data_type,
cos.constraint_type,tb.table_type
FROM INFORMATION_SCHEMA.COLUMNS a
LEFT JOIN (SELECT c.table_name,c.column_name,pgd.description
FROM pg_catalog.pg_statio_all_tables AS st
  INNER JOIN pg_catalog.pg_description pgd ON (pgd.objoid=st.relid)
  INNER JOIN information_schema.columns c ON (pgd.objsubid=c.ordinal_position
    AND  c.table_schema=st.schemaname AND c.table_name=st.relname)) b ON b.table_name||'.'||b.column_name=a.table_name||'.'||a.column_name
 LEFT JOIN (SELECT c.table_name,c.column_name, tc.constraint_type
FROM information_schema.table_constraints tc 
JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name) 
JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema
  AND tc.table_name = c.table_name AND ccu.column_name = c.column_name
WHERE constraint_type NOT IN ('CHECK')) cos ON cos.table_name||'.'||cos.column_name = a.table_name||'.'||a.column_name
LEFT JOIN (SELECT TABLE_NAME, table_type FROM INFORMATION_SCHEMA.TABLES)
 tb ON tb.table_name=a.table_name
WHERE a.table_schema ='cde_data' AND a.table_name NOT LIKE 'act%' 
ORDER BY a.table_name,a.ordinal_position
  • gwtipstricks/idxtipstricks/estrazionemodellodatifisico_querypostgres.txt
  • Ultima modifica: 2019/11/06 09:42
  • (modifica esterna)