If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to get Primary keys of a table in DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-08-09, 02:56
swati malla swati malla is offline
Registered User
 
Join Date: Jul 2009
Posts: 26
How to get Primary keys of a table in DB2

Hello everyone,
I am trying to get the primary keys of a table in DB2

I tried with this query
"select t.TABSCHEMA,t.TABNAME, k.colseq, k.colname from syscat.keycoluse k, syscat.tabconst t where k.constname = t.constname and k.tabname = t.tabname and k.TABSCHEMA = t.TABSCHEMA and t.type='P'and t.TABSCHEMA NOT LIKE 'SYS%' AND t.TABLESCHEMA='HERNESS' AND t.TABLNAME='ACCOUNT'";

but it gives me back the table name,shcmeaname with the primary keys.

I need a query where i wanted to give the table name and schema name and I should be able to get the primary keys

I tried searching in google but did not find the correct query .
By the way i new to DB2.

So i am looking for a query where i can give the table name and schema name i should be able to get the primary keys of that table.
I would really appreciate if some one can help me out in this.
Thanks in advance
Regards,
swati
Reply With Quote
  #2 (permalink)  
Old 09-08-09, 09:32
db2udbgirl db2udbgirl is offline
Registered User
 
Join Date: Mar 2006
Location: Tuticorin, India
Posts: 100
Try this one
db2 -x "SELECT A.COLSEQ, A.COLNAME FROM SYSCAT.KEYCOLUSE A, SYSCAT.TABCONST B WHERE A.CONSTNAME=B.CONSTNAME AND B.TYPE='P' AND A.TABNAME='$TABNAME' AND A.TABSCHEMA='$SCHEMANAME' order by A.COLSEQ"
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On