I am trying to create a database that has 2 tables with interdependent foreign keys.
The first table is called questions, it contains data on questions asked by users, and should also contain the key to the best answer to which the answer to the question was given. (This should be the foreign key for our second table, called the "Answer")
The second table is called the “Answer”, it contains information about the answers to the questions, and should also contain the question_id field, which is the key to the question to which this answer answers. it is also a foreign key to the first table.
When I try to create tables, it cannot create the first, since it does not know the second (error when we try to declare a foreign key to a second table that does not exist yet)
Here is the code I'm 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 to solve this problem? Thanks
happy dude
source share