list all table primary key

This is a discussion on list all table primary key within the db2-udb forums in Other Databases category; Hi All, Is the any sql to list all the tables primary key name? Thanks....

Go Back   Database Forum > Other Databases > db2-udb

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-24-2007, 05:26 AM
Default list all table primary key

Hi All,

Is the any sql to list all the tables primary key name?
Thanks.
Reply With Quote
  #2  
Old 09-24-2007, 09:18 AM
Default Re: list all table primary key

calvinscy-at-hotmail.com wrote:

> Hi All,
>
> Is the any sql to list all the tables primary key name?


You can query SYSCAT.TABCONST:

SELECT *
FROM syscat.tabconst
WHERE type = 'P'

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Reply With Quote
  #3  
Old 09-25-2007, 03:16 AM
Default Re: list all table primary key

Thanks Knut.

Now, we got the primary key for each table,
So is it possible to extract the actual column to this primary key?

BTW, Is there any doc reference to SYSCAT.TABCONST...?
Or any other SYSCAT or SYSIBM references?
Thanks.
Reply With Quote
  #4  
Old 09-25-2007, 03:27 AM
Default Re: list all table primary key

Knut,

Can I do something like this...?

SELECT * FROM SYSCAT.COLUMNS where KeySeq = 1;

Is it same with your SELECT * FROM syscat.tabconst WHERE type = 'P'?
But with this sql, I can extract the actual column name for primary key.
Thanks.


Reply With Quote
  #5  
Old 09-25-2007, 05:51 AM
Default Re: list all table primary key

calvinscy-at-hotmail.com wrote:

> Knut,
>
> Can I do something like this...?
>
> SELECT * FROM SYSCAT.COLUMNS where KeySeq = 1;
>
> Is it same with your SELECT * FROM syscat.tabconst WHERE type = 'P'?


No, it is not. The KEYSEQ column tells you the position of the column in
the PK of the table. If there is more than one column in the PK, you would
have to check for KEYSEQ IS NOT NULL. Besides, querying SYSCAT.TABCONST
returns the constraint while querying SYSCAT.COLUMNS returns information
about the column in the constraint - that's a different level of
information.

If you want to know the columns of a unique constraint (PKs are the same as
UNIQUE constraints), you can use the catalog view SYSCAT.KEYCOLUSE.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Reply With Quote
  #6  
Old 09-25-2007, 11:45 AM
Default Re: list all table primary key

SELECT COLUMN_NAME,
KEY_SEQ,
PK_NAME
FROM SYSIBM."SQLPRIMARYKEYS"
WHERE "TABLE_SCHEM" = 'Schema Name'
AND "TABLE_NAME" = 'Table Name'

Reply With Quote
  #7  
Old 09-25-2007, 05:56 PM
Default Re: list all table primary key

Select on syscat.indexes as well, i.e.

db2 select indname,colnames from syscat.indexes where indname in
(select constname from syscat.tabconst)

calvinscy-at-hotmail.com wrote:
> Thanks Knut.
>
> Now, we got the primary key for each table,
> So is it possible to extract the actual column to this primary key?
>
> BTW, Is there any doc reference to SYSCAT.TABCONST...?
> Or any other SYSCAT or SYSIBM references?
> Thanks.

Reply With Quote
  #8  
Old 09-25-2007, 09:52 PM
Default Re: list all table primary key

Thanks Knut.

Learn a lot about the syscat,
and found solution to my issue
while I developing application accessing to db2.

Thanks again.
Reply With Quote
  #9  
Old 09-25-2007, 09:58 PM
Default Re: list all table primary key

Thanks Blair Kenneth Adamache, db2venky.

Really open my eye to Syscat stuff ...
After reading yours solution on this forum,
I read number of references about syscat.

Wow! ....

I think this is the only expression I can think of....
Thanks again.
Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 05:27 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Integrated by bbpixel2009 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.