List all primary keys (PKs) and their columns in Teradata database

Query below lists all primary keys constraints (PK) in the database with their columns (one row per column).

See also: list of all primary keys (one row per PK).

Query

select DatabaseName,
       TableName,
       IndexName,
       ColumnName,
       ColumnPosition
from DBC.IndicesV
where IndexType = 'K'
      and DatabaseName NOT IN ('All', 'Crashdumps', 'DBC', 'dbcmngr', 
    'Default', 'External_AP', 'EXTUSER', 'LockLogShredder', 'PUBLIC',
    'Sys_Calendar', 'SysAdmin', 'SYSBAR', 'SYSJDBC', 'SYSLIB', 
    'SystemFe', 'SYSUDTLIB', 'SYSUIF', 'TD_SERVER_DB', 'TDStats',
    'TD_SYSGPL', 'TD_SYSXML', 'TDMaps', 'TDPUSER', 'TDQCD',
    'tdwm', 'SQLJ', 'TD_SYSFNLIB', 'SYSSPATIAL')
order by DatabaseName,
         TableName,
         ColumnPosition;

Columns

  • DatabaseName - database name
  • TableName - table on which the primary key is built.
  • IndexName - name of the primary key
  • ColumnName - primary key column name
  • ColumnPosition - position of the column within an primary key.

Rows

  • One row represents one primary key column
  • Scope of rows: columns of all PK constraints in a database
  • Ordered by database name, table name, column position

Sample results

You could also get this

Get this interactive HTML data dictionary in minutes with Dataedo.

See live HTML data dictionary sample

Try for free