V
V
Viktoria Smirnova2019-06-14 06:25:16
PostgreSQL
Viktoria Smirnova, 2019-06-14 06:25:16

What is the correct practice for using a unique identifier (Postgres SERIAL)?

Good afternoon guys.
What would be the correct practice to use a unique id in my case?
There is a table with questions:

create table questions
(
  id    serial      not null
    constraint questions_pkey
      primary key,
  title varchar(25) not null
);

and there is a table with answers:
create table users
(
  question_id integer
    constraint users_question_id_fkey
      references questions,
  a           varchar(25)
);

As you understand, the answer "no" or "yes" is entered in the "a" field.
In my business, questions are collected in a Report (Reports are different. Depending on the Report, they have from 10 to 70 questions). Thus, the answers entering the user table must be divided by Reports.
How to implement it?
My vision of the problem is to create a table with reports that will assign a unique identifier, then take it and put it in the question_id field in the users table of each question, but these are two consecutive queries. What is the correctness of this algorithm? Or maybe there is a more elegant solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2019-06-14
@melkij

As you understand, the answer "no" or "yes" is entered in the "a" field.

what level of telepathic skills is required for this? No, it's not obvious at all.
And if the field is for boolean values, then why is the data type so strange? I won’t even ask about the name of the answer table.
I would like to know how the second follows from the first.
If you have some "reports" consisting of a certain number of questions, and users answer the questions, then it is quite obvious that you have:
a table of users
a table of reports
a table of questions with a field linking to a report
a table of answers of 3 fields: user id, question id , the value of the response. Primary key to the first two fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question