From: Sandro Dentella <sandro.dentella@tin.it>
To: pgsql-interfaces@postgresql.org
Subject: How to determine PRIMARY KEYS
Date: Mon, 11 Jun 2001 18:46:17 +0200
Message-ID: <20010611184617.A6328@bluff.diade.it> (raw)
Hi all,
I'm the author of sdsq/tksql an interface to postgreSQL I released under
GPL some weeks ago.
Tksql tries as much as possible to use PRIMARY KEYS instead of oids in any
operation so that logs are much more readable and so that I can use those
logs to do a replica of the database to keep two databases in sync (in a
very simple situation thought...)
Now, I'd like suggestion and a comment on the algorithm I'm using to
find the primary key.
I really think that this is more than needed, becouse if you have a table
w/ a primary key and unique index NOT NULL on an other attribute both these
attributes would be choosen not just the minimum needed (the primary key
OR an attribute NOT NULL w/ unique index).
Here is what I do:
1. find all unique indexes on table ($tbl)
SELECT i.indexrelid FROM pg_class c, pg_index i \
WHERE c.relname = '$tbl' and c.oid = i.indrelid \
and i.indisunique = 't'
2. select in pg_attribute the attributes of these indexes...
3. loop over these attrs ($at) and select those that are NOT NULL.
SELECT attname,attnotnull from pg_attribute \
where attrelid = (SELECT oid from pg_class \
where relname = '$tbl') and attname = '$at' \
and attnotnull = 't'
May I as an alternative just relay on the pattern *_pkey and look at the
attribute of relation *_pkey to get the PRIMARY KEY?
Is there a document that explains system tables?
Thanks in advance
sandro
*:-)
PS. For those who whant to look at tksql or sdsql (the tcl/tk package):
http://pcco2.mi.infn.it/sd/soft. If you encounter problems installing or
running it I'd really appreciate to be informed (and/or if you like
it...;-) . Thanks
Note: sorry if this message shows up 2 times. I had problems w/ config of
sendmail
--
Sandro Dentella *:-)
e-mail: sandro@ermit.it sandro.dentella@mi.infn.it
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-interfaces@postgresql.org
Cc: sandro.dentella@tin.it
Subject: Re: How to determine PRIMARY KEYS
In-Reply-To: <20010611184617.A6328@bluff.diade.it>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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