ORM Database design for quiz website - mysql

I am trying to learn ORM and better structuring a database. I am working on a simply quiz web app to try to learn the relationships between tables. Am I on the right track here?
Down below are my tables. And I was thinking like this:
Question has one answer.
Answer belongs to many questions.
Question has many alternatives.
Alternatives belongs to many
questions.
The tables
Question
ID
Question
Answer
ID
Answer
Alternatives
ID
Alternative
QuestionAlternative
QuestionID
AlternativeID

Consider a data model something like this:
You have a list of questions, each of which has zero, one or more offered options (possible answers). For each question, a user enters an answer. This answer may refer to a specific OPTION if applicable, or it could contain some kind of free-form information in the ANSWER table itself.

Related

Database schema for MCQ website/Quizzer

So I want to design a database schema for MCQ website in which I have several sections ,Lets say 3. and these section will have many sub-sections each.
I want to store the questions, choices and correct ans in such a way that every questions of sub-topics get stored in same table but questions of different section`s sub-section get stored in different one.
but,I don't know how to design a database schema like this. or what else could be better Approach ?
Possibly something like above? You have a section that can have as many questions associated as you want. Then a question that can associate with as many answers as you want. But the FK "answer_ID" under "Question" is a single "answer_ID" from the "Answers" table which tells you what the correct answer is. May not be the best but at least it would be a start. EDIT: the lines on the chart are wrong, sorry.

Surveys and Answers, relation on many-to-many

I have the following database scheme (I don't know if it's perfect but I think it's allright?)
It's a system where a User has many Surveys, the Users provide Answers for Questions in the Survey_Answers table.
Now a User can have multiple Surveys, it's the same questions but in a later time of the year they have to fill in the survey again.
I'm nearly there, I'm just wondering how to connect the answers to the survey. Should I make a relation between survey_answers and user_surveys.. thus adding an id to the user_surveys table
Or do you think it's ok to make a relation to the surveys table? I'm not sure which one is correct.
I outlined the 2 possibilities in the second screenshot.
Looking forward to your responses!
Thank you.
This probably depends on how your system is most likely/most frequently going to navigate the relationship.
If you are more likely to be looking at a Users answers and saying - hey let me see when this question was answered, as part of which dated survey, then you should join on user_surveys (I am assuming that the employee_id you are storing would match the user_id in user_surveys)
If you're more likely to be looking at a Users answers and saying - hey what survey did this question belong to, then you should join on Surveys.
You can still answer either question whichever join you use, it will just be a matter of more optimal performance (fewer table joins when trying to answer the most common query).
In reality there probably isn't much in it, so you could always toss a coin :)

Reusable Questions in MySQL Survey Design

I'm working on a Reusable Survey database design. So the idea is.
A Client has many users, A client has categories which consist of questions. Every User has to answer all questions to complete the Survey. Those answers are stored in the Answers table.
The hard part
Some users are coaches, so a coach can fill in the survey for the user, thus providing a score on what they would answer in the place of the user. So we can later compare what the user answered and what the coach answered for each user. That's not to hard! The following is:
After some months we should be able to let the users redo the surveys, so with new answers to all the still existing questions.
I'm wondering if my db design is allright for this.
I have the feeling that this isn't optimal.
For example the following queries seem difficult with my design
For a given Scan, give me all categories and questions
(because of the many tables in between)
Looking very forward to your responses!
Think about how you are going to want to use this information. Are you going to want to compare users scores to coaches scores to their new scores? I think that is likely. Will they end up taking the survey multiple times if they don't improve enough? Are there going to be questions that do not have integer answers? How are you going to store those results? When they create a new survey are they going to want to reuse some previous questions or answers (like yes/no). How are you going to identify a unique user, names are not unique and autogenerated IDs are unique, but how will you know which John Smith belongs to which of the 12 ids you have?
I would rename the Answer table as SurveyResponse.
To it I would add the datetime of the surveyresponse (so people can
answer it multiple times and you can compare the answers) and a
Survey ID (from the new table in the next suggestion).
I would create a Survey table that stores the questions that belong
to a particular survey.
I would create a new Answer table that just has possible answers and
an ID.
I would create a table called SurveyQuestionAnswer which stores the
allowed answers to the question for each survey (different surveys
might have different possible responses to the same question).

categories in questions algorithm

Situation
I'm a learner and im building a webapp in which people can login and ask/answer questions, sort of like quora.
Problem
I want the questions to be asked in perticular categories like..funny,tech,news,etc. and each questions can have multiple categories. So Im having a difficulty to desing a database based on this.
Possible Solution
My each question will have a questionid, unique and i can have a whole another table just for categories -> (questionid,category) which may have multiple entries per questionid. But is there any better solution?
Since each question can have multiple categories and each category can have multiple questions;Like this,
CATEGORY_TABLE
-categoryId(PK)
-categoryName
QUESTION_TABLE
-questionId(PK)
-question
QUESTION_CATEGORY_TABLE
-questionId(PK)
-categoryId(PK)
I guess this is what you have suggested as a solution right?

Best modeling tips in DynamoDB in User/Friend Tables

In mysql a User and Friend Table will somewhat look like these
User Table
id
name
phone
status [enabled/disabled]
===============================================
Friend Table
user_id
another_user_id
status [if friend or not]
===============================================
but in DynamoDb
I have been troubled about these two ways either
Approach 1.
User Table
id
name
phone
friends -> attributes
OR
Approach 2.
User Table
id
name
phone
===============================================
Friend Table
user_id
another_userid
===============================================
Im currently using the Approach 2. question is whats the best way to model tables in cost effective manner , latency and performance?.
PS: I emailed their support about these problems for me but still has no reply from them
so someone should already had gone through these problems.
I hope I iterated the question carefully to be understandable.
EDITED:
#chen
Q: Do you often query a user's friend list?
A: yes I will query every users-friends-list that will use my software
when a user logs in.
Q: Do you wish to know fast how many friends does a user have?
A: No, no need as long as i can get who the users friends are then its all good.
Q: How many friends do you think a user will have?
A: unlimited.
Q: How many users will the system have?
A: unlimited too.
thanks for giving the time.
thanks
David, you are running into a typical NoSQL problem.
When designing a relational database, you model the data as it fits the world, and also try to break the data into tables.
In DynamoDB (and other NoSQL) the real model is derived from the questions needing answers.
Do you often query a user's friend list?
Do you wish to know fast how many friends does a user have?
How many friends do you think a user will have?
How many users will the system have?
These questions will help you decide between approach #1 and #2.
If you comment with answers to these questions, I will be able to give you my thoughts on the model.
Regardless, if you really want to drop SQL, you might want to look at graph databases.
If you must use DynamoDB, then just keep references in the same table (approach 1). Have you already taken a decision on which DB to use? Couple of Reasons:
Some other NoSQL DBs have a vibrant community and great documentation.
GraphDB best seems to suit your problem above, but you are better aware of your systems' big picture.