Circular relationship in DB design - sql

I've some doubts about my database design. I've four tables that follow the following rules:
One form can have many questions (0..n).
One form can have many responses (0..n).
A question belongs to one form (1).
A response belongs to one form (1).
A response have many anwers to a question (Answer table) (0..n).
An Answer belongs to an specific question of a form(1).
The design has led to a circular looking dependency graph like the following:
Form <------------------------------- Question
^ ^
| |
| |
Response <---------------------------- Answer
Can anyone help me? Thanks for all.

The approach seems wrong. "One form can have many questions. One form can have many answer. An answer belongs to one form."
No, an answer should refer to a specific question. If a question belongs to a form, then the answers do belong to that form implicitly, but only indirectly, because they belong to a question.
It boils down to:
One form can have many questions.
One question can have many answers.
In the form of tables:
table form (form_id, ...)
table question (question_id, question_text, ..., form_id)
table answer (answer_id, answer_text, ..., question_id)
UPDATE according to latest request edit:
So you have forms, questions and aswers as shown above. Additionally you want to store the responses. A response contains several answers and some statistics.
table response (response_id, form_id, submit_time, ...)
table response_answer (response_id, answer_id)

Related

Normalizing a table to handle row linking

I have a table that stores questions for some exam with the following structure:
ID: ID of the question. ]
Number: Question number in the exam.
Max: Maximum points for the question a student can get.
Text: The question description.
isSubQ: Is it a Sub Question? Boolean value.
part(Optional): What part of the question if it is a sub-question.
exam: ID of the exam(ForiegnKey).
I feel this structure to store sub-parts of the parent question is redundant and a much better relational schema exists. What is the best way to store sub-questions in other words, what is the best way to link two question rows(one as the parent and one as the sub-part)?
To give more clarity, here is an example. Let's say question 3 on the exam has parts a,b, and c. Part a has a different text, and points than other parts(b and c) but is still related to the parent question which is 3.

Relational database design for surveys with different answer types?

