stackoverflow.com: How to find primary key on a table in DB2?

stackoverflow.com: How to find primary key on a table in DB2?

DB2 mainframe:

SELECT
     RTRIM(name)    AS index_name
    ,RTRIM(creator) AS index_schema
    ,uniquerule
    ,clustering
FROM sysibm.sysindexes
WHERE tbname     = @table
  AND tbcreator  = @schema
  AND clustering = 'Y'

Then, to see the actual columns in that index, you perform this query:

SELECT colname AS name
FROM sysibm.sysindexes a
JOIN sysibm.syskeys b
    ON a.name       = b.ixname 
    AND a.tbcreator = b.ixcreator 
WHERE a.name        = @index_name
    AND a.tbcreator = @index_schema
ORDER BY COLSEQ
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s