Re: Newbie question about db normalization theory: redundant keys OK?
Date: Fri, 14 Dec 2007 13:43:35 -0800 (PST)
Message-ID: <692ee78d-c398-4cf2-b56f-7cd41db204da_at_d4g2000prg.googlegroups.com>
On Dec 14, 11:49 am, "David Cressey" <cresse..._at_verizon.net> wrote:
> An example of a compound key. An enrollment system for courses, students,
> and enrollments.
> An enrollment might be called a student-course. In the MS design pattern,
> they will create keys for each table:
>
> StudentId for Students,
> CourseID for Courses,
> and StudentCourseID for StudentCourses.
>
> Lets accept for now that StudentID and CourseID are good ways to identify
> students and courses. Other participants will object, if the past is any
> guide, but you can analyze their objections for yourself.
>
> I claim, at this point, that StudentCourseID as a primary key for the third
> table, the StdentCourses table, is a bad idea. A better idea is to let
> StudentID and CourseID be a compound primary key for StudentCourses.
>
> These two columns will be in the database you get from the wizard anyway.
> They have to be there to serve as foreign keys, relating the row in
> StudentCourses to parent rows in Students and Courses. There is little to
> be lost, and much to be gained, by making these two column the compound
> primary key of this table.
OK, I just did this, and I would greatly appreciate your feedback at some point.
I have three tables, STUDENT table, with StudentID (PK), COURSE table, with CourseID (PK), and a third table STUDENTCOURSE, which within Design View I was actually able (without using the Wizard!) to set up a compound key using this trick: make sure the two fields (shows an rows but they're fields of course) you want to be the compound primary key are next to each other (this is important it seems), then holding down the "Control" button, select both 'rows' (I used the mouse, but arrow keys probably also work), then right click and select "Primary Key" (symbol), then the little "PK" key will show up next to both fields! Pretty cool, now both fields are your primary compound key. Then, in the relationship window, I enforced Referential Integrity (always a good thing from what I can tell) for this compound key of table STUDENTCOURSE with the PKs of both STUDENT and COURSE. It's a non-exclusive, one to many relationship so I had the "1" symbol running from both the first two tables to TWO "infinity" symbols (for many) running into the third table. Seems to work fine.
I think I'm getting the hang of this RDBMS stuff, it's pretty neat.
RL
PS--at some point I'm going to have to find out why a compound key is preferred sometimes, and I'm not talking about the semantics of what a compound key is, but that's further down the road and this is neither the time nor place for that probably. Received on Fri Dec 14 2007 - 22:43:35 CET