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 7092483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:19:09+00:00 2026-05-28T08:19:09+00:00

I am trying to create a database, which has 2 tables with mutually dependent

  • 0

I am trying to create a database, which has 2 tables with mutually dependent foreign keys.

First table is called questions, it holds data regarding questions that are asked by users, and also needs to hold a key to the best answer that was answered on the question.
(This should be a foreign key to our second table called Answer)

Second table is called Answer, it holds information regarding answers to questions, and also needs to hold a question_id field, that is a key to the question that this answer answers.
this is also a foreign key, to the first table.

When I try to create the tables, It cannot create the first one, since it is not aware of the second one (error when we try to declare the foreign key to the second table which does not exist yet)

Here is the code I am using :

create table question
( q_id              numeric(10,0),  
  best_a_id         numeric(10,0),
 primary key(q_id),
 foreign key (best_a_id) references answer(a_id),
); 


create table answer
( a_id              numeric(10,0),
  q_id              numeric(10,0) not null,
 primary key(a_id),
 foreign key (q_id) references question(q_id),
);

How Do i resolve this problem?
Thanks

  • 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-05-28T08:19:09+00:00Added an answer on May 28, 2026 at 8:19 am

    Create the first table without the foreign key constraint. Then create the 2nd table as-is. Finally, go back and alter the first table, adding the foreign key constraint seperately.

    And the SQL to add the foreign key will look like this:

    ALTER TABLE question
    ADD FOREIGN KEY (best_a_id)
    REFERENCES answer(a_id);
    

    Just curious, but why maintain the question-to-answer relationship in both tables? Because (as ypercube points out) you don’t have a “best answer” when the question is first asked, yet your design requires it. It’s probably better to maintain that relationship in the answer table, similar to how Olivier recommended.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a simple database table using the PHP MySQL query Create
I have a test results database which has the following three tables along with
My context model has a couple of related tables: CREATE TABLE CarSystem.Reads ( ReadId
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
im trying to create a database for a feedback application in ASP.net i have
I'm trying to create a Database object for myself in .net. The constructor takes
I am trying to create a database deadlock and I am using JUnit. I
I am trying to create a database of users with connection between users (friends
I am trying to create new Event objects to be persisted in the database
I am trying to create entities out of a SQLite database. SQLite doesnt have

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.