0

I am unable to fetch primary key in DB2. I used following code but It is not working for me.

SELECT TBCREATOR, TBNAME, NAME, KEYSEQ FROM SYSIBM.SYSCOLUMNS WHERE TBCREATOR = 'DSN8710' AND TBNAME = 'EMPLOYEE' AND KEYSEQ > 0 ORDER BY KEYSEQ;

And what is the means of TBCREATOR in this code and how to modified TBCREATOR value according to my case?

rawat0157
  • 13
  • 1
  • 1
  • 10

1 Answers1

1

I'll answer your last question first. creator is sometimes referred to as schema. If you're familiar with Oracle, this is roughly analogous to a database user (though not exactly).

As far as getting the "primary key" information, you probably want to know which index is the "clustering" index (which is what usually, but not always, determines the physical ordering of the rows on disk).

How you find the clustering index depends on the platform you're running:

Mainframe (z/OS):

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

Linux/Unix/Windows:

SELECT
     RTRIM(indname)   AS index_name
    ,RTRIM(indschema) AS index_schema
    ,uniquerule
    ,indextype
FROM syscat.indexes
WHERE tabname     = @table
  AND tabschema = @schema
  AND indextype = 'CLUS'

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

SELECT colnames as name
FROM sysibm.sysindexes
WHERE name        = @index_name
    AND tbcreator = @index_schema
ORDER BY NAME

LUW returns the list of columns as one string, delimited by +, which is kind of weird...

Community
  • 1
  • 1
bhamby
  • 15,112
  • 1
  • 45
  • 66
  • I'm afraid you are wrong about the creator and schema. `CREATOR` is the authorization ID of the user who ran the DDL statement, nothing more. It will be equal to the schema only in one case: when the schema name is not explicitly specified and DB2 derives it from the same source -- user's authorization ID. `create table mytab`, `create table foo.mytab`, and `set schema bar; create table mytab` will all have the same creator, but different schema names. – mustaccio Apr 20 '15 at 17:27
  • @mustaccio: I think in the context of the catalog views, however, the view columns that include `creator` actually refer to the object's `schema` (at least on z/OS... it appears that in LUW's `SYSCAT` tables, they've renamed it to have `SCHEMA`). For example, the description for `CREATOR` in the [`SYSTABLES`](http://www-01.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_sysibmsystablestable.dita?cp=SSEPEK_10.0.0%2F10-0-111&lang=en) view says "The **schema** of the table, view, or alias." Additionally, there is a separated `CREATEDBY` column on SYSTABLES. – bhamby Apr 20 '15 at 19:08
  • @mustaccio However, I do agree that if you don't provide the schema when creating the object, it is implicitly the [`CURRENT SCHEMA`](http://www-01.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_currentschema.dita?lang=en) special register, which is your auth id by default. – bhamby Apr 20 '15 at 19:10