Re: Using indexes and keys

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: John Taylor <postgres(at)jtresponse(dot)co(dot)uk>, <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Using indexes and keys
Date: 2002-02-11 16:37:05
Message-ID: web-690466@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

John,

> If I need to lookup on a combined key, how do I do that ?

To SELECT or JOIN based on a multi-column key, you need to use WHERE
conditions for each column:

...FROM tablea JOIN tableb ON (tablea.c1 = tableb.c1 AND tablea.c2 =
tableb.c2)

If you want to make sure and use the multi-column index on that key,
make sure to keep the columns in the same order as they were in the
key declaration.

> The CREATE TABLE syntax doesn't seem to allow me to combine the
> columns into a single key name.

Why not?

CREATE TABLE tablea (
col1 INT4 NOT NULL,
col2 INT4 NOT NULL,
CONSTRAINT tablea_pk PRIMARY KEY (col1, col2)
);

> Additionally, if I combine two VARCHAR columns are the following keys
> unique ?
>
> ...
> a VARCHAR(10),
> b VARCHAR(10),
> PRIMARY KEY (a,b),
> ...
>
> a="abc", b="def"
>
> a="ab", b="cdef"

Yes, they are unique. Postgres does not concatinate fields to make a
multi-column key.

-Josh

P.S. For anyone just tuning into the thread, keep in mind that
multi-column keys are considerably more trouble than they're worth 75%
of the time.

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2002-02-11 16:40:06 Re: C Function Question
Previous Message Christopher A. Goodfellow 2002-02-11 16:24:57 [Novice] Create Table