SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Nested Relationship Primary Key Design?


Nested Relationship Primary Key Design?

Author
Message
outatime
outatime
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 30
I have a Survey database with the following related tables:

Surveys -> QuestionGroups -> Questions -> Answers

These are all nested 1 to many relationships. Which of the following is a suggested practice design for the primary keys?

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

Table -> Key
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:

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

Any thoughts will be appreciated!
Michael Valentine Jones
Michael Valentine Jones
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5774 Visits: 11771
Consider this variation of B. It allows you the flexibility to use a QuestionGroup in multiple Surveys without having to re-enter the questions for each Survey.

Table         Keys
Survey
PK = SurveyID
QuestionGroup
PK = QuestionGroupID
SurveyQuestionGroup
PK = SurveyQuestionGroup,
FK = SurveyID to Survey,
FK = QuestionGroupID to QuestionGroup,
AK = SurveyID,QuestionGroupID
Question
PK = QuestionID,
FK = QuestionGroupID to QuestionGroup,
AK = QuestionID,QuestionGroupID
Answer
PK = AnswerID, FK = QuestionID,
FK = QuestionID,QuestionGroupID to Question,
FK = SurveyID,QuestionGroupID to SurveyQuestionGroup


outatime
outatime
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 30
Hey, that's pretty slick! Especially if you're re-using questions. However, in my Survey application each QuestionGroup will belong to exactly 1 Survey. So all "children" will have exactly one "parent".

There is also a "Responses" table. When a response is inserted for situation "A" it will need the SurveyID, QuestionGroupNumber, QuestionLetter, and AnswerOrdinal. For situation "B" it will need only the AnswerID.

"B" would be handy for binding to web controls, but "A" would be handy for reporting.

Decisions, decisions...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search