pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedUnique primary index?
5+ messages / 5 participants
[nested] [flat]
* Unique primary index?
@ 2005-06-23 12:43 orange_crush_068@hotmail.com
2005-06-28 07:50 ` Re: Unique primary index? M.D.G. Lange <mlange@dltmedia.nl>
2005-06-28 12:53 ` Re: Unique primary index? Bruno Wolff III <bruno@wolff.to>
2005-06-28 17:01 ` Re: Unique primary index? PFC <lists@boutiquenumerique.com>
0 siblings, 3 replies; 5+ messages in thread
From: orange_crush_068@hotmail.com @ 2005-06-23 12:43 UTC (permalink / raw)
To: pgsql-sql
What are the major differences between Unique, primary index & just
plain index?
When creating tables I nominate one of these, but not sure what the
difference is?
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Unique primary index?
2005-06-23 12:43 Unique primary index? orange_crush_068@hotmail.com
@ 2005-06-28 07:50 ` M.D.G. Lange <mlange@dltmedia.nl>
2 siblings, 0 replies; 5+ messages in thread
From: M.D.G. Lange @ 2005-06-28 07:50 UTC (permalink / raw)
To: orange_crush_068@hotmail.com; +Cc: pgsql-sql
I would say that you should learn a bit about relational databases
before you start working with them ;-)
All unique fields (or combinations of fields that -combined- are unique)
can serve as a primary key.
In relational databases we call them 'candidate key'.
if you have more than one candidate key in a table, there's nothing
wrong. You just simply choose.
Best choices are:
- select the set of the least amount of columns. (preferably only one)
- if you still have more than one candidate key, choose a column with an
integer (as integers are looked up the fastest, and you want your
indexes to be fast)
- if you still have more than one candidate key, pick one
That one you define as your primary key. By defenition a primary key is
unique. But not all unique fields are primary key.
The primary index is the index that should be chosen most, because it is
your fastest or best index. From my experience it is always the index of
your primary key, since the reasons for selecting the primary index are
normally about speed.
How you can create your indexes best, and how many you create and how
you create them are beyond the scope of this mail. I suggest you read
the documentation about the indexes.
Also get a book about basics of relational databases, it is really
useful! Also getting yourself informed about normalisation (usually up
to the 3rd form suffices) can help you on your way.
- Michiel
orange_crush_068@hotmail.com wrote:
>What are the major differences between Unique, primary index & just
>plain index?
>
>When creating tables I nominate one of these, but not sure what the
>difference is?
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 8: explain analyze is your friend
>
>
>
>
>
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Unique primary index?
2005-06-23 12:43 Unique primary index? orange_crush_068@hotmail.com
@ 2005-06-28 12:53 ` Bruno Wolff III <bruno@wolff.to>
2 siblings, 0 replies; 5+ messages in thread
From: Bruno Wolff III @ 2005-06-28 12:53 UTC (permalink / raw)
To: orange_crush_068@hotmail.com; +Cc: pgsql-sql
On Thu, Jun 23, 2005 at 05:43:52 -0700,
orange_crush_068@hotmail.com wrote:
>
> What are the major differences between Unique, primary index & just
> plain index?
>
> When creating tables I nominate one of these, but not sure what the
> difference is?
A unique key has an implied constraint that no two nonnull values can
be the same. This is implemented using a unique index which is automatically
created.
A primary key is a unique key that has an implied not null constraint and
is the default for foreign key references into the table.
You can also create stand alone indexes for performance reasons or to
enforce a unique constraint on a subset of a table.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Unique primary index?
2005-06-23 12:43 Unique primary index? orange_crush_068@hotmail.com
@ 2005-06-28 17:01 ` PFC <lists@boutiquenumerique.com>
2005-06-28 17:47 ` Re: Unique primary index? Scott Marlowe <smarlowe@g2switchworks.com>
2 siblings, 1 reply; 5+ messages in thread
From: PFC @ 2005-06-28 17:01 UTC (permalink / raw)
To: orange_crush_068@hotmail.com; pgsql-sql
index is... an index !
UNIQUE is an index which won't allow duplicate values (except for NULLS)
PRIMARY KEY is exactly like UNIQUE NOT NULL, with the bonus that the
database knows this column is the primary key so you can use stuff like
NATURAL JOIN without telling which column you want to use.
>
> What are the major differences between Unique, primary index & just
> plain index?
>
> When creating tables I nominate one of these, but not sure what the
> difference is?
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend
>
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Unique primary index?
2005-06-23 12:43 Unique primary index? orange_crush_068@hotmail.com
2005-06-28 17:01 ` Re: Unique primary index? PFC <lists@boutiquenumerique.com>
@ 2005-06-28 17:47 ` Scott Marlowe <smarlowe@g2switchworks.com>
0 siblings, 0 replies; 5+ messages in thread
From: Scott Marlowe @ 2005-06-28 17:47 UTC (permalink / raw)
To: PFC <lists@boutiquenumerique.com>; +Cc: orange_crush_068@hotmail.com; pgsql-sql
On Tue, 2005-06-28 at 12:01, PFC wrote:
> >
> > What are the major differences between Unique, primary index & just
> > plain index?
> >
> > When creating tables I nominate one of these, but not sure what the
> > difference is?
> index is... an index !
>
> UNIQUE is an index which won't allow duplicate values (except for NULLS)
>
> PRIMARY KEY is exactly like UNIQUE NOT NULL, with the bonus that the
> database knows this column is the primary key so you can use stuff like
> NATURAL JOIN without telling which column you want to use.
No. natural join joins on columns with the same name. However,
primary keys ARE automatically referred to by fk'd columns.
Personally, I think that a natural join should use the primary/fk
relationship as well, but I guess that's not what the spec says.
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2005-06-28 17:47 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-06-23 12:43 Unique primary index? orange_crush_068@hotmail.com
2005-06-28 07:50 ` M.D.G. Lange <mlange@dltmedia.nl>
2005-06-28 12:53 ` Bruno Wolff III <bruno@wolff.to>
2005-06-28 17:01 ` PFC <lists@boutiquenumerique.com>
2005-06-28 17:47 ` Scott Marlowe <smarlowe@g2switchworks.com>
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