https://postimg.cc/vDqtCNBc
We have different survey types. Each survey type has its own unique sections, and each section has its own unique questions. (Questions are not currently re-used by different survey types and may never be). For questions that are answered by tickboxes ('tick one only' or 'tick all that apply'), there are predefined answers, which are listed in the form_possible_answers table. The right side of the image shows the table relationships for this. Bridge table form_questions_answers_bridge determines which of the possible answers can belong to a particular question. (E.g. 'What condition is your car in?' will only be linked to the 'Excellent', 'Moderate', 'Poor' answers.)
When a form is submitted, details such as the form type and submission date are stored in the form_submissions table. This is where it gets complicated. There are 3 question types: a question that can have a single predefined answer (answered by ticking a single tickbox), a question that can have multiple predefined answers (answered by ticking all tickboxes that apply), and a question that is answered by user text input. I have a submitted_questions table that relates all submitted questions to the form submission. For text-answer questions (knowledge obtainable from the form_questions table), there is a text_answers table. For questions that are answered via a checkbox (single or multiple) there is a multiple_answers table that references the submitted question id and predefined answer.
The multiple_answers table references both the form_questions and form_possible_answers tables. One may question why I then have a bridge table form_questions_answers_bridge. It is a lookup--so I can enforce a constraint when writing data is attempted. (E.g. If the lookup/bridge says the answer 'no' is not possible for 'what color shirt?', then the data won't be written.) Is this a just argument?

SQL - User Stats table?

I am working on a multi-choice questions web-app. A user will have to answer 50 to 100 multiple-choice questions. The number of possible answers for each question can differ from 3 to 4 but there is only 1 correct answer.
I would like to create a page stats where the user can come back to the answered questions and see what answer he did select. I am not sure what would be the proper way to do that.
So far I have:
users table
questions table
answers table
I would organize the answers table like this:
In that way:
you will not have columns with null values,
you can have questions with more than 3-4 answers too without adding any extra column
you can have questions with multiple right answers too
Then, you will need a table, which holds the user's answers:
Now, you can easily construct a query, which shows the required data.

Can a table field be founded and then updated with a value in mvc Controller

yeah so the question is pretty simple: Can a table field be found using and ID and then updated with a value in mvc Controller?
And before anyone gets downvote crazy... I have no example because I don't know how to do this... it's not a home work question or any other things that you down vote on this site. It's a simple question that I need an answer and a example.
I now you can use the .find to and pass the ID... to a dataset but there is nothing I can find that shows you how to edit the values in the table once you find it.
So this part I get, This will allow me to find the the id with in the dataset.
TEST_DS tEST_DS = db.TEST_DS.Find(id);
if (tEST_DS == null)
{
return HttpNotFound();
}
Also I know that you see a call to the database like This
db.TEST_TABLE.Add(TEST_DS);
I don't see an option for edit or anything like that.
The task is to update a table field, The field is a status field so when the user makes a change on the mvc create form. When they create a record I want to mark a different table's status field with a number that represents a record was created.
Did a search did not see anything.. thanks may be something mvc can't do because of it's limitations or something.
If you do answer question I will need an example of a link to an example on how to do it. Thanks!!
I was able to make this work some I will post It there for others as they might have the same issues when dealing that mvc limitations and such.
So the answer is to simply have your data table in the dbcontext that you need to edit...
The code would not post.. it asking for 4 spaces and I gave it 4 spaces and it did not work so I added a picture.
See CodeAnswer Image

Database design for an online quiz

I am designing an online math quiz for a college project and having some trouble with designing my database.
The basic idea of the website is as follows:
A teacher, once registered may log in and add questions to their account. They can choose between making the questions multiple choice OR true or false. They can also choose between making their questions public or private. (If they choose to make the questions public, other teachers may view the questions.)
At any time the teacher may create a quiz for their students using the questions in their private bank and / or questions from the public bank. Each question may be used in multiple quizzes.
The idea is that the students will later log in and do the quiz; their answers are stored and the teacher can generate reports and check how the students did individually / highest and lowest scoring questions etc.
I am having some trouble deciding how to store the quizzes and questions which I hope somebody may be able to help me with.
So far I have the following:
‘Question’ table with attributes: QuestionID, SubjectArea, Concept, QuestionText, TeacherID, QuestionType, PublicYorN
‘MCQuestions’ table with attributes: QuestionID, AnsA, AnsB, AnsC, AnsD, AnsE, CorrectAns
‘TorFQuestions’ table with attributes: QuestionID, CorrectAns
‘Quiz’ table with attributes: QuizID, CreationDate, TeacherID
I think I then need another table as follows:
‘QuizQuestions’ and the only attributes will be QuizID, QuestionID which together make a concatenated primary key.
I feel like I should have a separate table to store answers to questions and I'm not sure if I need to separate true or false questions and multiple choice questions as I have done above.
(Obviously there are other tables containing user data etc. but this is the part I’m concerned with.)
Any advice / input is greatly appreciated!
A simple yet flexible design would be something like this:
Questions table (id, text, correct answer id, all the other question related data)
Answers table (id, question id text, all the other answer related data)
Quiz table (id, text, all the other quiz related data)
Quiz questions table (quiz id, question id, question display order, other related data such as question weight might also be added)
Quiz results table (quiz id, question id, answer id, user id, all the other related data such as date of answer and such).
I think my only input to this open ended question is:
Combine MCQuestions and TorF Questions to have a different format. Answers table has 4 cols. QID, AnswerID,answer,Correct Y/N.
Then a true or false question has 2 rows - eg. QID=888, AID=1,Ans=TRUE,true. Next row is QID=888, AID=2,Ans=FALSE,false.
Multiple choice has several - so which is the bright yellowy thing? QID=889, AID=3,Ans=Moon,false. QID=889, AID=4,Ans=Sun,true. QID=889, AID=5,Ans=Mars,false.
Then you grab a list of answers and can populate a set of radio buttons with the QID and AID values. Then you are matching integers in your code rather than long text strings which will need to be passed around for matching. Might make it a bit easier in case of special chars in the answers or whatever and you're matching the quiz results to a unique key.
Apart from that minor change it's difficult to help without knowing any other constraints...
Nick