Q
Q
quges12018-02-25 06:42:49
PostgreSQL
quges1, 2018-02-25 06:42:49

How to organize a table schema for surveys?

The table needs to store the options for answering the poll and the id of the users who voted for the corresponding answer.
For example,

choice = {
"1-ый вариант ответа:  ["1-юзер"],
"2-ой вариант ответа:  ["2-юзер", "3-юзер"]
}

In this case, you need to add users who vote all the time. How to implement the choice field, accessing it and adding users?
CREATE TABLE polls(
    id serial primary key,
    url text not null,
    user_id integer not null,
    question text not null,
    choice .... ,
    FOREIGN KEY (user_id) REFERENCES USERS(id)
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kisaa, 2018-02-25
@quges1

Table Polls:
Poll_id | Poll_text
Table Choices
Poll_id | Choice_id | Choice_text
Table Users
User_id | poll_id | Choice_id
If there is only one poll at all, then it is even simpler, without the first table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question