View Single Post

  #4 (permalink)  
Old 10-23-2005
Gordon Burditt
 
Posts: n/a
Default Re: Need to understand INDEX and PKEYS

>I am not 100% happy with my understanding of indexes and primary keys.

A primary key is a unique index that in addition is the primary
one. This is similar to one of a set of twins being the real person
and the other one is "just a twin". It's which one you CALL primary
that makes the differenece.

>I have a table with ID, Author and Text, (ID is auto increment)
>
>Most of the select I do is "SELECT * from Table where ID = someID"


A field you use often in a WHERE clause is a good candidate for an index.
Chances are that any field named "ID" is unique and a good candidate
for a primary key. A column that is auto_increment must be unique
and is probably a good candidate for a unique index or primary key.

>and then I with the result I do "SELECT Text, Author from Table where Author
>= someAuthor"


Is this the same table? You got both Text and Author from the first
query above looking up by ID. Or is the point here to find other
records with the same Author as the given ID?

If you often do lookups by Author, that's another candidate for
an index (which might not be unique if, for example, this is a table
of books and an author can write more than one book).

>It all works fine enough, (because I only have 400 records), but I don't
>have a primary key or an index.
>
>What primary key/index should I create?


An auto_increment column like ID MUST have a unique index on it.
Are you sure you don't already have an index on it?
If you often do lookups by Author you may want an index on that also.

>And where could I find a simple tutorial/help on primary keys and indexes to
>help me understand them once and for all.


Gordon L. Burditt
Reply With Quote