Normalizing a table to handle row linking - sql

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.

Related

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?

LIKE query with a varying number of LIKE’s

My current test revolves around a hypothetical database consisting of a PERSON table populated with their details.
I can undertake a simple LIKE query were I return the details of say a person or persons named Tom.
My question is how can I build a query were there may be multiple names and return their details?
I my exercise there is a checkbox list of names and I need to return the details for all those I select?
Thank you in advance for your assistance.

Circular relationship in DB design

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)

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

Merging SQL views [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working for the IT Department of a college as a student worker and have very limited knowledge in using SQL (I am not majoring in Computer Science/Engineering). I'll try my best to describe what I want to accomplish here.
I want to create a table that includes info about new students, basically : id, first name, last name, need exams (Y/N), Course101 section, instructor.
My problem is, exchange and transfer students and also some first year students would not have to/did not sign up for Course101, so using WHERE studnt_course = 'Course101%' will leave out those students. I would like to pick up those students in my view and display their Course101 section, Instructor values as Null.
I am thinking about making two views, one for all new students, and one for students with Course101 only, and do some kind of merging/union but not sure how to actually do that.
Any help will be greatly appreciated!
It's still a bit vague what the current tables actually look like which makes it hard to give a good suggestion.
Based on what you've given us I'd suggest looking into a LEFT INNNER JOIN which would put NULL where the two tables don't overlap.
If you are interested in learning database design (rather than just solving this particular problem) I'd suggest looking into proper database design.
Create two queries. One for the 101 students and one for the overseas/transfers (you will need to work out what the WHERE clause if for that)
Get each one working to your satisfaction.
If every thing is the same apart from the WHERE clause then:
Then grab the conditions for each, wrap them in brackets and put an OR in between, put that into one query
So something like:
SELECT Name, Id,ShoeSize
FROM Students
WHERE (studnt_course = 'Course101%') OR (transferred = 1 OR is_exchange = 1)
i.e., So all students WHERE <this> OR <that> is true
Other wise (e.g., using diffrent tables):
Make sure that you are selecting the same column names in you SELECT statement
If one column has the same info but is call some thing different you can go:
<columnName> AS <NameYouWantToCallIt>
Make sure the column names are in the same order.
Then put the word UNION in-between the two queries.
This will combine the results from both queries (and remove duplicates).
SELECT Name, Id, ShoeSize
FROM Students
WHERE studnt_course = 'Course101%'
UNION
SELECT Name, Id, FootSize AS ShoeSize
FROM exchangeStudents
WHERE transferred = 1 OR is_exchange = 1