Re: [SQL] database design SQL prob.

D'Arcy Cain <darcy@druid.net>

From: "D'Arcy" "J.M." Cain <darcy@druid.net>
To: stuart@ludwig.ucl.ac.uk (Stuart Rison)
Cc: Frederic.De.Leersnijder@pandora.be, pgsql-sql@postgreSQL.org
Date: 1999-07-24T11:49:38Z
Lists: pgsql-hackers
Thus spake Stuart Rison
> Currently, in postgreSQL, primary keys are created as a UNIQUE index on the
> field(s) that form the primary key.
> 
> This means that there is no difference between explicitely declaring a
> PRIMARY KEY in your table definition or using the CREATE UNIQUE INDEX
> command.

Not completely accurate.  Create some tables using both methods then
run the following query.

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';

This will give you a list of the primary keys if you declare them as
primary at creation time.  The ones created with just a unique index
won't be displayed.

While I am on the subject, anyone know how to enhance the above query
to display all the fields when a complex primary key is defined?  The
above assumes that all primary keys are one field per table.

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  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.