I need help with a database table called “Question”. I want to know which layout is better out of the two below when it comes to primary keys:
Question Table method 1:
QuestionId (int 3) PK
SessionId (varchar10) PK
QuestionContent (varchar800)
NoofAnswers (int 3)
AnswerId (int 5) Auto Increment
Marks (int 3)
In the above table, QuestionId and SessionId are PK (primary Key), AnswerId is not PK, but it is auto increment.
Question Table method 2:
QuestionId (int 3)
SessionId (varchar10)
QuestionContent (varchar800)
NoofAnswers (int 3)
AnswerId (int 5) Auto Increment PK
Marks (int 3)
In the table above, only AnswerId is PK and autoincrement. But if if I do that, it means other tables which contains QuestionId will have to link to this table’s QuestionId as non keys or something like that.
The database is not letting me do PK for QuestionId, SessionId and AnswerId as its saying that AnswerId is auto increment so no need for any other primary keys.
I think both cases are not good! You are giving PK’s a meaning, but PK’s should have no meaning.
Just make your PK a
int(11) Auto Incrementand link tables together with a foreign key.Update
Each table has its own PK. The Questions table has a QuestionId PK.
Tying table together is based on the relation that tables have. There are different scenarios for
1:1relations,1:manyrelations andmany:manyrelations.Example:
Looking from the Questions there can only be 1 answer on a question (
1:1). Looking from a answer, it can be used for many questions (1:n). For amany:manyrelations you need a extra table.Here is a youtube doing some extra explanation: http://www.youtube.com/watch?v=RXOj0D80kRg