List all primary keys (PKs) in Snowflake

See also: tables with their primary keys.

Primary Key Columns

Snowflake doesn't allow us to simple list all PKs columns by one query. To see specific table primary key columns you can use following command

Query

describe table <table_name>;

Sample result

Primary Keys Constraints

Query below lists all primary keys constraints (PK) in the database.

Query

select table_schema,
       constraint_name,
       table_name,
       constraint_type
from information_schema.table_constraints
where constraint_type = 'PRIMARY KEY'
order by table_schema, 
         table_name;

Columns

  • table_schema - schema name constraint belongs to
  • constraint_name - name of the PK's constraint
  • table_name - name of the table
  • constraint_type - type of constraint (redundant as it should be exactly PRIMARY KEY)

Rows

  • One row represents one primary key (table) in a database
  • Scope of rows: all PK constraints in a database
  • Ordered by table schema, table name

Sample results