Database diagram for multiple selection - database

I am having a logic problem while designing my database for a online survey. Part of my software consist in a survey generator where the user can create a question and add checkboxes and radiobuttons as options. So every question have an ID, however 1 question can have many options.
So far I have only work with software where the user can choose from determined range of options so I can collect the input and store it in the respective column of a table. However this is a different case for me because the columns are not there yet. I am trying to figure out a way to work around however I have been block for hours.

Assuming by options you mean Possible answers to a question
Question -Tracks the question of the survey
QID PK
Answers - Tracks the available answers for a survey item
AID PK
QuestionAnswers - Identifies which answers can be used on which questions
QUID PK
AID PK
Users - identifies a person taking a survey
UID PK
UserAnswers - Identifes the answers a user has selected for a survey and relates to both users and questionAnswers thus limiting which answers can be used for a question but each row allows the user to select 1-M answers as needed.
UID PK
QID PK
AID PK

Related

ER model design for application with changing number of Questions

I need to create an ER model for an application that has some questions and answers, some questions have more than one answer. Currently, I created an ER model as follows.
Step 1: Create separate tables for Questions and Answers. Both these
tables have one column named TYPE which is question number (this is
to make sure that answers belong to a particular question only). There is no relationship between Questions and Answers table.
Step2: Create a table AnsweredQuestions where we can save the information
of questions answered. This table contains 10 columns with names
Answer1, Answer2,..., Answer10 to enter the answers for each
question. The number of questions/Questions/number of answers can
change in the future.
Is there any better way of designing this ER diagram?

Is AWS DynamoDB fit for data collecting and analysis app?

I'm building a simple let's say survey app. I have the following requirements:
User:
- name
- surname
- age
Question:
- question (text)
- type
- author
Answer:
- value
- date
I want users to give questions to answers and I want to be able to query the following:
Get user's questions and answers
Get question and its answers
Get user's questions and answers by (or where) type
Get answers to questions by value (or where value)
Count answers to a question
Get answers to a question by user age
Get answers to questions over time (by user or general)
So far I came up with the following solution:
So here the Partition Key is the ID and the Sort Key is type
So the first problem is the first requirement:
Get user's questions and answers
Shall I add another type which would be user ?
Now how to:
get a question and its answers ?
I can query id = 1 and type starts with question but then I can get a lot of unnecessary user records.
Now the next one:
get user's answers to questions of specific type (type as question attribute)
How to count same answers to a question?
I'm new to DynamoDB so any help is greatly appreciated!
I would add an id (uuid generated from code) for each table.
Get user's questions and answers
Set userId as field in both Question and Answer, then set an index on that. This will allow you (with 2 separate queries) to get all questions and answer of a particular user.
get a question and its answers ?
Answer will have as field questionId and same index principle applies
get user's answers to questions of specific type (type as question attribute)
You can add a field to Answer, which is questionType and filter on that. Remember that NoSQL schema focuses on the queries you need, not for a perfect normalised schema.
How to count same answers to a question?
I am guessing you meant how many answers does a question have. Well if you know the questionId, just follow "get a question and its answers ?" and execute a .withSelect(Select.COUNT) on the query
EDIT
User:
- userId (hash key)
- name
- surname
- age
Question:
- questionId (hash key)
- question (text)
- type
- author
- askedByUserId (index - hash key)
Answer:
- answerId (hash key)
- value
- date
- answeredByUserId (index - hash key)
- questionId (index - hash key)
So everything depends on how you query data (and of course you may end up switching to SQL even, noSQL not an 1:1 replacement). Referring just by id is not wrong, I would prefer to have immutable questions and answers instead of editing a question every time it receives an answer. So take into consideration that you can't use joins and always prefer immutable data (add a new value instead of editing).
EDIT 2
In order to get all questions and answers from a User, create and manage this table:
UserItems:
- userId (hash key)
- question (full json of a question, optional)
- answer (full json of a answer, optional)
So every time you create a new question for example, add it to both Question and UserItems. Do a simple query based on hash key and you will get all questions and answers, with full data. Again, this is based on your querying needs.
Ok, so it took me a few good hours to figure out the answer.
Let's start with the entities and their relationships, here is the diagram:
Where:
USR - User
ANS - Answer
QUE - Question
As it can be seen above, I don't have any many-to-many relationships so the only pattern I will be using for retrieving the data is the Primary Key (Partition Key + Selection Key) pattern.
If I had many-to-many relationship in the model, apart from PK+SK I would also use GSI (Global Secondary Index)
GSI can also be used to create a different view, for example by swapping places of PK with SK.
Now the DB access patterns:
The above unfortunately doesn't cover all the access patterns, a few more I can think of are:
Query answers by user age
Query answers to a question and calculate percentage of answers by type (should this be calculated outside of the DynamoDB?)
And now the DB Table design:
So based on the access patterns in the Access Patterns table with this design I can query:
All the answers of a user to a question
SELECT PK=USR_1 WHERE SK begins_with(AR_QUE_2)
All the answers to all the question by a user
SELECT PK=USR_1 WHERE SK begins_with(AR_QUE) - in this case I would have to include question metadata (question itself as text) in the attributes of the AR_QUE_X_ANS_Y so that I don't have to query the table multiple times to get all the questions metadata.
All the answers to a question
Select PK=QUE_2
I'd really love to hear someone's opinion on this one.
I also appreciate that in some cases I would have to denormalise data and insert metadata information in Attributes.
I am still very curious how I could calculate number of answers by type to a question and calculate for example a percentage of answers of different type.

