View Single Post
  #2 (permalink)  
Old 07-23-2008, 11:11 AM
Eivind
 
Posts: n/a
Diggs:
Default Re: do i always have to have a primary key in a mysql table?

krisbrixon skreiv:
> Technically no.
>
> However, you should always pick one for two reasons.
> 1. You need something to uniquely identify a row so you can do an update or
> delete and only affect that row.
> 2. A primary key is also an index and without indexes, your database will be
> much sloooooower.


But note that this doesn't mean that all tables must/should have a
single primary-key column.

For example, if you have three tables, one for classes, one for
students, and one which links students to classes, then;

You probably want a primary key, "studentid" or similar.

You probably want a primary key "classid" or similar.

Assuming one student can only enroll at most -once- in a single class,
you probably DONT need a extra field for primary key on the
student_class table, instead you make a COMPOSITE primary key, that is,
the combination of student and class is unique.

There can be multiple entries for a student (one for each class hes
taking) and multiple for each class (one for each student taking that
class); but there cannot be more than a single record saying that (for
example) student 5 is taking class 3.

These kinds of questions are really database-questions and not
coldfusion-questions though.... (the answer would be exactly the same if
you where using any other language)


Eivind Kjørstad
Reply With Quote