Received: from druid.net (root@druid.net [198.96.119.226]) by hub.org (8.9.2/8.9.1) with ESMTP id XAA64836 for ; Fri, 19 Feb 1999 23:59:27 -0500 (EST) (envelope-from darcy@druid.net) Received: from localhost (1511 bytes) by druid.net via sendmail with P:stdio/R:bind_hosts/T:inet_zone_bind_smtp (sender: ) (ident using unix) id for ; Fri, 19 Feb 1999 23:59:26 -0500 (EST) (Smail-3.2.0.104 1998-Nov-20 #2 built 1999-Feb-16) Message-Id: Subject: Re: [SQL] SQL-Query 2 get primary key In-Reply-To: <36CD659A.438BE39B@atix.de> from Marc Grimme at "Feb 19, 1999 02:22:34 pm" To: grimme@atix.de (Marc Grimme) Date: Fri, 19 Feb 1999 23:07:22 -0500 (EST) From: "D'Arcy" "J.M." Cain X-Mailer: ELM [version 2.4ME+ PL50 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: darcy@druid.net Thus spake Marc Grimme > if I create a table like this: > CREATE TABLE test ( > id decimal(3) primary key, > name varchar(32)); > > how can I ask postgres which is the primary key from table test? SELECT pg_class.relname, pg_attribute.attname FROM pg_class, pg_attribute, pg_index WHERE pg_class.oid = pg_attribute.attrelid AND pg_class.oid = pg_index.indrelid AND pg_index.indkey[0] = pg_attribute.attnum AND pg_index.indisprimary = 't'; That lists all the primary keys in your database. Add a "WHERE pg_class = 'test'" clause to get the specific table. Note that this makes the assumption that only one field can be in the primary key (no complex primary keys) but I don't think there will ever be more than one the way we declare it now. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 424 2871 (DoD#0082) (eNTP) | what's for dinner.