Find tables without primary keys (PKs) in Redshift

Query below lists tables in a database without primary keys.

Query

select tab.table_schema,
       tab.table_name
from information_schema.tables tab
left join information_schema.table_constraints tco 
          on tab.table_schema = tco.table_schema
          and tab.table_name = tco.table_name 
          and tco.constraint_type = 'PRIMARY KEY'
where tab.table_type = 'BASE TABLE'
      and tab.table_schema not in ('pg_catalog', 'information_schema')
      and tco.constraint_name is null
order by table_schema,
         table_name;

Columns

  • table_schema - schema name
  • table_name - table name

Rows

  • One row represents one table in a database
  • Scope of rows: all tables without primary keys in a database
  • Ordered by schema, table name

Sample results

Below is a list of tables in Redshift database without primary keys. Is that a lot? Check out here.

0
There are no comments. Click here to write the first comment.