Wednesday, August 30, 2017

PostgreSQL: tables without PK

Source: http://petereisentraut.blogspot.fr/2009/07/how-to-find-all-tables-without-primary.html
-- Tables of db2 without PK
SELECT table_catalog, table_schema, table_name
    FROM information_schema.tables
    WHERE (table_catalog, table_schema, table_name) NOT IN
          (SELECT table_catalog, table_schema, table_name
               FROM information_schema.table_constraints
               WHERE constraint_type = 'PRIMARY KEY')
      AND table_schema NOT IN ('information_schema', 'pg_catalog') and table_schema ~ '_db$' and table_schema not in ('prov_db', 'queue_db', 'ota_db', 'sla_db');