Re: Join Table

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: T E Schmitz <mailreg(at)numerixtechnology(dot)de>
Cc: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Join Table
Date: 2004-11-01 17:03:24
Message-ID: 20041101170324.GA18943@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, Nov 01, 2004 at 04:34:32PM +0000, T E Schmitz wrote:
>
> I have created the following join table: the two FKs are the PK of the
> table. Typically, I will need to select rows for a given ITEM_FK.
>
> Question: is it necessary/advisable to create an index for the ITEM_FK
> column? Or is this redundantbecause this column is already one of the PK
> columns?

Here's an excerpt from the documentation for CREATE TABLE:

"PostgreSQL automatically creates an index for each unique constraint
and primary key constraint to enforce the uniqueness. Thus, it is
not necessary to create an explicit index for primary key columns."

However, read the "Multicolumn Indexes" section in the "Indexes"
chapter to be sure you understand when the index will be used and
when it won't be:

http://www.postgresql.org/docs/7.4/static/indexes-multicolumn.html

> CREATE TABLE SUPPLY
> (
> ITEM_FK integer NOT NULL,
> CONTACT_FK integer NOT NULL,
> COST numeric (7,2),
> PRIMARY KEY (ITEM_FK,CONTACT_FK)
> );

If ITEM_FK and CONTACT_FK are foreign keys, then you might want to
add foreign key constraints to ensure referential integrity.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

  • Join Table at 2004-11-01 16:34:32 from T E Schmitz

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2004-11-01 17:07:38 Re: Join Table
Previous Message T E Schmitz 2004-11-01 16:34:32 Join Table