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 > SELECT Primary keys

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-27-04, 08:46
elline elline is offline
Registered User
 
Join Date: Feb 2004
Posts: 1
SELECT Primary keys

Hello,

I have a database from some tables. Each of them has a primary key.
I have to write a query to get tables, which have primary keys as an answer.
Could anyone give some ideas how to do this?

Thanks for help,
Elline
Reply With Quote
  #2 (permalink)  
Old 02-27-04, 09:10
disaster disaster is offline
Registered User
 
Join Date: Feb 2004
Posts: 24
Re: SELECT Primary keys

Hi,

u have to write a query with a join over sysibm.systables sysibm.syscolumns and maybe sysibm.syskeys (for the index names).

hope this will help !

regards

marc gaines
Quote:
Originally posted by elline
Hello,

I have a database from some tables. Each of them has a primary key.
I have to write a query to get tables, which have primary keys as an answer.
Could anyone give some ideas how to do this?

Thanks for help,
Elline
Reply With Quote
  #3 (permalink)  
Old 02-27-04, 10:21
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Re: SELECT Primary keys

If you are on Unix, you can use the UNIQUERULE Column of the SYSCAT.INDEXES view ... UNIQUERULE is 'P' for primary keys ...

Please read 'Must Read Before Posting' thread ....

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #4 (permalink)  
Old 02-27-04, 10:27
disaster disaster is offline
Registered User
 
Join Date: Feb 2004
Posts: 24
Re: SELECT Primary keys

a little example (db2 for z/os):

select distinct t.name
from sysibm.systables t
inner join sysibm.syscolumns c
on t.name = c.tbname
where t.creator = c.tbcreator
and c.keyseq > 0

just an idea !

regards

marc gaines

Quote:
Originally posted by elline
Hello,

I have a database from some tables. Each of them has a primary key.
I have to write a query to get tables, which have primary keys as an answer.
Could anyone give some ideas how to do this?

Thanks for help,
Elline
Reply With Quote
  #5 (permalink)  
Old 02-29-04, 22:22
kraman_usa kraman_usa is offline
Registered User
 
Join Date: Feb 2004
Posts: 7
Re: SELECT Primary keys

Hi Elline

You have to join SYSIBM.SYSINDEXES and SYSIBM.SYSKEYS. For a given table and table creator (qualifier), join IXNAME and IXCREATOR columns between these two catalogs.

You will get the keys. Have the query sort by COLSEQ column of SYSKEYS.

Hope this helps

=============================================
Quote:
Originally posted by elline
Hello,

I have a database from some tables. Each of them has a primary key.
I have to write a query to get tables, which have primary keys as an answer.
Could anyone give some ideas how to do this?

Thanks for help,
Elline
Reply With Quote
  #6 (permalink)  
Old 02-29-04, 23:21
hanyheggy hanyheggy is offline
Registered User
 
Join Date: Jan 2004
Location: Egypt
Posts: 61
Re: SELECT Primary keys

Quote:
Originally posted by elline
Hello,

I have a database from some tables. Each of them has a primary key.
I have to write a query to get tables, which have primary keys as an answer.
Could anyone give some ideas how to do this?

Thanks for help,
Elline
one workaround idea that may help is to change DB2 diaglevel to 4, then use db2cc to list primary key of any table, then check db2diag.log and see the SQL used to list the primary keys, try to apply the same SQL on all tables using batch file

this donot need DB2 knowledge

donot forget to return yr diaglevel to normal ( 2 or 3 )
__________________
hany heggy,
IBM certified Professional, AIX system support
www.melodyhits.tv

Last edited by hanyheggy; 02-29-04 at 23:27.
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