Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Nested Relationship Primary Key Design?

Author  Topic 

outatime
Starting Member

2 Posts

Posted - 2009-03-10 : 12:47:26
I have a Survey database with the following related tables:

Surveys -> QuestionGroups -> Questions -> Answers

Which of the following is a better design for the primary keys?

A) Use composite keys where the keys are as follows:

Surveys -> SurveyID
QuestionGroups -> SurveyID & QuestionGroupNumber
Questions -> SurveyID & QuestionGroupNumber & QuestionLetter
Answers -> SurveyID & QuestionGroupNumber & QuestionLetter & AnswerOrdinal

B) Use a single unique ID and foreign key in each table:

Surveys -> SurveyID
QuestionGroups -> fk_SurveyID & pk_QuestionGroupID
Questions -> fk_QuestionGroupID & pk_QuestionID
Answers -> fk_QuestionID & pk_AnswerID

Any thoughts will be appreciated!

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-03-10 : 13:05:53
What is relationships(Cardinality) between tables? Also include necessary attributes that goes with tables .
Go to Top of Page

outatime
Starting Member

2 Posts

Posted - 2009-03-10 : 17:04:10
Surveys : QuestionGroups
1:N

QuestionGroups : Questions
1:N

Questions : Answers
1:N

Surveys
SurveyID
SurveyDescription
...

QuestionGroups
QuestionGroupNumber
QuestionGroupTitle
...

Questions
QuestionLetter
QuestionText
...

Answers
AnswerOrdinal
AnswerText
...

So I could insert a single primary key column in each table or use a composite key. For instance the primary key for the Answers table could be an AnswerID column, or a SurveyID column, a QuestionGroupNumber column, a QuestionLetter column, and the AnswerOrdinal column.

I'm just not sure what is the best practice for this relationship. A composite key could be quite handy for reporting. A single answer key is quite handy for databinding to web controls...
Go to Top of Page
   

- Advertisement -