pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
find foreign key name
6+ messages / 4 participants
[nested] [flat]

* find foreign key name
@ 2007-12-18 09:20  Tore Halset <halset@pvv.ntnu.no>
  0 siblings, 1 reply; 6+ messages in thread

From: Tore Halset @ 2007-12-18 09:20 UTC (permalink / raw)
  To: pgsql-jdbc

Hello.

I want to programatically drop a foreign key constraint via jdbc. How  
can I find the foreign key name for a PostgreSQL foreign key via jdbc?  
I know all about the related tables and columns, but not the name of  
the constraint.

Regards,
  - Tore.



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: find foreign key name
@ 2007-12-18 09:36  Heikki Linnakangas <heikki@enterprisedb.com>
  parent: Tore Halset <halset@pvv.ntnu.no>
  0 siblings, 1 reply; 6+ messages in thread

From: Heikki Linnakangas @ 2007-12-18 09:36 UTC (permalink / raw)
  To: Tore Halset <halset@pvv.ntnu.no>; +Cc: pgsql-jdbc

Tore Halset wrote:
> I want to programatically drop a foreign key constraint via jdbc. How 
> can I find the foreign key name for a PostgreSQL foreign key via jdbc? I 
> know all about the related tables and columns, but not the name of the 
> constraint.

You can query the pg_constraint catalog table:

http://www.postgresql.org/docs/8.2/interactive/catalog-pg-constraint.html

There seems to be some example queries in the user comments that you 
might be interested.

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: find foreign key name
@ 2007-12-18 14:43  Tore Halset <halset@pvv.ntnu.no>
  parent: Heikki Linnakangas <heikki@enterprisedb.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Tore Halset @ 2007-12-18 14:43 UTC (permalink / raw)
  To: pgsql-jdbc

On Dec 18, 2007, at 10:36 , Heikki Linnakangas wrote:

> Tore Halset wrote:
>> I want to programatically drop a foreign key constraint via jdbc.  
>> How can I find the foreign key name for a PostgreSQL foreign key  
>> via jdbc? I know all about the related tables and columns, but not  
>> the name of the constraint.
>
> You can query the pg_constraint catalog table:
>
> http://www.postgresql.org/docs/8.2/interactive/catalog-pg-constraint.html
>
> There seems to be some example queries in the user comments that you  
> might be interested.

Thanks. I was hoping for something easy in  
DatabaseMetaData#getExportedKeys.. :) But looking at pg_constraint  
should work fine.

Regards,
  - Tore.




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: find foreign key name
@ 2007-12-18 16:05  Kris Jurka <books@ejurka.com>
  parent: Tore Halset <halset@pvv.ntnu.no>
  1 sibling, 1 reply; 6+ messages in thread

From: Kris Jurka @ 2007-12-18 16:05 UTC (permalink / raw)
  To: Tore Halset <halset@pvv.ntnu.no>; +Cc: pgsql-jdbc



On Tue, 18 Dec 2007, Tore Halset wrote:

> Thanks. I was hoping for something easy in DatabaseMetaData#getExportedKeys.. 
>

What about the FK_NAME column in that result, doesn't that work?

Kris Jurka



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: find foreign key name
@ 2007-12-19 02:51  danap@ttc-cmc.net
  parent: Tore Halset <halset@pvv.ntnu.no>
  1 sibling, 0 replies; 6+ messages in thread

From: danap@ttc-cmc.net @ 2007-12-19 02:51 UTC (permalink / raw)
  To: pgsql-jdbc; +Cc: Tore Halset <halset@pvv.ntnu.no>

Hi Tore,
As Kris indicated perhaps FK_NAME should do it. I have
used something a little different Im not sure why unless
I looked a little closer, but this what I have

MyJSQLView Project
http://sourceforge.net/projects/myjsqlview/
Code | Browse | --- src/Panels/net/danap/myjsqlview
Class TableTabPanel_PostgreSQL.java Class Method
getColumnNames() ....

// Make a final check to see if there are any keys columns
// columns in the table. If not then try foreign keys.

if (primaryKeys.isEmpty())
{
  rs = dbMetaData.getImportedKeys(tableMetaData.getCatalogName(1),				    
     tableMetaData.getSchemaName(1),
				  tableMetaData.getTableName(1));

  while (rs.next())
  {
     if (columnNamesHashMap.containsValue(rs.getString("PKCOLUMN_NAME")) &&
	 !primaryKeys.contains(rs.getString("PKCOLUMN_NAME")))
     {
	primaryKeys.add(rs.getString("PKCOLUMN_NAME"));
	//System.out.println(rs.getString("PKCOLUMN_NAME"));
     }
   }
}

I was reluctant to access system catalogs directly in order to
derive the info. The above or FK_NAME would be a better way
to isolate from future changes in system catlogs.

dana

> On Dec 18, 2007, at 10:36 , Heikki Linnakangas wrote:
>
>> Tore Halset wrote:
>>> I want to programatically drop a foreign key constraint via jdbc.
>>> How can I find the foreign key name for a PostgreSQL foreign key
>>> via jdbc? I know all about the related tables and columns, but not
>>> the name of the constraint.
>>
>> You can query the pg_constraint catalog table:
>>
>> http://www.postgresql.org/docs/8.2/interactive/catalog-pg-constraint.html
>>
>> There seems to be some example queries in the user comments that you
>> might be interested.
>
> Thanks. I was hoping for something easy in
> DatabaseMetaData#getExportedKeys.. :) But looking at pg_constraint
> should work fine.
>
> Regards,
>   - Tore.
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: find foreign key name
@ 2007-12-19 15:17  Tore Halset <halset@pvv.ntnu.no>
  parent: Kris Jurka <books@ejurka.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Tore Halset @ 2007-12-19 15:17 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: pgsql-jdbc

On Dec 18, 2007, at 17:05 , Kris Jurka wrote:

> On Tue, 18 Dec 2007, Tore Halset wrote:
>
>> Thanks. I was hoping for something easy in  
>> DatabaseMetaData#getExportedKeys..
>
> What about the FK_NAME column in that result, doesn't that work?

Thanks a lot. It works perfect. Should have seen that myself...

Regards,
  - Tore.



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2007-12-19 15:17 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-12-18 09:20 find foreign key name Tore Halset <halset@pvv.ntnu.no>
2007-12-18 09:36 ` Heikki Linnakangas <heikki@enterprisedb.com>
2007-12-18 14:43   ` Tore Halset <halset@pvv.ntnu.no>
2007-12-18 16:05     ` Kris Jurka <books@ejurka.com>
2007-12-19 15:17       ` Tore Halset <halset@pvv.ntnu.no>
2007-12-19 02:51     ` danap@ttc-cmc.net

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox