dbTalk Databases Forums  

Finding Foreign/Primary Keys

comp.databases comp.databases


Discuss Finding Foreign/Primary Keys in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jason D.
 
Posts: n/a

Default Finding Foreign/Primary Keys - 05-08-2007 , 01:28 PM






Is there a generic way, using ODBC/OLEDB/ADO.NET or another
programmable database technology to determine foreign and primary key
constraints in a table?

I've tried using the ODBC API's SQLPrimaryKeys and SQLForeignKeys but
they aren't always supported. I've also tried SQLSpecialColumns and
SQLStatistics for limited success with Primary Keys, but no idea for
Foreign Keys.

Thanks!


Reply With Quote
  #2  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Finding Foreign/Primary Keys - 05-09-2007 , 02:47 AM






On 08.05.2007 19:28 Jason D. wrote:
Quote:
Is there a generic way, using ODBC/OLEDB/ADO.NET or another
programmable database technology to determine foreign and primary key
constraints in a table?
The JDBC drivers do reveal this information as well (that would fall in
the category "another programmable database technology")

Quote:
I've tried using the ODBC API's SQLPrimaryKeys and SQLForeignKeys but
they aren't always supported.
With JDBC this is up to the driver to support the calls or not (they are
not required, as far as I know). I could image this is similar with the
ODBC specs (I haven't used any of the mentioned interfaces)

Look at the DatabaseMetaData class in the JDK documentation.

Thomas


Reply With Quote
  #3  
Old   
Jason D.
 
Posts: n/a

Default Re: Finding Foreign/Primary Keys - 05-09-2007 , 08:03 AM



On May 9, 3:47 am, Thomas Kellerer <JUAXQOSZF... (AT) spammotel (DOT) com> wrote:
Quote:
On 08.05.2007 19:28 Jason D. wrote:

Is there a generic way, using ODBC/OLEDB/ADO.NET or another
programmable database technology to determine foreign and primary key
constraints in a table?

The JDBC drivers do reveal this information as well (that would fall in
the category "another programmable database technology")

I've tried using the ODBC API's SQLPrimaryKeys and SQLForeignKeys but
they aren't always supported.

With JDBC this is up to the driver to support the calls or not (they are
not required, as far as I know). I could image this is similar with the
ODBC specs (I haven't used any of the mentioned interfaces)

Look at the DatabaseMetaData class in the JDK documentation.

Thomas
Thanks for the advice! It's a pain trying to use the ODBC API for
it.....the calls are Level 2 and don't seem to be well supported. I'm
having good luck with OLEDB using the OleDbSchemaGuid Class in .NET;
though I might try doing it with .NET.

Cheers,
Jason



Reply With Quote
  #4  
Old   
David Cressey
 
Posts: n/a

Default Re: Finding Foreign/Primary Keys - 05-09-2007 , 08:51 AM




"Jason D." <Tronman (AT) gmail (DOT) com> wrote

Quote:
On May 9, 3:47 am, Thomas Kellerer <JUAXQOSZF... (AT) spammotel (DOT) com> wrote:
On 08.05.2007 19:28 Jason D. wrote:

Is there a generic way, using ODBC/OLEDB/ADO.NET or another
programmable database technology to determine foreign and primary key
constraints in a table?

The JDBC drivers do reveal this information as well (that would fall in
the category "another programmable database technology")

I've tried using the ODBC API's SQLPrimaryKeys and SQLForeignKeys but
they aren't always supported.

With JDBC this is up to the driver to support the calls or not (they are
not required, as far as I know). I could image this is similar with the
ODBC specs (I haven't used any of the mentioned interfaces)

Look at the DatabaseMetaData class in the JDK documentation.

Thomas

Thanks for the advice! It's a pain trying to use the ODBC API for
it.....the calls are Level 2 and don't seem to be well supported. I'm
having good luck with OLEDB using the OleDbSchemaGuid Class in .NET;
though I might try doing it with .NET.

Cheers,
Jason

I don't know the specific answers to your question, but in general.....


If the foreign key/primary key pair have been declared in a referential
integrity constraint, and if the constraint has been catalogued in the
metadata (system tables), and if the metadata conforms to a database
industry standard (like the SQL metadata standard) reasonably well,
then you should be able to compose your own query to detect the foreign
key/primary key pairs.

Alternatively, if the DBMS supports user defined domains, and if the
domains were defined somewhat intelligently, and if this is catalogued in
the metadata, it should be possible to pull a query that would group all
collections of more than one column that are based on a single domain. The
query should be pretty easy. This won't limit you to foreign key/primary
key pairs, but it should provide the information you need, mixed in with a
little possibly extraneous information.

I realize that this is less helpful than pointing you at a tool, but I hope
someone reading can make some use of it.






Reply With Quote
  #5  
Old   
Sualeh Fatehi
 
Posts: n/a

Default Re: Finding Foreign/Primary Keys - 05-10-2007 , 12:00 PM



Take a look at http://www.devx.com/Java/Article/32443 for a simple to
use, and open source, Java API to obtain all types of database
metadata.

Sualeh Fatehi.


Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.