Managing both fixed and user-defined values

I'm designing the database for an application in which the user is presented with questions, and he must answer them. Think of it either as a questionnaire or as a quiz game, the concept applies to both. I plan to have:
a table with the questions
a table with the possible answers, each of them linked to the question it belongs to with a foreign key (let's keep things simple and assume it's a 1:many relationship, where answers cannot be shared between questions)
a table with the answers that users provided (with foreign keys to the question, the answer and the user ID)
Since many of the questions will be common cases, like yes/no, I decided I'd specify a "question type" enumeration to each question. If the application sees a yes/no question, for example, it means there are no answers in the database, and the application will automatically add the two answers, "Yes" and "No". This saves me hundreds or thousands of useless rows in the answers table.
However, I'm not sure how I should define the table to record user answers. Without the special types of questions, I'd just record the question ID, the answer ID and the user ID, which means "user X answered Y to question Z". However, "yes/no" questions would not have a matching answer in the table, so I can't use the answer ID.
Even making the answers shareable between questions (by making a many-to-many relationship between questions and answers) is not a good solution. Sure, it would allow me to define "Yes" and "No" as regular answers, but then applications should be aware that a "yes/no" question uses answers (say) 7 and 8 - or, when creating a "yes/no" question answers 7 and 8 should be bound to that question. But this means that these "special" answers' IDs must be hardcoded somewhere else. Also, this would not scale well should I add more special types of question in the future.
How should I proceed? Ideally, I need to store in each row of my "user answers" table either a fixed value or a foreign key to the answers table. Is there a better solution than using two columns, one of which is NULL?
I'm using SQL Server, if that matters.
Based on your description I think I'd go on the route of adding another column to the table and making the FK column nullable.
You'd probably have only a few choices for those special questions, so a nullable TINYINT datatype would cut it, and it is only 1 extra byte for your answer row. If this extra column happen to raise the number of columns to more than a multiple of eight, say you go from 8 to 9 or 16 to 17, than you pay another extra byte for the growth of the null bitmap. But it's 2 extra bytes per row worst case.

SQL Questionaire Database Design (EAV Model) Issue

Im building a friendship site where I try and match users who share similar interests.
I have 25 questions with defined answers(drop down answers) that the user must fill out.
Im using an entity–attribute–value model to store the users id the question id the answered id the user selects.
I then use the count function to see which users have the most matches to my profile.
Current Table Structure
Question Table
Answer Table
Question_Answer_User Table
The problem im running into is I have two question and im not sure where the best place to store them is.
The question is what is your country?
The question is what is your State?
Im not sure if I should store them with the other 25 questions or if i should store them in a three separate tables as seen below.
country table
state table
user_country_state Table
There are going to be alot of answer entries for these 2 questions. For example there are 25 countries the user can answer and a total of 900 states / provinces the user can answer from.
I want to be able to consider the users location as similarity to count but im not sure what the best approach to incorporate this is?
I think the selected country and state should live in the user table, along with the other necessary user information such as name and email address. I don't think it belongs in the Answers table, but it would work there.
For the list of options for the user to select from when setting up his account, storing them in your pre-defined Q and A tables are as good a place as any. It depends, I guess, on how your data and functionality is broken apart so that you aren't crossing responsibility boundaries for the Q&A table storing survey-type answers as well as user-setup answers.

Should a table with only 1 field of useful data be its own table

Just got a question here about a database table. If the table only has a primary key (identity) and 1 column of useful data, is it okay to be its own table or should it be in the parent table as just the data?
The table is storing Security Questions that the user will set up with they make their account and be used to reset password in the event they want to change password or forgot the password. I have the ID of the question, and the question string in this table.
The reason I have it in its own table is that the same question could be used for many users so why store the question many times in the parent table. Thats my thinking, just wanted a few others' opinions on this.
EDIT: The Security Questions are going to be input by my team, not the user themselves. The user will pick one of the questions to use.
I would suggest this sample design using bridge table:
You can have multiple questions for a user as well as their answers unique. Also, the questions can be same for multiple users.
You must always try to prevent duplicates, that's why your solution is the best.
it will also keep your database smaller. A foreign key with int value is smaller than a string.

Resources