Re: Join Table - Mailing list pgsql-sql

From Michael Fuhr
Subject Re: Join Table
Date
Msg-id 20041101170324.GA18943@winnie.fuhr.org
Whole thread Raw
In response to Join Table  (T E Schmitz <mailreg@numerixtechnology.de>)
Responses Re: Join Table  (T E Schmitz <mailreg@numerixtechnology.de>)
List 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/


pgsql-sql by date:

Previous
From: T E Schmitz
Date:
Subject: Join Table
Next
From: Tom Lane
Date:
Subject: Re: Join Table