Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9075165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:48:30+00:00 2026-06-16T18:48:30+00:00

Question Is the set up for foreign keys I want to do for Answer

  • 0

Question

Is the set up for foreign keys I want to do for Answer Table the correct Idea and how to write SQL on Add foreign key constraint dealing with multiple fields?

Session Table:

SessionId (PK) SessionName
1              AAA
2              AAB

Question Table:

QuestionId(PK)  SessionId(PK)  QuestionContent
1               1              What is 2+2?
2               1              What is 3+3?
1               2              What is 4+4?
2               2              What is 5+5?

Answer Table:

AnswerId(auto, PK)  QuestionId(fk)  SessionId(fk)  Answer
1                   1               1              A
2                   1               1              D
3                   2               1              C
4                   2               1              A
5                   1               2              True
6                   2               2              A
7                   2               2              B

Now the issue I have is with foreign keys with the Answer Table, I want to know which foreign key is correct to do.

What I have done is that I add a foreign key to the SessionId which means that if I click on a row’s SessionId, it matches the SessionId in the Session Table.

Then I thought I will provide a foreign key on QuestionId in Answer Table linking to Question Table. Problem with this though is that if within a row I click on a QuestionId, then it will display me all of the rows in the QuestionId which contains the same QuestionId. So if I wanted to look up this Answer:

AnswerId(auto, PK)  QuestionId(fk)  SessionId(fk)  Answer
1                   1               1              A

When I click on QuestionId Foreign key above it outputs this below:

QuestionId(PK)  SessionId(PK)  QuestionContent
1               1              What is 2+2?
1               2              What is 4+4?

The Answer does not belong to both of these questions, it only belongs to one question but because both QuestionId are the same, it displays both questions.

So what I am thinking is that I want a foreign key constraint where that if user clicks on the QuestionId in a row, it will look up both QuestionId and SessionId so that it knows that question and session (exam) that answer belongs to so that it outputs below:

QuestionId(PK)  SessionId(PK)  QuestionContent
1               1              What is 2+2?

My question is that I don’t know how to write a foreign key constraint which deals with two fields? Also is the set up of foreign keys for just SessionId and SessionId and QuestionId together correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:48:31+00:00Added an answer on June 16, 2026 at 6:48 pm

    You can make a composite foreign key for your Answers table using this syntax:

    FOREIGN KEY (QuestionId, SessionId) REFERENCES Questions (QuestionId, SessionId)
    

    However, it seems strange that your SessionId column for the Questions table is part of a composite Primary Key, considering you have a Sessions table using a column by that name as the Primary Key. You should probably consider making the Questions.SessionId a foreign key on your Sessions table, and have QuestionId be a standard single Primary Key.

    If you do this, you’ll find that you won’t need a SessionId column on your Answers table.

    Here’s an example of what you might do:

    -- Sessions table
    SessionId (PK) | SessionName
    
    -- Questions table
    QuestionId (PK) | SessionId (FK) | QuestionContent
    
    -- Answers table
    AnswerId (PK) | QuestionId (FK) | Answer
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In below data list represents set of question's and answer, How to check whether
My question is: How to set NSIndexPath programmatically. For example I add method: -
My question is : I have an EditText and I want set a margin
It's simple - All I want to do is set who answered the question
Boring of googling this question - how to set indentation offset for python in
Question: What is the best way to set focus to a web control in
I have a question about how to set up the relations between two models
My question is regarding this post Is it possible to set an animated gif
Old question My understanding is that C# has in some sense HashSet and set
SHORT VERSION OF QUESTION: So basically my question is: How can I set the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.