0

I'm trying to do something like a "decision tree" in my BBDD. The objective is kind of the questions of Google, you have a question with three answers and each of the answers have assigned a different question.

Well, I know I need recursion to do that. So far I've tried these two data models.

CREATE TABLE Level (
     question_id SERIAL PRIMARY KEY,
     question_description varchar (100) not null,
     level smallint not null
);

CREATE TABLE Response (
     response_id SERIAL PRIMARY KEY,
     response_body varchar (25) not null,
     principal boolean not null,
     level int not null,
     parent_response int,
    FOREIGN KEY (parent_response) REFERENCES Response (response_id),
     FOREIGN KEY (level) REFERENCES Level (question_id) ON DELETE CASCADE
); 

And, the second data model it's for this question, trying to pass it to SQL.

CREATE TABLE question (
    id_question SERIAL PRIMARY KEY,
    question_text VARCHAR(255)
);

CREATE TABLE responses (
    id_response SERIAL PRIMARY KEY, 
    response_text VARCHAR(255),
    question int,
    FOREIGN KEY (question) REFERENCES question(id_question) 
)

Neither of these worked correctly because I need to know which question comes after each answer.

I have outlined for a better understanding of the problem. I want to achieve this I want to achieve this

  • Tag your question with the database you are using. – Gordon Linoff May 19 '21 at 10:59
  • Please can you add to your question 1) Some sample source data 2) the result you are getting 3) the result you want to achieve 4) The SQL queries you are running – NickW May 19 '21 at 13:05
  • @NickW Thanks for comment. I've got a CRUD for the models, that's working. The problem's the data structure, bcs I think I need recursion in the question table and not in the responses table, but I can't do that because of the (one to many) relationship. I'm going to upload a schema to give you more information. – Andrea Méndez May 20 '21 at 06:50
  • Just to be clear - are you trying to model the questions and answers (i.e. all the possible paths) or the questions and responses (the specific path that an individual took when answering the questions) - as those are not the same models? – NickW May 20 '21 at 08:14
  • @NickW All the possible paths – Andrea Méndez May 20 '21 at 10:45

1 Answers1

0

I would model it with intersection tables between the questions and responses - gives you flexibility and allows you to add attributes to the relationships e.g. weighting a question/response combination.

Proposed Data Model

NickW
  • 8,430
  • 2
  • 6
  • 19